1. Basic Python Types

1.1 Verifying the python version you are using


In [1]:
import sys
print(sys.version)

At this point anything above python 3.5 should be ok.

1.2 Perform basic operations


In [2]:
print(2 / 3)
print(2 // 3)
print(2 - 3)
print(2 * 3)
print(2 ** 3)
print(12 % 5)

Notes:

1.3 String additions


In [3]:
print("Welcome" + " to the " + "BornAgain" + " School")

Notes:

1.4 list and tuples addition


In [4]:
print([1, 2, 3, 4] + [5, 6, 7, 8])
print((1, 2, 3, 4) + (5, 6, 7, 8))

Notes:

1.5 Boolean logic: comparisons


In [5]:
print(5 < 6)
print(5 >= 6)
print(5 <= 6)
print(5 == 6)
print(5 in [1, 2, 3, 4, 5, 6, 7])

Notes:

1.6 Boolean logic: operations


In [6]:
print(5 < 6 and 5 >= 6)
print(5 >= 6 or 5 < 6)
print(not 5 == 6)

Notes:

1.7 Exercise:

Use what you have seen to answer:

  • Is 10 smaller than 5 ?
  • Is 10 divisible by 5 ?
  • Is 10 included in the following sequence: 0,10,20,30?
  • Is 10 bigger than 2 or bigger than 20 ?

In [ ]:

Notes:

1.8 Python Objects


In [7]:
a = 2.0
b = 3.0
c = 4.0

print(type(a))
#print(float.__dict__)
print(a / b)
print(a.__truediv__(b))
print(c >= b)
print(c.__ge__(b))

Notes:

1.9 Create a list


In [8]:
a = [
    0.0,
    1.0,
    6.2,
    5.333,
    9,
    4,
    3.4]

print("a is equal to ", a)
print("a is of type: ", type(a))
print("a[0] is equal to ", a[0], "and is of type: ", type(a[0]))
print("a[4] is equal to ", a[4], "and is of type: ", type(a[4]))

Notes:

1.10 Slice a list


In [9]:
b = a[2:4]
print(b)

b = a[:4]
print(b)

b = a[2:]
print(b)

b = a[::-1]
print(b)

b = a[2:6:2]
print(b)

Notes:

1.11 Exercise:

Create a list with ten values from 0 to 9 and slice it to have only the values from 3 to 7 inside


In [ ]:

Notes:

2. Python: Utilizing Sequences

2.1 Iterators


In [10]:
iterator = iter(a)
print(type(iterator))

print(next(iterator))
print(next(iterator))
print(next(iterator))
print(next(iterator))

Notes:

2.2 For loop

Design a for loop printing the elements of a


In [11]:
for element in a:
    print(element, type(element))

Notes:

2.3 if, elif, else statements

Design a for loop over the previsouly defined list a and print 'smaller', 'equal'or 'bigger'depending on if the element in a meets these condition compared to 4.


In [12]:
b = 4

for element in a:
    if element < b:
        print(element, "is smaller than ", b)
    elif element == b:
        print(element, "is equal to ", b)
    else:
        print(element, "is bigger than ",b)

Notes:

2.4 Exercise:

Create a list with ten values from 0 to 9 and loop over it while checking if the element multiplied by two is still smaller than 10.


In [ ]:

Notes: