tutorial 2 “rules and patterns”

1

Upload: others

Post on 28-Dec-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 2 “Rules and Patterns”

Tutorial 2 “Rules and Patterns”

Page 2: Tutorial 2 “Rules and Patterns”
Page 3: Tutorial 2 “Rules and Patterns”
Page 4: Tutorial 2 “Rules and Patterns”
Page 5: Tutorial 2 “Rules and Patterns”
Page 6: Tutorial 2 “Rules and Patterns”
Page 7: Tutorial 2 “Rules and Patterns”
Page 8: Tutorial 2 “Rules and Patterns”
Page 9: Tutorial 2 “Rules and Patterns”
Page 10: Tutorial 2 “Rules and Patterns”
Page 11: Tutorial 2 “Rules and Patterns”
Page 12: Tutorial 2 “Rules and Patterns”
Page 13: Tutorial 2 “Rules and Patterns”
Page 14: Tutorial 2 “Rules and Patterns”
Page 15: Tutorial 2 “Rules and Patterns”
Page 16: Tutorial 2 “Rules and Patterns”
Page 17: Tutorial 2 “Rules and Patterns”

#### QUICK SUMMARY OVERVIEW

#### VARIABLES#### Integer: Ganzzahl typecast: int(5.6789) -> Result: 5#### Float: Gleitkommazahl typecast: float(5) -> Result: 5.0#### String: Zeichenkette typecast: str(5.6789) -> Result: "5.6789"######## to find out the type of a variable: type(var)

#### LISTS#### defining lists:#### mylist = [ 1, 3, 2, 4, 7]#### kollegen=["andi", "richard", "urs", "gregor", "eszter", "fabian"]#### #### kollegen[0] / mylist[0] -> first element#### kollegen[1] / mylist[1] -> second element#### kollegen[-1] / mylist[-1] -> last element#### kollegen[0:4] / mylist[0:4] -> list with first through fourth (not fifth!) element#### kollegen[-3:] / mylist[-3:] -> list with all elements starting from third last######## NOTE: this syntax also works on Strings#### my_str = "hello python"#### my_str[0] -> "h"#### my_str[-3:] -> "hon"

#### LOOPS#### Repeat a block statement until condition is reached#### #### WHILE LOOP#### i=0 # variable needs to be set befor loop#### while (i < 10): # condition in parantheses is evaluated as true or false#### print i # block statement is indented#### i=i+1 # if variable is not incremented, loop will run forever!!!######## EQUIVALENT FOR LOOP (no initializing or explicit incrementing necessary!)#### for i in range(10):#### print i # block statement is indented######## for name in kollegen:#### print name

Page 18: Tutorial 2 “Rules and Patterns”
Page 19: Tutorial 2 “Rules and Patterns”