<jupyter_start><jupyter_text>Python für Psychologen Class Session 1 We program because sometimes we want to do something very often - accurately and fast.If we do something only a few times or once, it's often simpler to do it by hand.But with many iterations, programming starts paying off. Some of the most basic mechanisms of Python are- variables, which should be a unique combination of letters (strictly speaking, Python doesn't know variables, only names)- lists, which are initialized with square brackets []- strings, which are initialized with '' or ""We *collect* stuff, such as strings, in lists. We can collect almost everything in lists - we can have lists of lists.We *assign* something to variables, such as strings or lists.<jupyter_code>names = ["Rebecca", "Ulrike", "Jenny", "Julien", "Karin", "Ilka", "Sebastian",
         "Elisabeth", 'Jakob', "Svenja", "Dominik", "Clara", "Alina", "Jasmin"]<jupyter_output><empty_output><jupyter_text>Here, we have assigned a list of *strings* to the *variable* `name`. In this, we assign a string to a variable:<jupyter_code>some_random_variable_name = "Some string"<jupyter_output><empty_output><jupyter_text>Strings and variables are similar in that you have to open and close them to avoid a `SyntaxError`:<jupyter_code>["Rebecca", "Ulrike",
"Rebecca<jupyter_output><empty_output><jupyter_text>Observe that the error message accompanying the `SyntaxError` clearly tells us what the problem is with our code: that we have not closed the bracket. If we *evaluate* the variable, it returns its content.<jupyter_code>names
some_random_variable_name<jupyter_output><empty_output><jupyter_text>If a variable has not been assigned, accessing it returns a `NameError`.<jupyter_code>some_undefined_variable<jupyter_output><empty_output><jupyter_text>Another, even simpler type are numbers, specifically integers.<jupyter_code>1<jupyter_output><empty_output><jupyter_text>With integers, we can do basic math.<jupyter_code>1 + 2<jupyter_output><empty_output><jupyter_text>Observe the imoortant difference between the string `"1"` and the number `1`!If we use operations like math on a type for which they are not defined, we get a `TypeError`.<jupyter_code>1 + "2"<jupyter_output><empty_output><jupyter_text>Lists can contain strings, numbers, a mix of the two, or any other type of object - including other lists!<jupyter_code>another_list = [1, "1", names, [1, 2, 3]]
another_list[-1]
another_list[0]<jupyter_output><empty_output><jupyter_text>We can access members of lists by *indexing into the list* via `[idx]`.This also works for strings.This is an *expression*. It *returns* the content of the list at the specific position.Note that Python starts counting at 0!<jupyter_code>names[0]<jupyter_output><empty_output><jupyter_text>If we want to count from the end, we use `-`.<jupyter_code>names[-1]<jupyter_output><empty_output><jupyter_text>Another basic and extremely versatile mechanism of Python are *List Comprehensions*.They are very natural language - like; "Do x for (each) x in some_list". We only skip the "Do", the "each" and wrap it in square brackets."for" and "in" are *control flow operators*. (There are only very few control flow operators in Python, and "for" and "in" are probably the most common ones.)<jupyter_code>[name for name in names]<jupyter_output><empty_output><jupyter_text>Note that what we specifically did here was: for each thing in names, we evaluate the thing and return the result. Lists can also contain numbers.<jupyter_code>positions = [3, 5, 7]<jupyter_output><empty_output><jupyter_text>We can use lists plus list comprehensions to retrieve only specific positions from a list:<jupyter_code>[names[position] for position in positions]<jupyter_output><empty_output><jupyter_text>This is of course equivalent to:<jupyter_code>[names[3], names[5], names[7]]<jupyter_output><empty_output><jupyter_text>Note that what happens in a list comprehension, stays in the list comprehensions. We have used `position` in the list comprehension, but it is not available to the outside world.<jupyter_code>position<jupyter_output><empty_output><jupyter_text>Another useful container type, somewhat similar to the list, is the *dictionary* or *dict*.Dicts contain of *key*, *value* pairs. We use the keys to access the values by using a key with square brackets.Dictionaries are initialized with curly brackets/`{}` and comma-separated key:value pairs.<jupyter_code>ages = {"Jona": 32, "Rebecca": 26, "Ulrike": 35, "Jennifer": 33}<jupyter_output><empty_output><jupyter_text>While we square brackets and positional indices inside of lists, we can use strings and curly brackets for *key-based* retrieval from dictionares.<jupyter_code>names[0]
ages["Rebecca"]<jupyter_output><empty_output><jupyter_text>And of course ...<jupyter_code>ages[names[0]]<jupyter_output><empty_output><jupyter_text>If a key is not in a dict, a `KeyError` is raised.<jupyter_code>ages["<NAME>"]<jupyter_output><empty_output><jupyter_text>Assignment to lists and dicts works in the same way as retrieval plus assignment to variables.<jupyter_code>ages["<NAME>"] = 101
ages
names[6]
names[6] = "Basti"
names[6]<jupyter_output><empty_output><jupyter_text>Things you should have learned- how to use the iPython notebook- lists, dicts, strings; as well as brackets and quotation marks- indexing into lists and accessing dicts via keys- KeyErrors- variables- conditions- NameErrors- the operators `for` and `in`- list comprehensionIt also wouldn't help to keep in mind the concepts- expression- assignment- Types  Homework * Construct a dictionary under the name "discrimination" with two keys: "old" and "young". Under "old", put a list of the names of     people currently in the dictionary "ages" who are older than 30, under "young", everyone younger than 30.<jupyter_code>old = ["...

discrimination =<jupyter_output><empty_output><jupyter_text>* cause an error: a `NameError`, `TypeError`, `SyntaxError` or `KeyError`<jupyter_code>...<jupyter_output><empty_output><jupyter_text>* Sum together (add up) the ages of the last person in the list of old people and the first person in the list of young person. Use the dictionary "discrimination".<jupyter_code>discrimination[<jupyter_output><empty_output><jupyter_text>* Using the dictionary `discrimination` and a list comprehension, create the list of the age of the first person in the list of young people and the first person in the list of old people.<jupyter_code>[discrimination[... for age in ...]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup