(1) python, pip installert (2) Variabler Python er object oriented, "objects" kan innholde fields (attributes) eller procedyrer (methods) Man trenger ikke å deklarere variabler og variabel typer Ex. my_int = 1 print(my_int) my_float = 1.5 print(my_float) my_string = "hi!" print(my_string) my_list = [1, 2, 3] print(my_list) print(my_list[1]) print(list("my name is Eric and Eric is my name".split())) ['my', 'name', 'is', 'Eric', 'and', 'Eric', 'is', 'my', 'name'] print(set("my name is Eric or Eric is my name".split())) set(['and', 'is', 'my', 'name', 'Eric']) liste er ordna, kan ha dublettar sett er alfabetisk ordna, du kan ikkje ha dublettar ("Hei, %s!" % names) %s betyr: bruk streng-variebelen som kjem etterpå a = 2 a = a + 1 a += 1 (3) String formattering names = "Trond og Lene" print("Hei, %s!" % names) name = Chiara age = 32 print("I dag blir %s %d år gammel." % (name, age)) (4) Operations +, -, *, /, %, ** a = 5 b = a % 2 print(b) c = a ** 3 print(c) % = modulo (gir rest av divisoren) (5) Andre operatører antall elementer i lista: len(my_list) name = "Chiara" print(len(name)) 6 my_list.append(4) my_string.startswith("h") my_string.endswith("!") my_sentence = "Hei Lene og Trond" my_string.split(" ") m = ["asdf", "bsdf", "csf"] m.append("ddfg") print(m) ['asdf', 'bsdf', 'csf', 'ddfg'] print(m[1].startswith("a")) False print(m[1].endswith("a")) False print(m[1].endswith("f")) True (6) Conditions if (condition): code to do something elif (another condition): code to do something else else: code to do another thing Ex. names = "Lene og Trond" if (names == "Lene"): print("Hei Lene!") elif (names == "Trond"): print("Hei Trond!") else: print("Hei Lene og Trond“) ! betyr if false if (names != "Lene"): print("Hei Lene!") list = ["Lene", "Trond", "Chiara"] if "Lene" in list: print("Hei Lene") (7) Loops For-loop # Prints out 1,2,3 my_list = [1, 2, 3] for number in my_list: print(number) - range function (zero-based) # Prints out 0,1,2,3,4 for x in range(5): print(x) # Prints out 3,4,5 for x in range(3, 6): print(x) # Prints out 3,5,7 for x in range(3, 8, 2): print(x) import random for x in range(10): print random.randint(1,101) print random.uniform(1.5, 1.9) While-loop # Prints out 0,1,2,3 count = 0 while count < 4: print(count) count += 1 - break (exit loop) and continue (skip current block and go back to loop-statement) # Prints out 0,1,2,3,4 count = 0 while True: print(count) count += 1 if count >= 5: break # Prints out only odd numbers - 1,3,5,7,9 for x in range(10): # Check if x is even if x % 2 == 0: continue print(x) (8) Functions def my_function(): print("Hello from my function!") my_function() def my_function(f): print("Hello to %s from my function!" % (f)) namn = "Lene" my_function(namn) def sum_two_numbers(a, b): return a + b x = sum_two_numbers(1,2) print(x) num1 = 1 num2 = 2 x = sum_two_numbers(num1, num2) print(x) x = sum_two_numbers(num1, num1) print(x) (9) Classes, think of them as templates to create objects Version1: class my_class: color = "red" def function(self): print("The color of this class is, %s!" % self.color) my_object1 = my_class() my_object2 = my_class() my_object2.color = "green" # Then print out both values my_object1.function() my_object2.function() Version2: class class_colors: global color1, color2, color3 color1 = ["red", "rosa", "blue", "black"] color2 = "green" color3 = "blue" def function_for(self): for item in color1: print("The color of this class is, %s!" % item) my_object1 = class_colors() my_object2 = class_colors() my_object2.color3 = "green" print((my_object1).function_for()) print((my_object1).function_for()) The color of this class is, red! The color of this class is, rosa! The color of this class is, blue! The color of this class is, black! None The color of this class is, red! The color of this class is, rosa! The color of this class is, blue! The color of this class is, black! None get None because print((my_object1).function_for()) prints what is returned in function_for() --> None