<jupyter_start><jupyter_text>Introduction to programming* Assigning variables* DAta types* Operators* functions  Assigning variables<jupyter_code>name = 'Sven' # String type
age = 87 # integer type
weight = 42.578329 # float type 
name
print('hello', name)
type(name)
type(age)
type(weight)<jupyter_output><empty_output><jupyter_text>Math operators<jupyter_code>2 + 2 # addition
6 * 7 # multiplication
2 ** 16 # Power<jupyter_output><empty_output><jupyter_text>Logic Operators<jupyter_code>3 > 4
result = 3 > 4
result
type(result)
True and True
True and False
True or False
# You use logical operators during data filtering<jupyter_output><empty_output><jupyter_text>Sequences: Lists and Tuples<jupyter_code>numbers = [1, 2, 3]
numbers
type(numbers)
numbers[0] # Get element of index 0 NB 0-indexing in python
for num in numbers:
    print('hi there')
    print(num)
print('finally done')
# appending to lists
numbers.append(4)
numbers
help(numbers) # Get numbers
whatever = [1,2]<jupyter_output><empty_output><jupyter_text>Tuplesordered sequence of elements. They can not be changed once created (they are 'immutable')<jupyter_code>a_tuple = (1, 2, 3)
a_tuple
type(a_tuple)
another_tuple = ('blue', 'green', 'red')
another_tuple = ('blue', 'green', 2)
a_list = [1, 2, 3]<jupyter_output><empty_output><jupyter_text>Exercise: Understanding lists and tuples* What happens when you execute a_list[1] = 5?* What happens when you execute a_tuple[2] = 5?* What does type(a_tuple) tell you about a_tuple? What happens when you execute a_list[1] = 5?<jupyter_code>a_list
a_list[1] = 5
string_list = ['a', 'b', 'c']
print(string_list)
string_list[1] = 'd'
print(string_list)
a_list<jupyter_output><empty_output><jupyter_text>What happens when you execute a_tuple[2] = 5?<jupyter_code>a_tuple[2] = 5<jupyter_output><empty_output><jupyter_text>What does type(a_tuple) tell you about a_tuple?<jupyter_code>type(a_tuple)
a_list[-1] # start indexing from the end
%who<jupyter_output>a_list	 a_tuple	 age	 another_tuple	 name	 num	 numbers	 result	 string_list	 
weight	 whatever<jupyter_text>Shortkeys for cells:Esc - switching to 'selecting' the cell* m - markdown* y - code* a - cell above* b - cell below* x - delete cell  DictionariesA dictionary is a container that holds pairs of objects - keys and values.<jupyter_code>translation = {'one': 'first', 'two': 'second'} # one=key first=value
translation['one'] # give me the value that corresponds to key 'one'
translation['two']
type(translation)
rev = {'first': 'one', 'second': 'two'}
rev['first']
rev['third'] = 'three' # For key 'third' assign value 'three'
rev
# list for loop
for num in numbers:
    print('hi there')
    print(num)
rev
for key, value in rev.items():
    print(key, '->', value)
for key in rev.keys():
    print(key)<jupyter_output>first
second
third<jupyter_text>Exercise: Dictionaries* First, print the value of the rev dictionary to the screen.* Reassign the value that corresponds to the key second so that it no longer reads “two” but instead 2.* Print the value of rev to the screen again to see if the value has changed.<jupyter_code>rev
print(rev)
rev['second'] = 2
rev
type(rev['second'])
rev['second'] = '2'
type(rev['second'])<jupyter_output><empty_output><jupyter_text>Remove elements from a list or dict?<jupyter_code>a_list = [1, 2, 3]
a_list
a_list.remove(2) # remove first value in list that matches 2
a_list
a_list.pop(0) # remove element from list at index 0
a_list
a_dict = {'hello': 2}
a_dict.pop('hello') # remove key-value pair with key 'hello' form dict
a_dict
# which type of brackets
a_dict = {'one': 'first'}
a_list = [1, 2]
a_tuple = (1, 2)

#indexing (getting a value) uses square brackets
a_dict['one']
a_list[0]
a_tuple[0]

# calling something (i.e. a function or method) uses parentheses
print()<jupyter_output><jupyter_text>Functions<jupyter_code>def add(a, b): # a and b are input arguments
    result = a + b 
    return result # return statement tells what function should give back

z = add(20, 22)
z
a_list<jupyter_output><empty_output><jupyter_text>Exercise: when to use tuples, lists, dictionariesFor the following practical examples write down whether you would use a tuple, list, or dictionary to represent the data.1. Storing the favourite superpowers of the people in this class when later in our notebook we need to be able to answer: what is `name`'s favourite superpower?2. A point in 2D space (i.e. with x and y coordinate)3. The names of people in this class4. (optional) The age and weight of people in this class, so we can later ask what is `name`'s age and weight?<jupyter_code># 1.
superpowers = {'sven': 'flying', 'jens': 'changing into an animal'}
superpowers['jens']
#2. 
point = (0.23, 0.56)
new_point = (0.1, 0.34)
#3.
names = ['sven', 'jens']
names.append('Fangxin')
names
#4.
people_data = {'sven': (87, 42.3), 'jens': (99, 12.4)}
people_data['sven']
(87, 42.3)
{'age': 87, 'weight': 52.3}
numpy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12022630834512023, 'fraction_numerical': 0.02263083451202263, 'mean_word_length': 5.653225806451613, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '612654', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1710, 'n_words': 577, 'path': 'files/python-intro/1-introduction-to-python.ipynb', 'repo_name': 'escience-academy/2021-ssi-in-ml-workshops', 'size': '4949'}