Python basics


Format strings


In [4]:
age = 29
name = 'Bazso'

print('My name is {one} and I am {two} years old'.format(one=name, two=age))
My name is Bazso and I am 29 years old

List

In [5]:
my_list = [1, 2, 3]
my_list.append(4)
my_list
Out[5]:
[1, 2, 3, 4]
In [11]:
nested_list = [1, 2, [3, 4], 'Hello']

print(nested_list[0])
print(nested_list[2])
print(nested_list[2][0])
print(nested_list[3])
1
[3, 4]
3
Hello

Indexing Strings and Lists

In [14]:
my_str = 'abcdef'
my_list = [1, 2, [3, 'Hello']]

print(my_str[0:3])
print(my_list[2:])
abc
[[3, 'Hello']]

Dictionaries

In [18]:
d = {'key1' : 'Hello', 'key2' : 3, 'key3' : [1, 2, [1, 'Hello']]}
print(d['key1'])
print(d['key2'])
print(d['key3'])
Hello
3
[1, 2, [1, 'Hello']]
In [20]:
d = {'k1' : {'inner_k1' : [1, 2, 3]}}
print(d['k1'])
print(d['k1']['inner_k1'])
{'inner_k1': [1, 2, 3]}
[1, 2, 3]

Tuples

In [22]:
my_tuple = (1, 2, 3) # tuples are immutable objects !

print(my_tuple[0])

# my_tuple[0] = 2 # illegal
1

Sets

In [29]:
# collection of unique elements
my_set = {1, 2, 3}
print(my_set)

my_set.add(4)
print(my_set)

my_set.add(4)
print(my_set) # because unique elements!

my_list = [1, 1, 1, 2]

s = set(my_list)
print(s)
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4}
{1, 2}

Loops

In [30]:
seq = [1, 2, 3]

for s in seq:
    print(s)
1
2
3
In [33]:
for i in range(0, 3):
    print(i)
0
1
2
In [34]:
print( list(range(0, 5)) ) # range is a function which is generate a sequance
[0, 1, 2, 3, 4]

List comprehension

In [38]:
s = [1, 2, 3]
out = []

for i in s:
    out.append(i*i)

print(out)

######  This is the same

out = [i ** 2 for i in s]
print(out)
[1, 4, 9]
[1, 4, 9]
In [32]:
i = 0
while i < 3:
    print('iteration {}...'.format(i))
    i += 1
iteration 0...
iteration 1...
iteration 2...

Functions

In [39]:
def func1(name = 'Default name'):
    print('Hello ' + name)
    
func1()
func1('Bazso')
Hello Default name
Hello Bazso
In [43]:
def square_func(num):
    """
    This is a doc stings
    return the squares of the param
    jupyter notebook shift+tab show the doc string
    """
    return num**2

num = square_func(3)
print(num)
9

Map function

In [50]:
def times2(var):
    return var * 2

seq = list(range(0, 5))
print(seq)

seq_times2 = list( map(times2, seq) )
print(seq_times2)
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]

Lambda expression

In [52]:
print(seq)

seq2 = list( map( lambda num : num*3, seq ) )
print(seq2)
[0, 1, 2, 3, 4]
[0, 3, 6, 9, 12]

Filters

In [55]:
s = list( filter( lambda num: num%2 == 0, seq ) )

print(seq)
print(s)
[0, 1, 2, 3, 4]
[0, 2, 4]

Built in methods

Huge number of built in methods can be used ...

In [58]:
s = 'hello my name is Bazso #bazso'

print( s.upper() )
print( s.split() )
print( s.split('#') )
HELLO MY NAME IS BAZSO #BAZSO
['hello', 'my', 'name', 'is', 'Bazso', '#bazso']
['hello my name is Bazso ', 'bazso']
In [64]:
d = {'name':'Bazso', 'age':29}

print( d.keys() )
print( d.values() )

for keys in d.keys():
    print(d[keys])
dict_keys(['name', 'age'])
dict_values(['Bazso', 29])
Bazso
29
In [65]:
'x' in ['x', 1, 3]
Out[65]:
True

Tuple unpacking

In [69]:
x = [(1,2), (3,4), (5,6)]

for i in x:
    print(i)
    
# unpacking
for a, b in x:
    print(a)
(1, 2)
(3, 4)
(5, 6)
1
3
5