In [3]:
print('Hello world!')


Hello world!

Variable


In [4]:
my_var = 1
my_var


Out[4]:
1

List


In [7]:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

In [10]:
my_list[0]


Out[10]:
1

In [11]:
my_string = "Hello world"
my_string[0]


Out[11]:
'H'

In [12]:
"HeLlO WoRlD".lower()


Out[12]:
'hello world'

In [15]:
"    HeLlO WoRlD    ".strip()


Out[15]:
'HeLlO WoRlD'

In [16]:
"hello world".split(" ")


Out[16]:
['hello', 'world']

In [17]:
'; '.join(['hello', 'world'])


Out[17]:
'hello; world'

Dictionary


In [18]:
capital_cities = {'Poland': 'Warsaw', 'Belarus': 'Minsk', 'Czech': 'Prague'}
capital_cities['Poland']


Out[18]:
'Warsaw'

Conditions


In [21]:
country = input("What country are you looking for?")

if country in capital_cities:
    print('We know a caplital of {0}: "{1}"'.format(country, capital_cities[country]) )
else:
    input_var = input("Please help me, what is the capital of " + country + ": ")
    capital_cities[country] = input_var.strip()


What country are you looking for?USA
We know a caplital of USA: "Washington"

In [23]:
capital_cities


Out[23]:
{'Belarus': 'Minsk',
 'Czech': 'Prague',
 'Poland': 'Warsaw',
 'USA': 'Washington'}

Loop - for


In [27]:
output_list = []

for x in range(10):
    output_list.append(x) #append elemnt into list
    
output_list


Out[27]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [28]:
def doubled_fun(value):
    return value * 2

output_list = []
for x in range(10):
    output_list.append( doubled_fun(x) )
    
output_list


Out[28]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Mapping


In [29]:
my_list = range(10)
list(my_list)


Out[29]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [31]:
def doubled_fun(value):
    return value * 2
    
list( map(doubled_fun, my_list) )


Out[31]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Lambda - anonymous function


In [32]:
list( map(lambda x: x*2, my_list) )


Out[32]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

List Comprehension


In [33]:
[x for x in range(10)]


Out[33]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [34]:
[x*2 for x in range(10)]


Out[34]:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

In [35]:
[x*2 for x in range(10) if x % 2 == 0]


Out[35]:
[0, 4, 8, 12, 16]

In [ ]: