<jupyter_start><jupyter_text>Basic operations and types  Assignment and evaluation<jupyter_code>a = 1
b = a + 2
b + a<jupyter_output><empty_output><jupyter_text>Evaluation with the `print`-function<jupyter_code>a = 1
b = a + 2
print(b + a)<jupyter_output>4<jupyter_text>Note that the variable type is not declared:```Cint a = 1;```In python this is done automatically.<jupyter_code>type(1)
type(1.0)
i = 1
f = 1.0
s = 'Hello, world!'
b = 1 == 0
type(i), type(f), type(s), type(b)
f = 1.0
f_i = int(f)
print(f, 'to', f_i, type(f_i))

f = 1.6
f_i = int(f)
print(f, 'to', f_i, type(f_i))

i = 1
i_f = float(i)
print(i, 'to', i_f, type(i_f))

i = 1
i_s = str(s)
print(i, 'to', i_s, type(i_s))<jupyter_output>1.0 to 1 <class 'int'>
1.6 to 1 <class 'int'>
1 to 1.0 <class 'float'>
1 to Hello, world! <class 'str'><jupyter_text>Containers: Lists, tuples, strings and dictionaries<jupyter_code>colors = ['red', 'blue', 'green', 'black', 'white']
objects = ['cube', 3, colors]
colors, objects
type(colors)
colors[0], colors[2], colors[4]
#colors[5]
colors[-1], colors[4]
colors[-2], colors[3]
colors[0:2]
colors[2:-2]
colors[:]
colors[3:], colors[:3]
colors[0:5:2], colors[::2], colors[::-1]<jupyter_output><empty_output><jupyter_text>Assignment to existing lists, and list modules (list manipulation):<jupyter_code>colors[0] = 'yellow'
colors
colors[2:4] = ['gray', 'purple']
colors
colors.append('pink')
colors
colors.pop(-1)
colors.extend(['pink', 'orange'])
colors<jupyter_output><empty_output><jupyter_text>Just like in bash, you can type `colors.` and hit TAB, to display a list for tab-completion.Basic list-operations (note that lists are not arrays!)<jupyter_code>colors + ['magenta', 'cyan']
colors
numbers = [1, 2, 3]
numbers + numbers, numbers * 2<jupyter_output><empty_output><jupyter_text>Tuples behave just like lists, but they are immutable.<jupyter_code>fruit = ('apple', 'banana', 'pear')
#fruit[1] = 'pineapple'<jupyter_output><empty_output><jupyter_text>Assignment using lists:<jupyter_code>numbers = [1, 2, 3]
a = numbers
print(a)

a1, a2, a3 = numbers
print(a1, a2, a3)

a = 1, 2, 3
print(a)

a1, a2, a3 = 1, 2, 3
print(a)<jupyter_output>[1, 2, 3]
1 2 3
(1, 2, 3)
(1, 2, 3)<jupyter_text>Strings are immutable lists of characters, therefore they are essentially tuples:<jupyter_code>s = "Hello, world!"
s[2:4]
#s[2:4] = "zz"
s1 = "Hello, world!"
s2 = 'Hello, world!'
s3 = '''Hello, world!'''
s4 = """
Hello, world!"""
print(s1, s2, s3, s4)<jupyter_output>Hello, world! Hello, world! Hello, world! 
Hello, world!<jupyter_text>Again, strings come with many useful methods. With `s.` and hitting TAB, you can have a look at all of them.<jupyter_code>s.replace('l', 'z')
s.find('l')<jupyter_output><empty_output><jupyter_text>Dictionaries are unordered, but labeled lists. Similar to tables.<jupyter_code>cargo = {'bananas': 45, 'apples': 32, 'milk': 40}
cargo
#cargo[0]
cargo['bananas']
cargo['milk'] = 0
cargo
cargo.keys()
cargo.values()
cargo1, cargo2, cargo3 = cargo.values()
cargo1, cargo2, cargo3<jupyter_output><empty_output><jupyter_text>Careful with the assignment: use tuples instead of lists as much as possible!<jupyter_code>numbers = [1, 2, 3]
a = numbers
print(numbers, a)

a[0] = 100
print(a[0])

print(numbers, a)
numbers = (1, 2, 3)
a = numbers
print(numbers, a)

#a[0] = 100
#print(numbers[0])

#print(numbers, a)
numbers = [1, 2, 3]
a = numbers.copy()
print(numbers, a)

a[0] = 100
print(numbers[0])

print(numbers, a)<jupyter_output>[1, 2, 3] [1, 2, 3]
1
[1, 2, 3] [100, 2, 3]<jupyter_text>if/elif/else and True/False<jupyter_code>if 2 < 4:
    print('2 < 4')
if 2 > 4:
    print('2 > 4')
if 2 > 4:
    print('2 > 4')
else:
    print('2 < 4')
if 2 > 4:
    print('2 > 4')
if 4 > 4:
    print('4 > 4')
else:
    print('nothing > 4')
if 6 > 4:
    print('6 > 4')
if 8 > 4:
    print('8 > 4')
else:
    print('nothing > 4')
if 6 > 4:
    print('6 > 4')
elif 8 > 4:
    print('8 > 4')
else:
    print('nothing > 4')
a = True
b = False

if a:
    print('a is True')
if b:
    print('b is False')
a = 5 < 12
a
a = 6 == 6
b = 10 != 6
print(a, b)

if a and b:
    print('for AND both have to be true')

b = False
if a or b:
    print('for OR only one needs to be true')

if not b:
    print('NOT negates the following boolean')<jupyter_output>True True
for AND both have to be true
for OR only one needs to be true<jupyter_text>Loops with `for` and `while`<jupyter_code>for i in range(4):
    print(i)
letters = ('a', 'b', 'c', 'd')
for i in range(4):
    print('index i =', i, ', letters[i] =', letters[i])
letters = ('a', 'b', 'c', 'd')
for letter in letters:
    print(letter)
letters = ('a', 'b', 'c', 'd')
for i, letter in enumerate(letters):
    print(i, letter)
irange = range(4)
letters = ('a', 'b', 'c', 'd')
for i, letter in zip(irange, letters):
    print(i, letter)
numbers = (1, 2, 3, 4)
colors = ('red', 'green', 'blue', 'yellow')
letters = ('a', 'b', 'c', 'd')
for n, c, l in zip(numbers, colors, letters):
    print(n, c, l)
i = 0
while(i < 4):
    print(i)
    i = i + 1
i = 0
letters = ('a', 'b', 'c', 'd')
while(i < 4):
    print(i, letters[i])
    i = i + 1
i = 0
while(True):
    print(i)
    if i == 3:
        break
        
    i = i + 1
x, y = (0, 1, 2, 3, 4, 5, 6, 7), (0, 1, 2, 3, 4, 5, 6, 7)
xy2 = []
for x_ in x:
    for y_ in y:
        xy2.append(x_**2 + y_**2)

print(xy2)<jupyter_output>[0, 1, 4, 9, 16, 25, 36, 49, 1, 2, 5, 10, 17, 26, 37, 50, 4, 5, 8, 13, 20, 29, 40, 53, 9, 10, 13, 18, 25, 34, 45, 58, 16, 17, 20, 25, 32, 41, 52, 65, 25, 26, 29, 34, 41, 50, 61, 74, 36, 37, 40, 45, 52, 61, 72, 85, 49, 50, 53, 58, 65, 74, 85, 98]
{'fraction_non_alphanumeric': 0.19033124440465532, 'fraction_numerical': 0.06392121754700089, 'mean_word_length': 4.269811320754717, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '387512', 'license': 'MIT', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2374, 'n_words': 736, 'path': 'lectures/1_Basics/1_2_basics_types_loops.ipynb', 'repo_name': 'mwaidele/python_lecture_KIS2020', 'size': '5585'}