<jupyter_start><jupyter_text>HelloWorldThis notebook contains some code to allow you to check if everything runs as intended.[Jupyter notebooks](https://jupyter.org) contain cells of code or text written in [markdown](https://www.markdownguide.org/getting-started/). This cell contains markdown, you can edit it by double clicking on it. You can create new cells using the "+" (top right bar). You can run cells to execute the code of markdown they contain and see what happens. **For a more extended revision of these materials, see http://www.karsdorp.io/python-course (Lesson 1).**<jupyter_code># It is customary for your first program to print Hello World! This is how you do it in Python.

print("Hello World!")
# You can comment your code using '#'. What you write afterwards won't be interpreted as code.<jupyter_output><empty_output><jupyter_text>The code you write is executed from a certain *working directory* (we will see more when doing input/output). You can access your working directory by using a *package* (boundle of Python code which does something for you) part of the so-called Python standard library: `os` (a package to interact with the operating system).<jupyter_code>import os # we first import the package
os.getcwd() # we then can use some of its functionalities. In this case, we get the current working directory (cwd)<jupyter_output><empty_output><jupyter_text>You can check the Python version you are using with the `sys` package.<jupyter_code>import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)<jupyter_output>/anaconda3/bin/python
3.7.3 (default, Mar 27 2019, 16:54:48) 
[Clang 4.0.1 (tags/RELEASE_401/final)]
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)<jupyter_text>You can check the version of a package via its property `__version__`.<jupyter_code>import pandas as pd
pd.__version__<jupyter_output><empty_output><jupyter_text>The code below does some basic things using Python. We will discuss it live during our first class. Please check if you know what it does and, if not, you can still figure it out.  Variables and operations<jupyter_code>a = 2
b = a
b += 10
a
b
a == b
s = "Hello World!"
print(s)
s[-1]
s
list # names in green are reserved by Python: avoid using them as variable names
# variables have a certain data type. 
# Python is very flexible with allowing you to assign variables to data as you like (if you need a certain data type, you need to check it explicitly)

type(s)
type(2.3)
type("Hello")<jupyter_output><empty_output><jupyter_text>Exercise* Create two variables with integers* Calculate their power of two and multiply* Print their value as a string<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>String methods<jupyter_code># find

"Hello World".find("World")
# split

s.split(" ")
s.split() # splits on white space by default
# lower

s.lower()<jupyter_output><empty_output><jupyter_text>Data structures<jupyter_code>l = [1,2,3,5,6,7] # a list is a collection of objects in the order they are put in
d = {1:"one",2:"two",3:"three"} # a dictionary is composed of key:value pairs
l.append(4)
l
d.update({4:"four"})
d
l[2] # access by index
d[2] # access by key
# comprehensions

[x*2 for x in l]
# slicing
l[1:3]
# operations on lists have their own implementation

l*2
# arrays
import numpy as np

v = np.array(l)
v
# broadcasting

v*2
# length
len(l)
# delete an element by index
del l[2]
# by value
l.remove(1)<jupyter_output><empty_output><jupyter_text>Exercise* Create a dictionary with the name of some courses you have already completed at UvA and the academic year you have done them on* Add this course in 2020 (hint: use update)* Print the dictionary* Remove this course from the dictionary* Create a list with the name of the courses* Print the last two elements<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>Looping<jupyter_code># iterate through the elements of a list
for x in l:
    print(x/2) # note the indentation, Python's way to keep track of the scope of different pieces of code
# iterate keeping track of index position

for n,x in enumerate(l):
    if n < 1:
        print(x)
# iterate until a condition is met

n = 0

while n < len(l):
    print(l[n])
    n += 1
# sort the elements of a list

sorted(l, reverse=True)
# iterate through the elements of a dictionary

for k,v in d.items(): # use .keys() or .values() for just keys or values, respectively
    print(k,v)<jupyter_output>1 one
2 two
3 three
4 four<jupyter_text>Functions<jupyter_code># you can define pieces of code by name, to use them again

def power(x,p=2):
    return x**p
power(2)
power(2,3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09149072296865003, 'fraction_numerical': 0.01471529110684581, 'mean_word_length': 5.059431524547803, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '276955', 'license': 'MIT', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1460, 'n_words': 617, 'path': 'notebooks/0_HelloWorld.ipynb', 'repo_name': 'Giovanni1085/UvA_CDH_2020', 'size': '4689'}