From 82e7a91ef7d238651cd4de77a381bed0c3c0f9e2 Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 19:20:43 +0100 Subject: [PATCH 1/6] Learned Variables --- python_sandbox_starter/variables.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/python_sandbox_starter/variables.py b/python_sandbox_starter/variables.py index 7ffc3e4..4e7c26d 100644 --- a/python_sandbox_starter/variables.py +++ b/python_sandbox_starter/variables.py @@ -1,4 +1,3 @@ -# A variable is a container for a value, which can be of various types ''' This is a @@ -13,3 +12,24 @@ - Must start with a letter or an underscore - Can have numbers but can not start with one """ +# x = 1 #int +# y = 2.5 #float +# name = "John" #string +# is_cool = True #bool + +#multi assigment + +x, y, name, is_cool = (1, 2.5, "john", True) + +print("Hello") + +# basic math +a = x + y + +# Casting + +x = str(x) +y = int(y) +z = float(y) + +print(type(z), z) From 96c4aec182543b22df2ecc69bb84372acaedcf96 Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 19:34:34 +0100 Subject: [PATCH 2/6] strings --- python_sandbox_starter/strings.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python_sandbox_starter/strings.py b/python_sandbox_starter/strings.py index 0942500..30f9981 100644 --- a/python_sandbox_starter/strings.py +++ b/python_sandbox_starter/strings.py @@ -1,6 +1,22 @@ # Strings in python are surrounded by either single or double quotation marks. Let's look at string formatting and some string methods +name = "Nikolas" +age = 32 + +#concatenate +# print('Hello i am ' + name + 'I am ' + str(age)) # String Formatting +# arguments by position +# print('Mt name is {name} and i am {age}'.format(name=name, age=age)) + +#F-Strings (3.6+) +# print(f'Hello, my name is {name} and i am {age}') + # String Methods + +s = 'hello kitty' + +# capitalizy +print(s.find("e")) From 982f0895dc3285966fd1390ca3f7544d0f97465e Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 20:11:30 +0100 Subject: [PATCH 3/6] lists --- python_sandbox_starter/lists.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/python_sandbox_starter/lists.py b/python_sandbox_starter/lists.py index 6cd3bbd..f0f8ae7 100644 --- a/python_sandbox_starter/lists.py +++ b/python_sandbox_starter/lists.py @@ -1 +1,33 @@ # A List is a collection which is ordered and changeable. Allows duplicate members. + +# create a list +numbers = [1,2,3,4,5] +fruits = ['apples', 'oranges', 'grapes'] + +# use constructor +# numbers2 = list((1,2,3,4,5)) + +# print(numbers, numbers2) + +# get a value +print(fruits[1]) +# get length +print(len(fruits)) +# append +fruits.append("mangos") +# remove +fruits.remove("apples") +# insert +fruits.insert(2, "papaya") +# Remove with pop +fruits.pop(2) +# reverse +fruits.reverse() +# sort +fruits.sort() +# reverse sort +fruits.sort(reverse=True) +# change value +fruits[0] = 'bananas' + +print(fruits) \ No newline at end of file From a97205125da623c51ef5446ed21d957420cc1196 Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 20:28:15 +0100 Subject: [PATCH 4/6] sets and tuples --- python_sandbox_starter/tuples_sets.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python_sandbox_starter/tuples_sets.py b/python_sandbox_starter/tuples_sets.py index 90d2fc4..5e5b66e 100644 --- a/python_sandbox_starter/tuples_sets.py +++ b/python_sandbox_starter/tuples_sets.py @@ -1,5 +1,28 @@ # A Tuple is a collection which is ordered and unchangeable. Allows duplicate members. +# create a tuple +fruits = ('Apples', 'Oranges', 'Grapes') +fruits2 = 'Mango' + +# delete +# del fruits2 + +print(fruits) +print(fruits2) # A Set is a collection which is unordered and unindexed. No duplicate members. + +# create a set +fruits_set = {'Apples', 'Orange', 'Mango'} +# check if in set +print('Apples' in fruits_set) +# Add to set +fruits_set.add('papaya') +print(fruits_set) +# remove +fruits_set.remove('papaya') +print(fruits_set) +# clear set +fruits_set.clear() +print(fruits_set) From b11fbba42cedc52453eefabbc3e2fcc14c47e173 Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 20:40:19 +0100 Subject: [PATCH 5/6] dictionaries --- python_sandbox_starter/dictionaries.py | 42 +++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/python_sandbox_starter/dictionaries.py b/python_sandbox_starter/dictionaries.py index 5576e3d..40fa733 100644 --- a/python_sandbox_starter/dictionaries.py +++ b/python_sandbox_starter/dictionaries.py @@ -1,2 +1,42 @@ -# A Dictionary is a collection which is unordered, changeable and indexed. No duplicate members. + # A Dictionary is a collection which is unordered, changeable and indexed. No duplicate members. # Read more about dictionaries at https://docs.python.org/3/tutorial/datastructures.html#dictionaries + +# +person = { + 'first_name': 'John', + 'last_name': 'Doe', + 'age': 30 +} + +print(person, type(person)) +# get vlaue +print(person['first_name']) +print(person.get('last_name')) + +# add key value +person['phone'] = '34343434' + +# get dict key +print(person.keys()) + +# remove item + +del person['age'] +person.pop('phone') + +# clear +# person.clear() + +# legnth +print(len(person)) + +# list of dict +people = [ + {'name': 'niko', 'age': 5}, + {'name': 'yan', 'age': 54}, +] + +print(people[0]['name']) + + + From 7725f5bfb5c10ad67c51951a3b46ba99a8d30e24 Mon Sep 17 00:00:00 2001 From: NikolasKorakidis Date: Wed, 4 Nov 2020 21:07:22 +0100 Subject: [PATCH 6/6] dictionaries --- python_sandbox_starter/functions.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/python_sandbox_starter/functions.py b/python_sandbox_starter/functions.py index a1115ff..98844a9 100644 --- a/python_sandbox_starter/functions.py +++ b/python_sandbox_starter/functions.py @@ -1,7 +1,25 @@ # A function is a block of code which only runs when it is called. In Python, we do not use parentheses and curly brackets, we use indentation with tabs or spaces +# Create a function +def sayHello(name): + print(f'Hello {name} !') +def sayHelloSam(name='Sam'): + print(f'Hello to you to {name}') -# A lambda function is a small anonymous function. +sayHello('Nikolas') +sayHelloSam() + +# return values +def getSum(num1, num2): + total = num1 + num2 + return total + +print(getSum(3,5)) + + +# A lambda function is a small anonymous function. +value = lambda n1, n2: n1 + n2 +print(value(2,3)) # A lambda function can take any number of arguments, but can only have one expression. Very similar to JS arrow functions