IB111 Úvod do programovaní skrze python 2/3. š Š Premenná Premenná ★ ž ★ ž ○ ★ č č ★ ★ ★ ď loop_count = 0 Premenná ★ ť loop_count = 0 print(loop_count) loop_count = 3 print(loop_count) loop_count = "Text" print(loop_count) 0 3 Text Dátový typ ★ ž ★ č ★ č ★ ★ ť ★ ★ ★ Dátové typy ★ ť type(1) type("1") type(1.0) Dátové typy ★ ť type(int("1")) type(str(1)) type(str(1.0)) type(int(1.7)) ??? Operátory Príklady a = 5 + 2 # 5+2 b = 5 - 2 # 5 -2 c = 5 * 2 # 5*2 d = 5.0 // 2#5.0//2 e = 5 / 2 # 5/2 f = 5 % 2 # 5%2 g = 5 ** 2 # 5**2 a = 7 b = 3 c = 10 d = 2 e = 2.5 f = 1 g = 25 Syntax sugar sum = 0 sum = sum + 1 sum = sum + 2 sum = sum + 3 sum = 0 sum += 1 sum += 2 sum += 3 Porovnávanie a = 4 == 4 b = 8 != 5 c = 9 < 4 d = 9 <= 4 e = 5 > 3 f = 5 >= 5 a = True b = True c = False d = False e = True f = True Logické spojky a = True and True b = True and False c = False and False d = True or True e = True or False f = False or False g = not True h = not False i = not not True a = True b = False c = False d = True e = True f = False g = False h = True i = True Kombinácia ★ ★ č ★ ★ … š ž ť "10" == str(2 * 5) and 5 ** 4 >= 200 Výstup Výpis print("Text") print(1) print("a" + "b") print("a", "b") print("This","won't","be",end=" ") print("ended with enter.") Formátovanie textu text = "Hello my name is {} and I come from {}.".format("Slavo", "Slovakia") print(text) Hello my name is Slavo and I come from Slovakia. Vetvenie Basic if ★ ★ žň ★ ○ ★ č ★ False == 0, 0.0, "", [], (), {}, None, ... if age >= 18: print ("You can legally buy alcohol.") If-else ★ ★ ★ ★ č if rating > 80: print("Should be good movie.") print("Rating:",rating) else: print("I wouldn't recommend it.") # continue... If-elif salary = 1000 if salary < 5000: tax = 0.05 elif salary < 20000: tax = 0.15 elif salary < 40000: tax = 0.30 else: tax = 0.50 Diagram Zdroj: http://www.programiz.com/python-programming/if-elif-else Cyklus Cyklus ★ ★ č č č ★ č ★ For ★ č č ★ č ★ ž for i in range(3): print(i) print("I will run just once.") For ★ č ★ č ★ ★ č ž ž for i in range(1,10,2): print(i, end=" ") # 1 3 5 7 9 While ★ ★ ž ť č ★ ť ★ ť ť i = 0 while i < 3: print(i) i += 1 for i in range(3): print(i) Kontrola vstupu choice = input('Like the course ? (y/n)') while choice != 'y' and choice != 'n': choice = input("Unexpected input. Enter again:") break i = 0 while True: print(i) i += 1 if i >= 3: break # Jump out of 'while' block print("All right. Off you go.") Continue i = 5 while i > 0: i -= 1 if i == 3: continue # Jump to condition print(i) print("All right. Off you go.") Procedúry Procedúry ★ ľ č ★ ★ ★ ○ ★ ★ ○ def procedure(param1, param2): do_something(param1) ... do_something(param2) Procedúry ★ ť ž ○ ★ ť ○ def greet(name): """ Greets someone :param name: name of the person to be greeted """ print(" Hello {} !".format(name)) Procedúry ★ č ž š ★ ★ greet("students") But why? ★ ž ť ť ž š š ★ ○ š ★ ž ľ ť ★ Ľ š č š ○ Default values def foo(x, y=3): print("x =", x) print("y =", y) foo(1) foo(2,5) x = 1 y = 3 x = 2 y = 5 Volanie menom def foo(x, y=3): print("x =", x) print("y =", y) foo(y=1, x=4) foo(x=5) print("x",end=" ") x = 4 y = 1 x = 5 y = 3 x Procedúry def p(a1, a2, a3): if a2 >= 0.5: print('Wow, that is bright!') if a3 >= 0.5: print("Wow, that is light!") p(170, 0.1, 0.6) Bill saw code like this... Procedúry def color_info(hue, saturation, luminosity): if saturation >= 0.5: print('Wow, that is bright!') if luminosity >= 0.5: print("Wow, that is light!") color_info(hue=170, saturation=0.1, luminosity=0.6) … and turn it into this. Be like Bill. Rozsahy ★ ★ ľ š t = Turtle() def move(): t.forward(100) Rozsahy ★ if price > 10: tmp = price price *= 0.8 print("Discount from: {} to {} ".format(str(tmp), str(price))) print("Print original price:", str(tmp)) #error Rozsahy var = "awesome" def shadow(): var = "boring" commend(var) if True: var = "interesting" commend(var) shadow() commend(var) Funkcie Funkcie ★ ★ ž ť š ž Zdroj: https://www.math.rutgers.edu/~greenfie/summer_135/diary.html Volanie funkcie print(greet("students")) def resolve_tax(salary): if salary < 5000: return 5 elif salary < 20000: return 15 elif salary < 40000: return 30 else: return 50 If-elif salary = 1000 if salary < 5000: tax = 0.05 elif salary < 20000: tax = 0.15 elif salary < 40000: tax = 0.30 else: tax = 0.50 Volanie funkcie paid_tax = salary * resolve_tax(salary) Volanie funkcie def divisibility5(n): return n % 5 == 0 a = divisibility5(1) b = divisibility5(20) c = divisibility5(4) if(divisibility5(5)): #... a = False b = True c = False Vstavané funkcie ★ print() ★ ž ○ ť ★ min() ★ max() Math ★ č ○ from math import * ★ ľ round(), ceil(), floor() ★ sin(), cos(),... ★ exp(), log(), sqrt(), ... Debug Úloha 1 ★ č č ○ ■ ■ ○ ■ ■ ★ factorial(5) = 120 ★ sum_of_numbers(100) = 5050 Úloha 2 ● č ľ č n ● divisor_count(18) = 6 ■ 1,2,3,6,9,18 Úloha 3 ● č č č ž ť š ● č ž ● ○ is_prime(7) = True ○ is_prime(1) = False ○ is_prime(4) = False Úloha 4 ● č ť ž ť č č ž ● n_primes(4) = 2 3 5 7 Úloha 5* ● čš č ľ ● gcd(7,33) = 1 ● gcd(5, 10)= 5 ● gcd(42, 36)= 6 Úloha 6* ● š č ● lcm(9,3) = 9 ● lcm(360,42) = 2520 ● š č ● factorization(25) = “5*5” ● factorization(42) = “2*3*7” Úloha 7* ● ● č ť ● Ď ○ ○ Zbierka Credits Special thanks to all the people who made and released these awesome resources for free: Presentation template by SlidesCarnival Photographs by Unsplash