├── lesson01 ├── ask1.py ├── ask2.py ├── hello.py ├── hello2.py ├── les01.pdf └── program.py ├── lesson02 ├── bool.py ├── boolean.py ├── brain_damage.py ├── bugs_data_types.py ├── comparison.py ├── conversions.py ├── exercise1.py ├── exercise10.py ├── exercise2.py ├── exercise3.py ├── exercise4.py ├── exercise5.py ├── exercise6.py ├── exercise7.py ├── exercise8.py ├── exercise9.py ├── float.py ├── float_operations.py ├── increase.py ├── integer.py ├── integer_operations.py ├── magic_pill.py ├── python02 .pdf ├── seconds.py ├── smart_message.py ├── string.py ├── string_concatenation.py ├── string_concatenation2.py └── string_concatenation3.py ├── lesson03 ├── adult.py ├── comparison.py ├── comparison2.py ├── exercise1.py ├── exercise10.py ├── exercise11.py ├── exercise2.py ├── exercise3.py ├── exercise4.py ├── exercise5.py ├── exercise6.py ├── exercise7.py ├── exercise8.py ├── exercise9.py ├── if.comparison.py ├── if.else.py ├── logical.py ├── nested_if.py ├── odd.even.py ├── python03.if.pdf └── short.hand.if.py ├── lesson04 ├── append.py ├── clear.py ├── exercise1.py ├── exercise2.py ├── exercise3.py ├── exercise4.py ├── exercise5.py ├── exercise6.py ├── exercise7.py ├── exercise8.py ├── extend.py ├── in.py ├── insert.py ├── list.element.py ├── list.slicing.py ├── lists.py ├── more.methods.py ├── pop.py ├── python04.lists.pdf ├── remove.py └── reverse.indexing.py ├── lesson05 ├── break.py ├── continue.py ├── else.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── exercise11.py ├── exercise12.py ├── for.list1.py ├── for.list2.py ├── for.range.py ├── for.string.py ├── nested.loops.py ├── nested.loops2.py ├── nested.loops3.py ├── python05.for.pdf ├── range.py ├── while1.py ├── while2.py ├── while3.py ├── while4.py ├── while5.py ├── while6.py └── while7.py ├── lesson06 ├── array2d.py ├── array2d.traverse.py ├── brain.damage.py ├── comprehensions.py ├── comprehensions2.py ├── comprehensions3.py ├── conversions.py ├── copy.lists.problem.py ├── copy.lists.py ├── exercise1.py ├── exercise2.py ├── exercise3.py ├── exercise4.py ├── exercise5.py ├── exercise6.py ├── exercise7.py ├── python06.tuples.lists.pdf ├── seq.conv.py ├── sequence.ops.py ├── tuples.py └── tuples2.py ├── lesson07 ├── exercise1.py ├── exercise2.py ├── exercise3.py ├── exercise4.py ├── exercise5.py ├── exercise6.py ├── no.lists.in.set.py ├── python07.sets.pdf ├── randrange.py ├── set.add.elements.py ├── set.comprehensions1.py ├── set.comprehensions2.py ├── set.comprehensions3.py ├── set.convert.delete.py ├── set.copy.py ├── set.operations.py └── sets.py ├── lesson08 ├── dict.add.py ├── dict.comprehensions.py ├── dict.convert.delete.py ├── dict.copy.py ├── dict.definition.py ├── dict.example.py ├── dict.if.py ├── dict.loop.py ├── dict.manipulation.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.initial.py ├── exercise06.py ├── exercise07.initial.py ├── exercise07.py ├── exercise08.initial.py ├── exercise08.py ├── exercise09.04.py ├── exercise09.py └── python08.dictionaries.pdf ├── lesson09 ├── exercise01.py ├── exercise02.initial.py ├── exercise02.py ├── exercise03.py ├── exercise04.initial.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.initial.py ├── exercise07.py ├── exercise08.initial.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── fstrings.py ├── python.encoding.py ├── python.encoding128.py ├── python09.strings.pdf ├── sting.character.checking.py ├── string.as.sequence.py ├── string.convert.py ├── string.definition.py ├── string.escape.characters.py ├── string.format.c.py ├── string.format.py ├── string.methods.modify.py ├── string.operators.py ├── string.partitioning.py ├── string.searching.py ├── string.tab.length.py ├── string.trimming.justifying.py └── string.upper.lower.py ├── lesson10 ├── compound.py ├── data.types.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise07.py ├── exercise08.py ├── frozen.set.py ├── id.py ├── id2.py ├── id3.py ├── id4.py ├── id5.py └── python10.memory.pdf ├── lesson11 ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.1.py ├── exercise10.2.py ├── exercise10.3.py ├── exercise11.py ├── exercise12.py ├── exercise12_initial.py ├── exercise13.py ├── exercise14.py ├── function.definition.py ├── global.vs.local.py ├── global1.py ├── global2.py ├── immutable.arguments.py ├── local1.py ├── local2.py ├── main.py ├── mutable.arguments.assignment.py ├── mutable.arguments.py ├── none.py ├── parameters.py ├── python11.functions.pdf ├── return.value.py └── return_values.py ├── lesson12 ├── exercise01.py ├── exercise02.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── exercise11.py ├── exercise12.py ├── factorial.py ├── factorial_print.py ├── fibonacci.print.py ├── fibonacci.py ├── func.call.py ├── python12.recursion.pdf └── swap.py ├── lesson13 ├── args.py ├── default.value.py ├── default.values.keyword.arguments.py ├── default.values.order.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── keyword.arguments.py ├── kwargs.py ├── positional.after.keyword.args.py └── python13.functions.more.pdf ├── lesson14 ├── built.in.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── fibonacci.py ├── function.data.type.py ├── function.factory.py ├── import1.py ├── import2.py ├── import3.py ├── inner.function.py ├── lesson14.exercise05 │ ├── exercise05.py │ ├── mod_pupils.py │ └── mod_teachers.py ├── module.py ├── python14.modules.scope.pdf └── scope.py ├── lesson15 ├── binary.dat ├── binary.file.py ├── bytes.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.admin.py ├── exercise09.user.py ├── exercise10.py ├── exercise11.py ├── file.open.close.py ├── for.readlines.py ├── json.dump.py ├── json.load.py ├── json.multiple.data.py ├── lear.txt ├── lear2.txt ├── lesson15.exercise12 │ ├── exercise12.py │ ├── mod_pupils.py │ ├── mod_teachers.py │ ├── pupils.json │ └── teachers.json ├── numbers.json ├── numbers.txt ├── overwrite.txt ├── python15.files.pdf ├── random.access.py ├── random.json.data.json ├── read.py ├── readlines.py ├── reminders.json ├── test.txt ├── test2.txt ├── test3.txt ├── test4.txt ├── users.json └── with.py ├── lesson16 ├── building.py ├── class.attribute.py ├── class.members.py ├── class.methods.py ├── del.py ├── empty.class.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── exercise11.py ├── exercise12.py ├── exercise13.py ├── is.py ├── lesson16.exercise14 │ ├── arena.py │ ├── character.py │ └── main.py ├── lesson16.exercise15 │ ├── exercise15.py │ ├── mod_pupils.py │ ├── pupils.json │ ├── teacher.py │ ├── teachers.json │ └── teachers.py ├── private.py ├── public.py ├── python16.oop.classes.pdf └── stack.py ├── lesson17 ├── binary.py ├── comparison.py ├── comparison2.py ├── comparison3.py ├── del.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.initial.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── exercise11.py ├── exercise12.py ├── function.py ├── iterate.py ├── lesson17.exercise13 │ ├── arena.py │ ├── character.py │ ├── equipment.py │ └── main.py ├── lesson17.exercise14 │ ├── exercise14.py │ ├── pupil.py │ ├── pupils.json │ ├── pupils.py │ ├── teacher.py │ ├── teachers.json │ └── teachers.py ├── new.py ├── python17.oop.methods.pdf ├── queue.py ├── repr.py └── str.py ├── lesson18 ├── abstract.class.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── exercise05.py ├── exercise06.py ├── exercise07.py ├── exercise08.py ├── exercise09.py ├── exercise10.py ├── exercise11.py ├── inheritance.py ├── interface.py ├── lesson18.exercise12 │ ├── arena.py │ ├── character.py │ ├── equipment.py │ ├── mage.py │ ├── main.py │ └── tank.py ├── linked_list.py ├── method.overriding.py ├── mixin.py ├── mixing,methods.py ├── mro.py ├── multiple.inheritance.py ├── protected.py ├── python18.oop.inheritance.pdf ├── super.mro.py ├── super.py └── temp.py ├── lesson19 ├── else.finally.py ├── error.py ├── exercise01.py ├── exercise02.py ├── exercise03.py ├── exercise04.py ├── lesson19.exercise05 │ ├── exercise05.py │ ├── lesson.py │ ├── lessons.json │ ├── lessons.py │ ├── pupil.py │ ├── pupils.json │ ├── pupils.py │ ├── teacher.py │ ├── teachers.json │ └── teachers.py ├── python19.exceptions.pdf ├── raise.py ├── tree.py ├── try.except.py └── user.exception.py └── lesson20 ├── args.py ├── assert.py ├── assert2.py ├── comment.ret.value.py ├── documentation.py ├── exercise01.py ├── exercise02.py ├── graph.py ├── lesson20.exercise02 ├── exercise03.py ├── lesson.py ├── lessons.json ├── lessons.py ├── pupil.py ├── pupils.json ├── pupils.py ├── teacher.py ├── teachers.json └── teachers.py ├── python20.testing.pdf ├── queue.py ├── teacher.py ├── temp.py ├── test_args.py ├── test_teacher.py └── waiter.py /lesson01/ask1.py: -------------------------------------------------------------------------------- 1 | print("Hello World!") 2 | print("How are you?") -------------------------------------------------------------------------------- /lesson01/ask2.py: -------------------------------------------------------------------------------- 1 | pirnt("Hello World!") -------------------------------------------------------------------------------- /lesson01/hello.py: -------------------------------------------------------------------------------- 1 | print("Hello World!") -------------------------------------------------------------------------------- /lesson01/hello2.py: -------------------------------------------------------------------------------- 1 | print('Hello World!') -------------------------------------------------------------------------------- /lesson01/les01.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson01/les01.pdf -------------------------------------------------------------------------------- /lesson01/program.py: -------------------------------------------------------------------------------- 1 | print("Hello!") 2 | print("how are you?") 3 | print("I am fine!") -------------------------------------------------------------------------------- /lesson02/bool.py: -------------------------------------------------------------------------------- 1 | x = True 2 | print(x) 3 | x = False 4 | print(x) 5 | -------------------------------------------------------------------------------- /lesson02/boolean.py: -------------------------------------------------------------------------------- 1 | is_pretty = True 2 | print(is_pretty) 3 | -------------------------------------------------------------------------------- /lesson02/brain_damage.py: -------------------------------------------------------------------------------- 1 | print(str(int(5.12)+4)+"!") 2 | -------------------------------------------------------------------------------- /lesson02/bugs_data_types.py: -------------------------------------------------------------------------------- 1 | an_int = 5 2 | a_float = 3.14 3 | print(an_int + a_float) 4 | a_string = "Hello!" 5 | print(a_string + an_int) 6 | -------------------------------------------------------------------------------- /lesson02/comparison.py: -------------------------------------------------------------------------------- 1 | x = 3 < 5 2 | print(x) 3 | 4 | x = 1 < 0 5 | print(x) 6 | -------------------------------------------------------------------------------- /lesson02/conversions.py: -------------------------------------------------------------------------------- 1 | a_float = 3.84 2 | to_int = int(a_float) 3 | print(to_int) 4 | to_str = str(a_float) 5 | print(to_str) 6 | -------------------------------------------------------------------------------- /lesson02/exercise1.py: -------------------------------------------------------------------------------- 1 | print(1+2) 2 | print(5-2) 3 | print(1*3) 4 | print(9/3) 5 | print(3**1) 6 | print(11%4) -------------------------------------------------------------------------------- /lesson02/exercise10.py: -------------------------------------------------------------------------------- 1 | hours = int(input("Dwse wres: ")) 2 | minutes = int(input("Dwse lepta: ")) 3 | seconds = int(input("Dwse deuterolepta: ")) 4 | 5 | message = str(hours) + ":" + str(minutes) + ":" + str(seconds) 6 | print(message) -------------------------------------------------------------------------------- /lesson02/exercise2.py: -------------------------------------------------------------------------------- 1 | print(1+2*3) 2 | print(3/2*7) 3 | print(3**2*7/2) 4 | print(3-2*7+2) 5 | print(3-2*(7+2)) 6 | -------------------------------------------------------------------------------- /lesson02/exercise3.py: -------------------------------------------------------------------------------- 1 | poso = 100 2 | epitokio = 0.01 3 | poso += poso*epitokio 4 | print(poso) -------------------------------------------------------------------------------- /lesson02/exercise4.py: -------------------------------------------------------------------------------- 1 | hours = 10 2 | minutes = 50 3 | seconds = 15 4 | 5 | total_seconds = seconds 6 | total_seconds += minutes*60 7 | total_seconds += hours*3600 8 | 9 | print(total_seconds) -------------------------------------------------------------------------------- /lesson02/exercise5.py: -------------------------------------------------------------------------------- 1 | message = 'Ο Βασίλης λεβέντης είπε "Πηγαίνετε να βοσκήσετε προβατάκια"' 2 | print(message) -------------------------------------------------------------------------------- /lesson02/exercise6.py: -------------------------------------------------------------------------------- 1 | full_name = "John Doe" 2 | 3 | message = "Hello " + full_name + "!" 4 | print(message) -------------------------------------------------------------------------------- /lesson02/exercise7.py: -------------------------------------------------------------------------------- 1 | an_int = 5 2 | a_float = 10.0 3 | a_string = "message" 4 | a_bool = True 5 | 6 | print(an_int + a_float) 7 | print(str(an_int) + a_string) 8 | print(an_int + a_bool) 9 | print(str(a_float) + a_string) 10 | print(a_float + a_bool) 11 | print(str(a_bool) + a_string) -------------------------------------------------------------------------------- /lesson02/exercise8.py: -------------------------------------------------------------------------------- 1 | an_int = 5 2 | print(type(an_int)) 3 | 4 | a_float = 3.14 5 | print(type(a_float)) 6 | 7 | a_string = "This is " 8 | print(type(a_string)) 9 | 10 | a_bool = True 11 | print(type(a_bool)) -------------------------------------------------------------------------------- /lesson02/exercise9.py: -------------------------------------------------------------------------------- 1 | radius = float(input("Dwse tin aktina: ")) 2 | PI = 3.14 3 | 4 | circumference = 2*PI*radius 5 | print(circumference) 6 | 7 | area = PI * radius ** 2 8 | print(area) -------------------------------------------------------------------------------- /lesson02/float.py: -------------------------------------------------------------------------------- 1 | number1 = 1.05 2 | number2 = 4.99 3 | result = number1 + number2 4 | print(result) 5 | -------------------------------------------------------------------------------- /lesson02/float_operations.py: -------------------------------------------------------------------------------- 1 | x = 3.0 2 | print(x) 3 | y = 6.0 4 | print(y) 5 | x = x ** y 6 | print(x) 7 | -------------------------------------------------------------------------------- /lesson02/increase.py: -------------------------------------------------------------------------------- 1 | x = 3.1 2 | print(x) 3 | x += 0.2 4 | print(x) 5 | -------------------------------------------------------------------------------- /lesson02/integer.py: -------------------------------------------------------------------------------- 1 | number = 5 2 | print(number) 3 | -------------------------------------------------------------------------------- /lesson02/integer_operations.py: -------------------------------------------------------------------------------- 1 | x = 3 + 5 2 | print(x) 3 | y = 4 - x 4 | print(y) 5 | z = x*y 6 | print(z) 7 | div1 = 5/3 8 | print(div1) 9 | div2 = 5//3 10 | print(div2) 11 | mod = 5%3 12 | print(mod) 13 | power = 5**251 14 | print(power) -------------------------------------------------------------------------------- /lesson02/magic_pill.py: -------------------------------------------------------------------------------- 1 | name = input("Type your name: ") 2 | surname = input("Type your surname: ") 3 | age = int(input("Type your age: ")) 4 | magic_pill = 10 5 | age -= magic_pill 6 | message_name = "Hello " + name + " " + surname 7 | message_age = ". You are " + str(age) + " years old!" 8 | message = message_name + message_age 9 | print(message) 10 | -------------------------------------------------------------------------------- /lesson02/python02 .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson02/python02 .pdf -------------------------------------------------------------------------------- /lesson02/seconds.py: -------------------------------------------------------------------------------- 1 | seconds = 156789 2 | 3 | hours = seconds//3600 4 | print(hours) 5 | 6 | seconds = seconds % 3600 7 | minutes = seconds//60 8 | print(minutes) 9 | 10 | seconds = seconds % 60 11 | print(seconds) 12 | 13 | -------------------------------------------------------------------------------- /lesson02/smart_message.py: -------------------------------------------------------------------------------- 1 | age = 19 2 | name = "John Doe" 3 | message = name + " is " + str(age) + " years old." 4 | print(message) 5 | -------------------------------------------------------------------------------- /lesson02/string.py: -------------------------------------------------------------------------------- 1 | my_message = "Hello World" 2 | print(my_message) 3 | message2 = "How are you?" 4 | print(message2) 5 | -------------------------------------------------------------------------------- /lesson02/string_concatenation.py: -------------------------------------------------------------------------------- 1 | name = "Alan" 2 | surname = "Turing" 3 | full_name = name + surname 4 | print(full_name) 5 | -------------------------------------------------------------------------------- /lesson02/string_concatenation2.py: -------------------------------------------------------------------------------- 1 | full_name = "Alan" + " Turing" 2 | print(full_name) 3 | -------------------------------------------------------------------------------- /lesson02/string_concatenation3.py: -------------------------------------------------------------------------------- 1 | name = "Alan" 2 | surname = "Turing" 3 | full_name = name + " " + surname 4 | print(full_name) 5 | -------------------------------------------------------------------------------- /lesson03/adult.py: -------------------------------------------------------------------------------- 1 | age = 17 2 | 3 | if age < 18: 4 | print("underage") 5 | else: 6 | print("adult") 7 | -------------------------------------------------------------------------------- /lesson03/comparison.py: -------------------------------------------------------------------------------- 1 | print(8+1 == 9) 2 | print(6*2 != 7-3) 3 | print(4%2 >= 5/4) 4 | print(6-5 < 1%3) 5 | 6 | print("abc" < "def") 7 | print("abc" < "abcd") 8 | print("abc" < "ABC") -------------------------------------------------------------------------------- /lesson03/comparison2.py: -------------------------------------------------------------------------------- 1 | a = 3 2 | b = 4 3 | 4 | if a > b: 5 | print("a>b") 6 | elif a == b: 7 | print("a==b") 8 | else: 9 | print("a 9 and 11 != 5/2) 7 | -------------------------------------------------------------------------------- /lesson03/exercise10.py: -------------------------------------------------------------------------------- 1 | fruit = input("Dwse frouto: ") 2 | 3 | if fruit == "orange": 4 | print("iperoxo!") 5 | elif fruit == "apple": 6 | print("apaisio!!") 7 | else: 8 | print("ikanopoiitiko") -------------------------------------------------------------------------------- /lesson03/exercise11.py: -------------------------------------------------------------------------------- 1 | a = int(input("Dwse a: ")) 2 | b = int(input("Dwse b: ")) 3 | 4 | if a != 0: 5 | res = -b/a 6 | print("H lysh einai: " + str(res)) 7 | else: 8 | print("Adynath") -------------------------------------------------------------------------------- /lesson03/exercise2.py: -------------------------------------------------------------------------------- 1 | is_geek = True 2 | computer_hours = 8 3 | sport_hours = 1 4 | geek_factor = computer_hours > 5 and sport_hours < 2 and not is_geek 5 | print(geek_factor) 6 | -------------------------------------------------------------------------------- /lesson03/exercise3.py: -------------------------------------------------------------------------------- 1 | x = int(input("Give x: ")) 2 | y = int(input("Give y: ")) 3 | 4 | if x == y: 5 | print(str(x) + "==" + str(y)) 6 | 7 | if x != y: 8 | print(str(x) + "!=" + str(y)) 9 | 10 | if x < y: 11 | print(str(x) + "<" + str(y)) 12 | 13 | if x <= y: 14 | print(str(x) + "<=" + str(y)) 15 | 16 | if x > y: 17 | print(str(x) + ">" + str(y)) 18 | 19 | if x >= y: 20 | print(str(x) + ">=" + str(y)) 21 | 22 | -------------------------------------------------------------------------------- /lesson03/exercise4.py: -------------------------------------------------------------------------------- 1 | x = int(input("Dwse x: ")) 2 | y = int(input("Dwse y: ")) 3 | 4 | if x >= y: 5 | print(x) 6 | else: 7 | print(y) 8 | -------------------------------------------------------------------------------- /lesson03/exercise5.py: -------------------------------------------------------------------------------- 1 | x = int(input("Dwse x: ")) 2 | y = int(input("Dwse y: ")) 3 | z = int(input("Dwse z: ")) 4 | 5 | if x >= y and x >= z: 6 | print(x) 7 | 8 | if y >= x and y >= z: 9 | print(y) 10 | 11 | if z >= x and z >= y: 12 | print(z) 13 | 14 | -------------------------------------------------------------------------------- /lesson03/exercise6.py: -------------------------------------------------------------------------------- 1 | x = int(input("Dwse x: ")) 2 | 3 | maximum = x 4 | 5 | y = int(input("Dwse y: ")) 6 | 7 | if y > maximum: 8 | maximum = y 9 | 10 | z = int(input("Dwse z: ")) 11 | 12 | if z > maximum: 13 | maximum = z 14 | 15 | print(maximum) 16 | -------------------------------------------------------------------------------- /lesson03/exercise7.py: -------------------------------------------------------------------------------- 1 | age = int(input("Give age: ")) 2 | 3 | if age<18: 4 | print("anilikos") 5 | elif age>=18 and age<=65: 6 | print("enilikos") 7 | else: 8 | print("sintaksiouxos") -------------------------------------------------------------------------------- /lesson03/exercise8.py: -------------------------------------------------------------------------------- 1 | p11 = 3 2 | p12 = 6 3 | p21 = 4 4 | p22 = 5 5 | 6 | if p11+p12 < p21+p22: 7 | print("O 2os paiktis paizei prwtos") 8 | elif p11+p12 == p21+p22: 9 | print("Ksanarikste ta zaria. Isopalia!") 10 | else: 11 | print("O 1os paiktis paizei prwtos") -------------------------------------------------------------------------------- /lesson03/exercise9.py: -------------------------------------------------------------------------------- 1 | hours = int(input("Dwse wres: ")) 2 | minutes = int(input("Dwse lepta: ")) 3 | seconds = int(input("Dwse deuterolepta: ")) 4 | 5 | if hours < 10: 6 | message1 = "0" + str(hours) 7 | else: 8 | message1 = str(hours) 9 | 10 | if minutes < 10: 11 | message2 = "0" + str(minutes) 12 | else: 13 | message2 = str(minutes) 14 | 15 | if seconds < 10: 16 | message3 = "0" + str(seconds) 17 | else: 18 | message3 = str(seconds) 19 | 20 | 21 | message = message1 + ":" + message2 + ":" + message3 22 | print(message) -------------------------------------------------------------------------------- /lesson03/if.comparison.py: -------------------------------------------------------------------------------- 1 | sea_wave_height = 0.20 2 | print("I am going to the beach") 3 | 4 | if sea_wave_height <= 0.50: 5 | print("I will swim") 6 | 7 | print("I will sunbath") 8 | 9 | 10 | -------------------------------------------------------------------------------- /lesson03/if.else.py: -------------------------------------------------------------------------------- 1 | age = 19 2 | 3 | if age < 18: 4 | print("underage") 5 | else: 6 | print("adult") 7 | -------------------------------------------------------------------------------- /lesson03/logical.py: -------------------------------------------------------------------------------- 1 | spirit = True 2 | flesh = False 3 | print(spirit and flesh) 4 | print(spirit or flesh) 5 | print(not spirit) 6 | print(spirit and not flesh) 7 | 8 | print("-------------") 9 | print(True and True) 10 | print(True and False) 11 | print(False and True) 12 | print(False and False) 13 | print("-------------") 14 | print(True or True) 15 | print(True or False) 16 | print(False or True) 17 | print(False or False) 18 | print("-------------") 19 | print(not True) 20 | print(not False) -------------------------------------------------------------------------------- /lesson03/nested_if.py: -------------------------------------------------------------------------------- 1 | day = "Sunday" 2 | tired = True 3 | 4 | if day == "Saturday": 5 | print("I read a bit") 6 | elif day == "Sunday": 7 | if tired: 8 | print("I won't study at all") 9 | else: 10 | print("I will study a lot") 11 | else: 12 | print("I will study") 13 | 14 | -------------------------------------------------------------------------------- /lesson03/odd.even.py: -------------------------------------------------------------------------------- 1 | x = 4 2 | 3 | if x % 2 == 0: 4 | print("even") 5 | else: 6 | print("odd") -------------------------------------------------------------------------------- /lesson03/python03.if.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson03/python03.if.pdf -------------------------------------------------------------------------------- /lesson03/short.hand.if.py: -------------------------------------------------------------------------------- 1 | number = 5 2 | 3 | result = "odd" if number % 2 == 1 else "even" 4 | 5 | print(result) 6 | -------------------------------------------------------------------------------- /lesson04/append.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | my_list.append(4) 3 | print(my_list) -------------------------------------------------------------------------------- /lesson04/clear.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | my_list.clear() 3 | print(my_list) 4 | -------------------------------------------------------------------------------- /lesson04/exercise1.py: -------------------------------------------------------------------------------- 1 | friends = ["Tom", "Bob", "Pat"] 2 | print(friends) 3 | friends[0] = "Jim" 4 | print(friends) 5 | print(friends[0]) 6 | print(friends[1]) 7 | print(friends[2]) -------------------------------------------------------------------------------- /lesson04/exercise2.py: -------------------------------------------------------------------------------- 1 | numbers = [2, 1, 0] 2 | 3 | maximum = numbers[0] 4 | 5 | if numbers[1] > maximum: 6 | maximum = numbers[1] 7 | 8 | if numbers[2] > maximum: 9 | maximum = numbers[2] 10 | 11 | print(maximum) -------------------------------------------------------------------------------- /lesson04/exercise3.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2 | 3 | print(my_list[3]) 4 | print(my_list[4]) 5 | print(my_list[5]) 6 | print(my_list[6]) 7 | 8 | print(my_list[-4]) 9 | print(my_list[-5]) 10 | print(my_list[-6]) 11 | print(my_list[-7]) 12 | 13 | print(my_list[3:7]) 14 | 15 | 16 | -------------------------------------------------------------------------------- /lesson04/exercise4.py: -------------------------------------------------------------------------------- 1 | results = [55, 28, 20] 2 | 3 | temp = results[:2] 4 | 5 | average = (temp[0]+temp[1])/2 6 | 7 | print(average) 8 | -------------------------------------------------------------------------------- /lesson04/exercise5.py: -------------------------------------------------------------------------------- 1 | cash_desk = [] 2 | 3 | cash_desk.append("Tom") 4 | cash_desk.append("Bob") 5 | print(cash_desk) 6 | 7 | person = cash_desk.pop(0) 8 | print(person + " served.") 9 | 10 | cash_desk.append("Pam") 11 | cash_desk.append("Jim") 12 | print(cash_desk) 13 | 14 | person = cash_desk.pop(0) 15 | print(person + " served.") 16 | print(cash_desk) -------------------------------------------------------------------------------- /lesson04/exercise6.py: -------------------------------------------------------------------------------- 1 | stack = [] 2 | 3 | stack.append("Bob") 4 | stack.append("John") 5 | print(stack) 6 | 7 | person = stack.pop() 8 | print(person + " served. ") 9 | 10 | stack.append("Pat") 11 | print(stack) -------------------------------------------------------------------------------- /lesson04/exercise7.py: -------------------------------------------------------------------------------- 1 | my_list = [] 2 | 3 | number = float(input("Dwse 1o arithmo: ")) 4 | my_list.append(number) 5 | 6 | number = float(input("Dwse 2o arithmo: ")) 7 | my_list.append(number) 8 | 9 | number = float(input("Dwse 3o arithmo: ")) 10 | my_list.append(number) 11 | 12 | my_list.sort() 13 | 14 | print(my_list) 15 | -------------------------------------------------------------------------------- /lesson04/exercise8.py: -------------------------------------------------------------------------------- 1 | favorite_movies = ["Movie 1", "Movie 2", "Movie 3", "Movie 4"] 2 | 3 | new_movie = input("Dwse kainourgia tainia: ") 4 | 5 | if new_movie in favorite_movies: 6 | print("Yparxei hdh. Den egine apothikeusi") 7 | else: 8 | favorite_movies.append(new_movie) 9 | favorite_movies.sort() 10 | print(favorite_movies) 11 | print(len(favorite_movies)) 12 | -------------------------------------------------------------------------------- /lesson04/extend.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | my_list.extend([5, 8]) 3 | print(my_list) 4 | -------------------------------------------------------------------------------- /lesson04/in.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | 3 | if 2 in my_list: 4 | print("exists") 5 | else: 6 | print("not exists") -------------------------------------------------------------------------------- /lesson04/insert.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | my_list.insert(1, 4) 3 | print(my_list) -------------------------------------------------------------------------------- /lesson04/list.element.py: -------------------------------------------------------------------------------- 1 | my_grades = [5, 8, 6, 7] 2 | print(my_grades) 3 | my_grades[1] = 9 4 | print(my_grades) 5 | print(my_grades[0]) 6 | print(my_grades[3]) 7 | -------------------------------------------------------------------------------- /lesson04/list.slicing.py: -------------------------------------------------------------------------------- 1 | my_list = ["a", "b", "c", "d", "e"] 2 | 3 | my_new_list = my_list[1:4] 4 | print(my_new_list) 5 | 6 | l2 = my_list[1:] 7 | print(l2) 8 | 9 | l3 = my_list[:3] 10 | print(l3) 11 | 12 | l4 = my_list[:] 13 | print(l4) 14 | 15 | my_newest_list = my_list[:] 16 | print(my_newest_list) -------------------------------------------------------------------------------- /lesson04/lists.py: -------------------------------------------------------------------------------- 1 | list_int = [1, 3, 4] 2 | list_float = [3.12, 5.11, 1.1] 3 | list_string = ["My", "name", "is"] 4 | list_collection = [1, "big", [1, 2]] 5 | 6 | print(list_collection) 7 | print(type(list_int)) 8 | -------------------------------------------------------------------------------- /lesson04/more.methods.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 5] 2 | print(my_list) 3 | my_list.reverse() 4 | print(my_list) 5 | my_list.sort() 6 | print(my_list) 7 | print(len(my_list)) -------------------------------------------------------------------------------- /lesson04/pop.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | last = my_list.pop() 3 | print(last) 4 | print(my_list) 5 | my_list.pop(0) 6 | print(my_list) 7 | -------------------------------------------------------------------------------- /lesson04/python04.lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson04/python04.lists.pdf -------------------------------------------------------------------------------- /lesson04/remove.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3, 2, 4] 2 | my_list.remove(2) 3 | print(my_list) 4 | -------------------------------------------------------------------------------- /lesson04/reverse.indexing.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 3, 5] 2 | print(my_list[-1]) 3 | 4 | my_list[-2] = 6 5 | print(my_list) 6 | 7 | my_list[-3] = 4 8 | print(my_list) 9 | -------------------------------------------------------------------------------- /lesson05/break.py: -------------------------------------------------------------------------------- 1 | numbers = [1, 8, 7, 4, 11, 12, 2, 9, 2, 5] 2 | search = 1231 3 | for number in numbers: 4 | if search == number: 5 | print("Found it!") 6 | break 7 | -------------------------------------------------------------------------------- /lesson05/continue.py: -------------------------------------------------------------------------------- 1 | for number in range(0,10): 2 | if number % 2 == 1: 3 | continue 4 | print(number) -------------------------------------------------------------------------------- /lesson05/else.py: -------------------------------------------------------------------------------- 1 | for number in range(0,10): 2 | print(number) 3 | else: 4 | print("finished!") -------------------------------------------------------------------------------- /lesson05/exercise01.py: -------------------------------------------------------------------------------- 1 | user_input = input("Give a number: ") 2 | while user_input != "quit": 3 | print(int(user_input) ** 2) 4 | user_input = input("Give a number: ") -------------------------------------------------------------------------------- /lesson05/exercise02.py: -------------------------------------------------------------------------------- 1 | cnt = 1 2 | s = 0 3 | while cnt <= 10: 4 | user_input = int(input("Give " + str(cnt) + "th number: ")) 5 | cnt += 1 6 | s += user_input 7 | 8 | print("sum=" + str(s)) 9 | -------------------------------------------------------------------------------- /lesson05/exercise03.py: -------------------------------------------------------------------------------- 1 | numbers = [1,2,3,4,5,6,7,8,9,10] 2 | cnt = 0 3 | s = 0 4 | while cnt < len(numbers): 5 | s += numbers[cnt] 6 | cnt += 1 7 | 8 | 9 | print("sum=" + str(s)) 10 | -------------------------------------------------------------------------------- /lesson05/exercise04.py: -------------------------------------------------------------------------------- 1 | numbers = [21,2,3,14,5,6,7,8,9,10] 2 | cnt = 1 3 | maximum = numbers[0] 4 | while cnt < len(numbers): 5 | if numbers[cnt] > maximum: 6 | maximum = numbers[cnt] 7 | cnt += 1 8 | 9 | print(maximum) 10 | 11 | -------------------------------------------------------------------------------- /lesson05/exercise05.py: -------------------------------------------------------------------------------- 1 | for number in range(10,20+1,2): 2 | print(number) 3 | 4 | print("===========") 5 | 6 | for number in range(19,11-1,-2): 7 | print(number) 8 | 9 | print("===========") 10 | 11 | for number in range(1,29+1,2): 12 | if number % 3 == 0: 13 | print(number) -------------------------------------------------------------------------------- /lesson05/exercise06.py: -------------------------------------------------------------------------------- 1 | cities = ["New York", "London", "Chania", "Rethymno", "Tokyo"] 2 | 3 | for index in range(0,len(cities),2): 4 | print(cities[index]) 5 | -------------------------------------------------------------------------------- /lesson05/exercise07.py: -------------------------------------------------------------------------------- 1 | numbers = [] 2 | 3 | for number in range(0,1000+1,2): 4 | numbers.append(number) 5 | 6 | print(numbers) -------------------------------------------------------------------------------- /lesson05/exercise08.py: -------------------------------------------------------------------------------- 1 | N = int(input("Give N: ")) 2 | while N<3 or N>20: 3 | N = int(input("Give N(3-20): ")) 4 | 5 | numbers = [] 6 | for cnt in range(0,N): 7 | numbers.append(int(input("Give " + str(cnt+1) + "th number: "))) 8 | 9 | numbers.sort() 10 | 11 | print(numbers) 12 | -------------------------------------------------------------------------------- /lesson05/exercise09.py: -------------------------------------------------------------------------------- 1 | hidden = 43 2 | 3 | cnt = 0 4 | max_guesses = 10 + 1 5 | guess = int(input("Give a number: ")) 6 | 7 | while True: 8 | cnt += 1 9 | if cnt == max_guesses: 10 | print("You lose!") 11 | break 12 | 13 | if guess > hidden: 14 | print("It's smaller! ") 15 | elif guess < hidden: 16 | print("It's bigger! ") 17 | else: 18 | print("You've found it!") 19 | break 20 | 21 | guess = int(input("Give a number: ")) 22 | 23 | -------------------------------------------------------------------------------- /lesson05/exercise10.py: -------------------------------------------------------------------------------- 1 | my_friends = ["a", "b", "c"] 2 | guests = ["1", "2", "a", "4", "5", "6", "b", "8", "9", "10"] 3 | 4 | cnt = 0 5 | for friend in my_friends: 6 | if friend in guests: 7 | cnt += 1 8 | 9 | if cnt < 2: 10 | print("I won't come") 11 | else: 12 | print("I'll come") -------------------------------------------------------------------------------- /lesson05/exercise11.py: -------------------------------------------------------------------------------- 1 | 2 | for a in range(1,20+1): 3 | for b in range(1, 20+1): 4 | for c in range(1, 20+1): 5 | if a**2 + b**2 == c**2: 6 | print("(" + str(a)+","+str(b)+","+str(c)+")") -------------------------------------------------------------------------------- /lesson05/exercise12.py: -------------------------------------------------------------------------------- 1 | N = 5 2 | for i in range(0, N): 3 | for j in range(0, N-i-1): 4 | print(" ", end=" ") 5 | for j in range(0, 2*i+1): 6 | print("*", end=" ") 7 | print("") 8 | -------------------------------------------------------------------------------- /lesson05/for.list1.py: -------------------------------------------------------------------------------- 1 | players = ["Jordan", "Giannis", "LeBron"] 2 | 3 | for player in players: 4 | print(player + " plays!") -------------------------------------------------------------------------------- /lesson05/for.list2.py: -------------------------------------------------------------------------------- 1 | semester_grades = [4, 6, 3, 8, 10] 2 | 3 | passed = 0 4 | sum_grades = 0 5 | for grade in semester_grades: 6 | if grade >= 5: 7 | passed += 1 8 | sum_grades += grade 9 | 10 | print("I've succeeded in " + str(passed) + " lessons") 11 | print("My average is " + str(sum_grades/passed)) 12 | -------------------------------------------------------------------------------- /lesson05/for.range.py: -------------------------------------------------------------------------------- 1 | for element in sequence: 2 | ... 3 | if condition: 4 | break 5 | ... 6 | -------------------------------------------------------------------------------- /lesson05/for.string.py: -------------------------------------------------------------------------------- 1 | string = "Once upon a time..." 2 | 3 | for char in string: 4 | print(char) -------------------------------------------------------------------------------- /lesson05/nested.loops.py: -------------------------------------------------------------------------------- 1 | for i in range(1,10): 2 | for j in range(1,11): 3 | print(str(i) + "*" + str(j) + "=" + str(i*j)) 4 | print("============") -------------------------------------------------------------------------------- /lesson05/nested.loops2.py: -------------------------------------------------------------------------------- 1 | for i in range(1,6): 2 | for j in range(1,i+1): 3 | print("*", end=" ") 4 | print("") 5 | -------------------------------------------------------------------------------- /lesson05/nested.loops3.py: -------------------------------------------------------------------------------- 1 | N = 5 2 | for i in range(0, N): 3 | for j in range(0, N-i-1): 4 | print(" ", end="") 5 | for j in range(0, i+1): 6 | print("*", end="") 7 | print("") 8 | -------------------------------------------------------------------------------- /lesson05/python05.for.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson05/python05.for.pdf -------------------------------------------------------------------------------- /lesson05/range.py: -------------------------------------------------------------------------------- 1 | for number in range(0,10,3): 2 | print(number) 3 | 4 | print(type(range(0,10))) 5 | -------------------------------------------------------------------------------- /lesson05/while1.py: -------------------------------------------------------------------------------- 1 | i = 1 2 | while i <= 5: 3 | print("Hello!") 4 | i += 1 5 | 6 | -------------------------------------------------------------------------------- /lesson05/while2.py: -------------------------------------------------------------------------------- 1 | i = 1 2 | while i <= 5: 3 | print(i) 4 | i += 1 5 | -------------------------------------------------------------------------------- /lesson05/while3.py: -------------------------------------------------------------------------------- 1 | i = 0 2 | while i <= 4: 3 | print(2*i+1) 4 | i += 1 5 | -------------------------------------------------------------------------------- /lesson05/while4.py: -------------------------------------------------------------------------------- 1 | cnt = 9 2 | while cnt >= 1: 3 | print(cnt) 4 | cnt -= 2 5 | 6 | -------------------------------------------------------------------------------- /lesson05/while5.py: -------------------------------------------------------------------------------- 1 | cnt = 9 2 | while cnt >= 1: 3 | print(cnt) 4 | cnt += 2 5 | -------------------------------------------------------------------------------- /lesson05/while6.py: -------------------------------------------------------------------------------- 1 | number = int(input("Enter a number(0-9): ")) 2 | while number < 0 or number > 9: 3 | number = int(input("Between 0 and 9 please: ")) 4 | 5 | print("You entered: " + str(number)) 6 | -------------------------------------------------------------------------------- /lesson05/while7.py: -------------------------------------------------------------------------------- 1 | active = True 2 | while active: 3 | user_input = input("Type string or 'quit': ") 4 | if user_input == "quit": 5 | print("Bye bye!") 6 | active = False 7 | else: 8 | print("Why " + user_input + "?!") 9 | -------------------------------------------------------------------------------- /lesson06/array2d.py: -------------------------------------------------------------------------------- 1 | array = [] 2 | rows = int(input("Give number of rows: ")) 3 | cols = int(input("Give number of columns: ")) 4 | 5 | for i in range(rows): 6 | array.append([]) 7 | for j in range(cols): 8 | elem = int(input("Give " + str(i) + "," + str(j) + " element: ")) 9 | array[i].append(elem) 10 | 11 | print(array) -------------------------------------------------------------------------------- /lesson06/array2d.traverse.py: -------------------------------------------------------------------------------- 1 | array = [[1,2,3], [4,5,6]] 2 | array[0][1] = 0 3 | for row in array: 4 | for elem in row: 5 | print(elem, end=" ") 6 | print("") 7 | -------------------------------------------------------------------------------- /lesson06/brain.damage.py: -------------------------------------------------------------------------------- 1 | my_list = [1,2,3] 2 | new_list = ((my_list * 2)[1:5] + list((7,8)))*4 3 | print(str((my_list+new_list).count(2))) -------------------------------------------------------------------------------- /lesson06/comprehensions.py: -------------------------------------------------------------------------------- 1 | my_list = [] 2 | for number in range(3): 3 | my_list.append(number) 4 | print(my_list) 5 | 6 | my_list = [number for number in range(3)] 7 | print(my_list) -------------------------------------------------------------------------------- /lesson06/comprehensions2.py: -------------------------------------------------------------------------------- 1 | my_list = [] 2 | for number in range(10): 3 | if number % 2 == 0: 4 | my_list.append(number) 5 | print(my_list) 6 | 7 | my_list = [number for number in range(10) if number%2 == 0] 8 | print(my_list) -------------------------------------------------------------------------------- /lesson06/comprehensions3.py: -------------------------------------------------------------------------------- 1 | my_list = [] 2 | for number in range(10): 3 | if number % 2 == 0: 4 | my_list.append(number) 5 | else: 6 | my_list.append(number/2) 7 | print(my_list) 8 | 9 | my_list = [number if number%2 == 0 else number/2 for number in range(10)] 10 | print(my_list) -------------------------------------------------------------------------------- /lesson06/conversions.py: -------------------------------------------------------------------------------- 1 | my_list = [1,2,3] 2 | my_tuple = tuple(my_list) 3 | print(my_tuple) 4 | 5 | my_tuple = (1,2,3) 6 | my_list = list(my_tuple) 7 | print(my_list) 8 | 9 | my_list = list(range(4)) 10 | print(my_list) 11 | 12 | msg = "Hello" 13 | print(list(msg)) 14 | -------------------------------------------------------------------------------- /lesson06/copy.lists.problem.py: -------------------------------------------------------------------------------- 1 | list1 = [1,2,3] 2 | list2 = list1 3 | list2[0] = 4 4 | print(list1) 5 | print(list2) -------------------------------------------------------------------------------- /lesson06/copy.lists.py: -------------------------------------------------------------------------------- 1 | list1 = [1,2,3] 2 | list2 = list1[:] 3 | list2[0] = 4 4 | print(list1) 5 | print(list2) 6 | -------------------------------------------------------------------------------- /lesson06/exercise1.py: -------------------------------------------------------------------------------- 1 | my_list = [] 2 | for i in range(10): 3 | user_input = int(input("Give a number: ")) 4 | while user_input < 10 or user_input > 20: 5 | user_input = int(input("Give a number(10...20): ")) 6 | my_list.append(user_input) 7 | 8 | print(my_list) 9 | 10 | my_tuple = tuple(my_list) 11 | print(my_tuple) 12 | 13 | list_squares = [] 14 | for i in range(10): 15 | list_squares.append(my_list[i]**2) 16 | 17 | list_squares.sort() 18 | tuple_squares = tuple(list_squares) 19 | print(tuple_squares) 20 | -------------------------------------------------------------------------------- /lesson06/exercise2.py: -------------------------------------------------------------------------------- 1 | N = 5 2 | 3 | for i in range(N): 4 | print(" " * (N-i-1), end="") 5 | print("* " * (2*i+1), end="") 6 | print("") -------------------------------------------------------------------------------- /lesson06/exercise3.py: -------------------------------------------------------------------------------- 1 | N = 27 2 | 3 | if N == 0 or N == 1: 4 | print("It's not prime") 5 | else: 6 | for i in range(2,N): 7 | if N % i == 0: 8 | print("It's not prime") 9 | break 10 | else: 11 | print("It's prime") 12 | -------------------------------------------------------------------------------- /lesson06/exercise4.py: -------------------------------------------------------------------------------- 1 | primes_list = [] 2 | 3 | for N in range(2, 100+1): 4 | for i in range(2, N): 5 | if N % i == 0: 6 | break 7 | else: 8 | primes_list.append(N) 9 | 10 | 11 | primes = tuple(primes_list) 12 | print(primes) -------------------------------------------------------------------------------- /lesson06/exercise5.py: -------------------------------------------------------------------------------- 1 | my_list = [number for number in range(100) if number%2 == 0 and number%3 == 0] 2 | print(my_list) -------------------------------------------------------------------------------- /lesson06/exercise6.py: -------------------------------------------------------------------------------- 1 | array = [ 2 | [1, 2, 3, 4], 3 | [5, 6, 7, 8], 4 | [9, 0, 1, 2] 5 | ] 6 | 7 | array.insert(0, [0, 0, 0, 0]) 8 | 9 | for row in array: 10 | for elem in row: 11 | print(elem, end=" ") 12 | print("") 13 | 14 | for row in array: 15 | row.append(1) 16 | 17 | print("") 18 | for row in array: 19 | for elem in row: 20 | print(elem, end=" ") 21 | print("") 22 | -------------------------------------------------------------------------------- /lesson06/python06.tuples.lists.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson06/python06.tuples.lists.pdf -------------------------------------------------------------------------------- /lesson06/seq.conv.py: -------------------------------------------------------------------------------- 1 | my_list = [1, 2, 3] 2 | my_tuple = tuple(my_list) 3 | print(my_tuple) 4 | 5 | my_tuple = (1, 2, 3) 6 | my_list = list(my_tuple) 7 | print(my_list) 8 | 9 | my_list = list(range(4)) 10 | print(my_list) 11 | 12 | msg = "Hello!" 13 | print(list(msg)) 14 | -------------------------------------------------------------------------------- /lesson06/sequence.ops.py: -------------------------------------------------------------------------------- 1 | numbers = (1,2,3,4,3,2,3,3) 2 | print("Length: " + str(len(numbers))) 3 | print("min: " + str(min(numbers))) 4 | print("max: " + str(max(numbers))) 5 | print("3s: " + str(numbers.count(3))) 6 | print("pos of a 3: " + str(numbers.index(3,4))) 7 | 8 | -------------------------------------------------------------------------------- /lesson06/tuples.py: -------------------------------------------------------------------------------- 1 | int_tuple = (1, 2, 3) 2 | collection = ("hi", 3.14, True) 3 | tuple_of_one = (1,) 4 | empty_tuple = () 5 | 6 | print(collection) 7 | print(type(tuple_of_one)) -------------------------------------------------------------------------------- /lesson06/tuples2.py: -------------------------------------------------------------------------------- 1 | my_tuple = (1, 2, 3) 2 | print(my_tuple[1:3]) 3 | print(my_tuple[1] + 4) 4 | for number in my_tuple: 5 | print(number) -------------------------------------------------------------------------------- /lesson07/exercise1.py: -------------------------------------------------------------------------------- 1 | N = 5 2 | 3 | A = set() 4 | for i in range(1,N+1): 5 | A.add(i) 6 | 7 | print(A) 8 | 9 | result = set() 10 | for element in A: 11 | result.add((element, element**2)) 12 | 13 | print(result) -------------------------------------------------------------------------------- /lesson07/exercise2.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | i = 0 8 | columns = [] 9 | while True: 10 | column = set() 11 | 12 | # 10-19 13 | 14 | rand_number = randrange(10,20) 15 | column.add(rand_number) 16 | 17 | while True: 18 | rand_number = randrange(10,20) 19 | if rand_number not in column: 20 | column.add(rand_number) 21 | break 22 | 23 | # 20-39 24 | 25 | rand_number = randrange(20,40) 26 | column.add(rand_number) 27 | 28 | while True: 29 | rand_number = randrange(20,40) 30 | if rand_number not in column: 31 | column.add(rand_number) 32 | break 33 | 34 | # 1-9 EVEN 35 | 36 | rand_number = 2 * randrange(1,5) 37 | column.add(rand_number) 38 | 39 | # 41-49 ODD 40 | 41 | rand_number = randrange(41,49+1,2) 42 | column.add(rand_number) 43 | 44 | if column not in columns: 45 | columns.append(column) 46 | i += 1 47 | if i==10: 48 | break 49 | 50 | 51 | for column in columns: 52 | print(column) 53 | -------------------------------------------------------------------------------- /lesson07/exercise3.py: -------------------------------------------------------------------------------- 1 | N = 100 2 | 3 | evens = set() 4 | for number in range(0,N+1,2): 5 | evens.add(number) 6 | print(evens) 7 | 8 | odds = set() 9 | for number in range(1,N,2): 10 | odds.add(number) 11 | print(odds) 12 | 13 | multiples3 = set() 14 | for number in range(0,N,3): 15 | multiples3.add(number) 16 | print(multiples3) 17 | 18 | primes = set() 19 | for number in range(2, N+1): 20 | for i in range(2, number): 21 | if number % i == 0: 22 | break 23 | else: 24 | primes.add(number) 25 | print(primes) 26 | 27 | set1 = evens | multiples3 28 | print(set1) 29 | 30 | set2 = odds & primes 31 | print(set2) 32 | 33 | set3 = primes - odds 34 | print(set3) 35 | 36 | set4 = primes ^ odds 37 | print(set4) -------------------------------------------------------------------------------- /lesson07/exercise4.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | N = 30 8 | 9 | pupils = set() 10 | for number in range(N): 11 | pupils.add("pupil" + str(number)) 12 | 13 | list_pupils = list(pupils) 14 | math_teams = set() 15 | for _ in range(N//2): 16 | pos1 = randrange(0, len(list_pupils)) 17 | pupil1 = list_pupils.pop(pos1) 18 | pos2 = randrange(0, len(list_pupils)) 19 | pupil2 = list_pupils.pop(pos2) 20 | team = (pupil1, pupil2) 21 | math_teams.add(team) 22 | print("Math teams: " + str(math_teams)) 23 | 24 | list_pupils = list(pupils) 25 | geography_teams = set() 26 | for _ in range(N//2): 27 | pos1 = randrange(0, len(list_pupils)) 28 | pupil1 = list_pupils.pop(pos1) 29 | pos2 = randrange(0, len(list_pupils)) 30 | pupil2 = list_pupils.pop(pos2) 31 | team = (pupil1, pupil2) 32 | geography_teams.add(team) 33 | print("Geography teams: " + str(geography_teams)) -------------------------------------------------------------------------------- /lesson07/no.lists.in.set.py: -------------------------------------------------------------------------------- 1 | my_set = {1, 2, (1, 2)} 2 | print(my_set) 3 | x = 3 4 | my_set = {x, x**3, (x, 3*x)} 5 | print(my_set) 6 | my_set = {x, [x, 2]} -------------------------------------------------------------------------------- /lesson07/python07.sets.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson07/python07.sets.pdf -------------------------------------------------------------------------------- /lesson07/randrange.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | x = randrange(10, 20) # from 10 to 19 8 | print(x) 9 | -------------------------------------------------------------------------------- /lesson07/set.add.elements.py: -------------------------------------------------------------------------------- 1 | my_set = {1,2,3} 2 | my_set.add(5) 3 | my_set.add(3) 4 | print(my_set) 5 | my_set.update((1,4,5)) 6 | print(my_set) -------------------------------------------------------------------------------- /lesson07/set.comprehensions1.py: -------------------------------------------------------------------------------- 1 | my_set = {number for number in range(3)} 2 | print(my_set) 3 | 4 | my_set = {number for number in range(10) if number%2 == 0} 5 | print(my_set) 6 | 7 | my_set = {number if number%2 == 0 else number/2 8 | for number in range(10)} 9 | print(my_set) -------------------------------------------------------------------------------- /lesson07/set.comprehensions2.py: -------------------------------------------------------------------------------- 1 | my_set = set() 2 | 3 | for i in range(2): 4 | for j in range(3): 5 | my_set.add((i,j)) 6 | print(my_set) 7 | 8 | my_set = {(i, j) for i in range(2) 9 | for j in range(3)} 10 | print(my_set) -------------------------------------------------------------------------------- /lesson07/set.comprehensions3.py: -------------------------------------------------------------------------------- 1 | my_set = set() 2 | 3 | for i in range(6): 4 | if i%2==0: 5 | for j in range(6,10): 6 | if j%2==1: 7 | my_set.add((i,j)) 8 | print(my_set) 9 | 10 | my_set = {(i, j) for i in range(6) if i%2==0 11 | for j in range(6,10) if j%2==1} 12 | print(my_set) -------------------------------------------------------------------------------- /lesson07/set.convert.delete.py: -------------------------------------------------------------------------------- 1 | my_list = [number for number in range(10) if number % 2 == 0] 2 | print(my_list) 3 | my_set = set(my_list) 4 | print(my_set) 5 | my_set.discard(2) 6 | print(my_set) 7 | my_set.remove(2) 8 | print(my_set) 9 | -------------------------------------------------------------------------------- /lesson07/set.copy.py: -------------------------------------------------------------------------------- 1 | set1 = {1,2,3} 2 | set2 = set1.copy() 3 | set2.add(4) 4 | print(set1) 5 | print(set2) -------------------------------------------------------------------------------- /lesson07/set.operations.py: -------------------------------------------------------------------------------- 1 | A = {1,2,3,4} 2 | B = {4,5} 3 | print("union: " + str(A | B)) 4 | print("intersect: " + str(A & B)) 5 | print("diff A-B: " + str(A - B)) 6 | print("diff B-A: " + str(B - A)) 7 | print("symm.dif.: " + str(A ^ B)) 8 | print("A subset B: " + str(A.issubset(B))) 9 | print("A, B disjoint: " + str(A.isdisjoint(B))) 10 | 11 | A = A | B 12 | A.update(B) 13 | print("A= A | B: " + str(A)) 14 | A.intersection_update(B) 15 | print("A= A & B: " + str(A)) -------------------------------------------------------------------------------- /lesson07/sets.py: -------------------------------------------------------------------------------- 1 | int_set = {1, 2, 3} 2 | collection = {"hi", 3.14, True} 3 | empty_set = set() 4 | duplicates = {1, 2, 3, 1, 1, 2} 5 | 6 | print(duplicates) 7 | print(type(int_set)) 8 | -------------------------------------------------------------------------------- /lesson08/dict.add.py: -------------------------------------------------------------------------------- 1 | hero = {"name": "Bruce Banner", "alias": "Iron Man"} 2 | print(hero) 3 | hero["name"] = "Tony Stark" 4 | hero["equipment"] = "suite" 5 | print(hero) 6 | 7 | 8 | -------------------------------------------------------------------------------- /lesson08/dict.comprehensions.py: -------------------------------------------------------------------------------- 1 | dict1 = {v:v**2 for v in range(10)} 2 | print(dict1) 3 | 4 | dict2 = {v:v**2 for v in range(10) if v%2==0} 5 | print(dict2) 6 | 7 | dict3 = {(i, j): 0 for i in range(1,7) 8 | for j in range(1,7)} 9 | print(dict3) -------------------------------------------------------------------------------- /lesson08/dict.convert.delete.py: -------------------------------------------------------------------------------- 1 | a_list = [("name", "Natasha Romanoff"), ("alias", "Black Widow")] 2 | hero = dict(a_list) 3 | hero["ability"] = "hand-to-hand combat" 4 | print(hero) 5 | hero.pop("name") 6 | print(hero) 7 | hero.clear() 8 | print(hero) -------------------------------------------------------------------------------- /lesson08/dict.copy.py: -------------------------------------------------------------------------------- 1 | hero1 = {"name": "Tony Stark", "alias": "Iron Man"} 2 | hero2 = hero1.copy() 3 | hero2["equipment"] = "suite" 4 | print(hero1) 5 | print(hero2) 6 | -------------------------------------------------------------------------------- /lesson08/dict.definition.py: -------------------------------------------------------------------------------- 1 | empty = {} 2 | person = { 3 | "grade": 13, 4 | (1,2): 29, 5 | "name": "Jim", 6 | } 7 | 8 | print(person) 9 | print(type(person)) -------------------------------------------------------------------------------- /lesson08/dict.example.py: -------------------------------------------------------------------------------- 1 | person = { 2 | "name": "Tony Stark", 3 | "alias": "Iron Man", 4 | "power": 20 5 | } 6 | 7 | print(person) 8 | -------------------------------------------------------------------------------- /lesson08/dict.if.py: -------------------------------------------------------------------------------- 1 | hero = {"name": "Bruce Banner", "alias": "Hulk"} 2 | if "equipment" not in hero: 3 | print(hero["alias"] + " has no equipment") -------------------------------------------------------------------------------- /lesson08/dict.loop.py: -------------------------------------------------------------------------------- 1 | heroes_weapons = { 2 | "Black Panther": "Anti-Metal Claws", 3 | "Wolverine": "Claws", 4 | "Ultron": "Plasma Weapons", 5 | "Spider-Man": "Web-shooters", 6 | "Beast": "Claws", 7 | "Venom": "Web-shooters" 8 | } 9 | 10 | print("Key-value loop: ") 11 | for key, value in heroes_weapons.items(): 12 | print(key + " has " + value) 13 | 14 | print("\nOrdered Key loop: ") 15 | for key in sorted(heroes_weapons.keys()): 16 | print(key + " has " + heroes_weapons[key]) 17 | 18 | print("\nWeapons Gallery: ") 19 | for value in set(heroes_weapons.values()): 20 | print(value, end=", ") -------------------------------------------------------------------------------- /lesson08/dict.manipulation.py: -------------------------------------------------------------------------------- 1 | a_list = {("name", "Natasha Romanoff"), ("alias", "Black Widow")} 2 | hero = dict(a_list) 3 | hero["ability"] = "hand-to-hand combat" 4 | print(hero) 5 | hero.pop("name") 6 | print(hero) 7 | hero.clear() 8 | print(hero) 9 | -------------------------------------------------------------------------------- /lesson08/exercise01.py: -------------------------------------------------------------------------------- 1 | dictionary = { 2 | "ιταμός" : "Προκλητικός, αυθάδης, αναιδής", 3 | "όνειδος": "ντροπή, καταισχύνη", 4 | "πομφόλυγες": "αερολογίες, ανοησίες" 5 | } 6 | 7 | print(dictionary) 8 | 9 | dictionary["φληναφήματα"] = "ανοησίες, σαχλαμάρες" 10 | 11 | print(dictionary) 12 | 13 | key = input("Δώσε τη λέξη: ") 14 | value = input("Δώσε την επεξήγηση: ") 15 | 16 | dictionary[key] = value 17 | 18 | print(dictionary) -------------------------------------------------------------------------------- /lesson08/exercise02.py: -------------------------------------------------------------------------------- 1 | person = { 2 | "name": "Κωνσταντίνος", 3 | "surname": "Κωνσταντίνου", 4 | "father_name": "Γεώργιος", 5 | "birth_date": "15/03/1956", 6 | "address": "Πανεπιστημίου 28", 7 | "phone": "2101234567" 8 | } 9 | 10 | print("Ονοματεπώνυμο : " + person["name"] + " " + person["father_name"] + " " + person["surname"]) 11 | print("Ημ/νία Γέννησης:" + person["birth_date"]) 12 | print("Διεύθυνση :" + person["address"]) 13 | print("Τηλέφωνο :" + person["phone"]) -------------------------------------------------------------------------------- /lesson08/exercise03.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | numbers = {} 8 | N = 10 9 | 10 | for i in range(1,6+1): 11 | numbers[i] = 0 12 | 13 | for _ in range(N): 14 | num = randrange(1, 6+1) 15 | numbers[num] += 1 16 | 17 | for i in range(1,6+1): 18 | print(str(i) + ": " + str(numbers[i]/N)) 19 | 20 | -------------------------------------------------------------------------------- /lesson08/exercise04.py: -------------------------------------------------------------------------------- 1 | string = "Bolton refused to testify during Trump's impeachment inquiry, choosing instead to save his disclosures for the book that has already vaulted to Amazon's best seller list, though he later said he would testify in the impeachment trial if subpoenaed by the Senate. He argues in the book that lawmakers should have broadened impeachment inquiries to examine a raft of ways Trump sought to bend the law." 2 | 3 | my_list = list(string) 4 | print(my_list) 5 | 6 | dictionary = {} 7 | 8 | for char in my_list: 9 | if char not in dictionary: 10 | dictionary[char] = 1 11 | else: 12 | dictionary[char] += 1 13 | 14 | max_value = max(list(dictionary.values())) 15 | 16 | for key, value in dictionary.items(): 17 | if value == max_value: 18 | if key == " ": 19 | print("blank") 20 | else: 21 | print(key) -------------------------------------------------------------------------------- /lesson08/exercise05.py: -------------------------------------------------------------------------------- 1 | merchandise = { 2 | "book": 10.18, 3 | "parsley": 0.22, 4 | "cement": 5.17, 5 | "cd": 0.05 6 | } 7 | 8 | rate = 2.2 9 | 10 | new_values = { key: value*(1+rate) for key, value in merchandise.items()} 11 | 12 | print(new_values) 13 | -------------------------------------------------------------------------------- /lesson08/exercise08.initial.py: -------------------------------------------------------------------------------- 1 | s = 0 2 | for i in range(1, 100): 3 | s += 1//i 4 | print("i=" + str(i) +" 1/i=" + str(1//i) + " s=" + str(s)) 5 | print(s) -------------------------------------------------------------------------------- /lesson08/exercise08.py: -------------------------------------------------------------------------------- 1 | s = 0 2 | for i in range(1, 100+1): 3 | s += 1/i 4 | print(s) 5 | -------------------------------------------------------------------------------- /lesson08/python08.dictionaries.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson08/python08.dictionaries.pdf -------------------------------------------------------------------------------- /lesson09/exercise01.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | array = [] 8 | 9 | for row in range(3): 10 | new_row = [] 11 | for item in range(3): 12 | new_row.append(randrange(0,1000)) 13 | array.append(new_row) 14 | 15 | for row in array: 16 | for _ in range(3): 17 | print("+----", end="") 18 | print("+") 19 | for item in row: 20 | print(("|" + str(item) + "\t").expandtabs(5), end="") 21 | print("|") 22 | for _ in range(3): 23 | print("+----", end="") 24 | print("+") 25 | 26 | -------------------------------------------------------------------------------- /lesson09/exercise02.initial.py: -------------------------------------------------------------------------------- 1 | string = "I guess the only time most people think about in justice is when it happens to them." 2 | -------------------------------------------------------------------------------- /lesson09/exercise02.py: -------------------------------------------------------------------------------- 1 | string = "I guess the only time most people think about in justice is when it happens to them." 2 | 3 | letters = set(string) 4 | dictionary = {letter: 0 for letter in letters} 5 | 6 | for char in string: 7 | dictionary[char] += 1 8 | 9 | for key in sorted(dictionary.keys()): 10 | print(key + ": " + str(dictionary[key])) 11 | -------------------------------------------------------------------------------- /lesson09/exercise04.initial.py: -------------------------------------------------------------------------------- 1 | string = """ 2 | How the hell could a person enjoy being awakened at 6:30AM, 3 | by an alarm clock, leap out of bed, dress, force-feed, shit, piss, 4 | brush teeth and hair, and fight traffic to get to a place 5 | where essentially you made lots of money for somebody else 6 | and were asked to be grateful for the opportunity to do so?""" 7 | -------------------------------------------------------------------------------- /lesson09/exercise04.py: -------------------------------------------------------------------------------- 1 | string = """ 2 | How the hell could a person enjoy being awakened at 6:30AM, 3 | by an alarm clock, leap out of bed, dress, force-feed, shit, piss, 4 | brush teeth and hair, and fight traffic to get to a place 5 | where essentially you made lots of money for somebody else 6 | and were asked to be grateful for the opportunity to do so?""" 7 | 8 | lower = string.lower() 9 | letters = set(lower) 10 | dictionary = {letter: 0 for letter in letters} 11 | 12 | for char in lower: 13 | dictionary[char] += 1 14 | 15 | for char in " ,-036:?\n": 16 | dictionary.pop(char) 17 | 18 | for key in sorted(dictionary.keys()): 19 | print(key + ": " + str(dictionary[key])) 20 | -------------------------------------------------------------------------------- /lesson09/exercise05.py: -------------------------------------------------------------------------------- 1 | while True: 2 | string = input("Give an integer: ") 3 | 4 | if string.isdigit(): 5 | number = int(string) 6 | print(f"Number entered: {number}") 7 | break 8 | else: 9 | print("INTEGER mf please! ") 10 | -------------------------------------------------------------------------------- /lesson09/exercise06.py: -------------------------------------------------------------------------------- 1 | while True: 2 | string = input("Give name: ") 3 | string = string.strip() 4 | 5 | if string.isalpha(): 6 | name = string.capitalize() 7 | print(f"Name entered: {name}") 8 | break 9 | else: 10 | print("only characters please! ") 11 | 12 | while True: 13 | string = input("Give surname: ") 14 | string = string.strip() 15 | 16 | if string.isalpha(): 17 | surname = string.capitalize() 18 | print(f"Surname entered: {surname}") 19 | break 20 | else: 21 | print("only characters please! ") 22 | 23 | 24 | print(f"+{28*'-'}+") 25 | print(f"|{(name+ ' ' + surname).center(28)}|") 26 | print(f"+{28*'-'}+") -------------------------------------------------------------------------------- /lesson09/exercise07.initial.py: -------------------------------------------------------------------------------- 1 | quote = "I don't hate them...I just feel better when they're not around." 2 | -------------------------------------------------------------------------------- /lesson09/exercise07.py: -------------------------------------------------------------------------------- 1 | quote = "I don't hate them...I just feel better when they're not around." 2 | 3 | while True: 4 | while True: 5 | string = input("Give word: ") 6 | string = string.strip() 7 | 8 | if string.isalpha(): 9 | word = string.lower() 10 | break 11 | else: 12 | print("only characters please! ") 13 | 14 | if word == "quit": 15 | break 16 | 17 | quote_lowered = quote.lower() 18 | 19 | if quote.find(word)!=-1: 20 | print(quote_lowered.replace(word,word.upper())) 21 | else: 22 | print("Word not exists in quote!") -------------------------------------------------------------------------------- /lesson09/exercise08.initial.py: -------------------------------------------------------------------------------- 1 | poem = """ There’s a bluebird in my heart that 2 | wants to get out 3 | but I’m too tough for him, 4 | I say, 5 | stay down, do you want to mess me up? 6 | you want to screw up the works? 7 | you want to blow my book sales in Europe? 8 | """ -------------------------------------------------------------------------------- /lesson09/exercise08.py: -------------------------------------------------------------------------------- 1 | poem = """ There’s a bluebird in my heart that 2 | wants to get out 3 | but I’m too tough for him, 4 | I say, 5 | stay down, do you want to mess me up? 6 | you want to screw up the works? 7 | you want to blow my book sales in Europe? 8 | """ 9 | 10 | while True: 11 | string = input("Give word: ") 12 | string = string.strip() 13 | 14 | if string.isalpha(): 15 | word = string.lower() 16 | break 17 | else: 18 | print("only characters please! ") 19 | 20 | poem_lowered = poem.lower() 21 | poem_lines = poem_lowered.splitlines() 22 | print(poem_lines) 23 | 24 | for pos in range(len(poem_lines)): 25 | if poem_lines[pos].find(word) != -1: 26 | print("Line " + str(pos) + ": " + poem_lines[pos].replace(word, word.upper())) -------------------------------------------------------------------------------- /lesson09/exercise09.py: -------------------------------------------------------------------------------- 1 | string = "Hello!" 2 | integer = 5 3 | fl = 3.14 4 | 5 | print(string + " " + str(integer) + " " + str(fl)) 6 | print(f"{string} {integer} {fl}") 7 | print("%s %d %f" % (string, integer, fl)) 8 | print("{} {} {}".format(string, integer, fl)) -------------------------------------------------------------------------------- /lesson09/exercise10.py: -------------------------------------------------------------------------------- 1 | for i in range(10000): 2 | if i%50==0: 3 | print(f"\n{i}-{i+49}: ", end="") 4 | print(chr(i), end="") 5 | -------------------------------------------------------------------------------- /lesson09/fstrings.py: -------------------------------------------------------------------------------- 1 | print(f"With a result: {1+4}") 2 | x = 3 3 | print(f"For debugging: {x=}") 4 | mult_line = ( 5 | f"multiline: {x} value\n" 6 | f"multiline: {x*x} square" 7 | ) 8 | print(mult_line) 9 | print(f"A float with 2 decimals: {1/3:.2}|") 10 | print(f"A float with width 6: {1/4:6}|") 11 | print(f"A float with width 6 and 2 decimals: {1/3:6.2}|") 12 | print(f"An integer(hexadecimal): {155:x}") 13 | print(f"An integer(octal): {155:o}") 14 | print(f"An integer(scientific): {155:e}") -------------------------------------------------------------------------------- /lesson09/python.encoding.py: -------------------------------------------------------------------------------- 1 | print("a".encode("ascii")) 2 | print("a".encode("utf-8")) 3 | print("α".encode("utf-8")) 4 | print("⻨".encode("utf-8")) 5 | print("a".encode("utf-32")) 6 | print("α".encode("utf-32")) 7 | print("⻨".encode("utf-32")) -------------------------------------------------------------------------------- /lesson09/python.encoding128.py: -------------------------------------------------------------------------------- 1 | for i in range(128): 2 | if i%10==0: 3 | print(f"\n{i}-{i+9}: ", end="") 4 | print(chr(i), end="") 5 | -------------------------------------------------------------------------------- /lesson09/python09.strings.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson09/python09.strings.pdf -------------------------------------------------------------------------------- /lesson09/sting.character.checking.py: -------------------------------------------------------------------------------- 1 | print("alnum : 123abc : " + str("123abc".isalnum())) 2 | print("alnum : 123abc# : " + str("123abc#".isalnum())) 3 | print("alpha : abcDδΔ : " + str("abcDδΔ".isalpha())) 4 | print("alpha : δαες12 : " + str("δαες12".isalpha())) 5 | print("digit : 123456 : " + str("123456".isdigit())) 6 | print("digit : 123a34 : " + str("123a34".isdigit())) 7 | print("space : : " + str(" ".isspace())) 8 | print("printable : aA12 # : " + str("aA12 #".isprintable())) 9 | print("printable : \\na : " + str("\na ".isprintable())) 10 | print("identifier: is_var : " + str("is_var".isidentifier())) 11 | print("identifier: 12ar : " + str("12ar".isidentifier())) 12 | print("lower : abca34 : " + str("abca34".islower())) 13 | print("upper : ABFa34 : " + str("ABFa34".isupper())) 14 | print("title : Tik Tok : " + str("Tik Tok".istitle())) -------------------------------------------------------------------------------- /lesson09/string.as.sequence.py: -------------------------------------------------------------------------------- 1 | string = "Sample String" 2 | print((string + " ") * 3) # mult 3 | print(string[1]) # indexing 4 | print(string[1:4] + string[-4:-1]) # slicing 5 | print(len(string)) # length 6 | print(max("sample")) # max 7 | print(min("String")) # min 8 | print(string.index("am")) # searching 9 | print(string.count("S")) # counting 10 | # str[2] = "c" not working.. 11 | new_str = string[:2] + "c" + string[3:] 12 | print(new_str) -------------------------------------------------------------------------------- /lesson09/string.convert.py: -------------------------------------------------------------------------------- 1 | print("int: " + str(5)) 2 | print("float: " + str(5.1)) 3 | print("boolean: " + str(True)) 4 | print("list: " + str([1,2])) 5 | print("tuple: " + str((1,2))) 6 | print("set: " + str({1,2})) 7 | print("dict: " + str({1:2,3:4})) 8 | 9 | print(list("Find what you love and let it kill you.")) 10 | 11 | print(" ".join(["a","b","c"])) 12 | print(", ".join({"a","b","c"})) 13 | print(" # ".join({"a":1, "k":10})) -------------------------------------------------------------------------------- /lesson09/string.definition.py: -------------------------------------------------------------------------------- 1 | empty_str = "" 2 | string = 'Hello World!' 3 | string2 = "I said 'Hello'!" 4 | mult_string = """... 5 | Hello there! 6 | I am a multi-line string! 7 | """ 8 | print(string2) 9 | print(mult_string) 10 | print(type(string2)) -------------------------------------------------------------------------------- /lesson09/string.escape.characters.py: -------------------------------------------------------------------------------- 1 | print("quotes: \", \'") 2 | print("slashes: / \\") 3 | print("tabs:\n\t|\t|a\t|aa\t|aaa\t|") 4 | print("change line: \n newline \r CR") 5 | print("Backspace: bb\baa\bc\b\baa\b") 6 | -------------------------------------------------------------------------------- /lesson09/string.format.c.py: -------------------------------------------------------------------------------- 1 | print("int value: %d %d" % (1, 5+1)) 2 | print("int width(right align): %10d " % 1) 3 | print("int width(left align): %-10d " % 1) 4 | print("float: %f" % (1/3)) 5 | print("float: decimals: %.4f %.2f" % (1/3, 1/8)) 6 | print("string: %s" % "Hello there! ") 7 | 8 | -------------------------------------------------------------------------------- /lesson09/string.format.py: -------------------------------------------------------------------------------- 1 | print("int value: {} {}".format(1, 5+1)) 2 | print("int width(right align): {:>10d} ".format(1)) 3 | print("int width(left align): {:<10d} ".format(1)) 4 | print("float: {}".format(1/3)) 5 | print("float: decimals: {:.4f} {:.2f}".format(1/3, 1/8)) 6 | print("string: {}".format("Hello there! ")) 7 | 8 | -------------------------------------------------------------------------------- /lesson09/string.methods.modify.py: -------------------------------------------------------------------------------- 1 | string = "Burning in Water, Drowning in Flame" 2 | 3 | print("capitalize: " + string.capitalize()) 4 | print("lower: " + string.lower()) 5 | print("casefold: " + string.casefold()) 6 | print("upper: " + string.upper()) 7 | print("title: " + string.title()) 8 | print("swapcase: " + string.swapcase()) -------------------------------------------------------------------------------- /lesson09/string.operators.py: -------------------------------------------------------------------------------- 1 | string = "Hello " 2 | concatenation = string + string 3 | print(f"Concatenation(+): {concatenation}") 4 | multiplication = string * 5 5 | print(f"Multiplication(*): {multiplication}") 6 | string += "World!" # string = string + "World!" 7 | print(f"Increment: {string}") 8 | print(f"Char in string: 'W' in {string}: {'W' in string}") 9 | print(f"Char not in string: 'W' not in {string}: {'W' not in string}") 10 | comparison = "abc" < "abd" # also: >, <=, >= 11 | print(f"Comparison: {comparison}") 12 | equality = "aa" == "AA".lower() # also not equal: != 13 | print(f"Equality: {equality}") -------------------------------------------------------------------------------- /lesson09/string.partitioning.py: -------------------------------------------------------------------------------- 1 | string = "You have to die a few times before you can really live." 2 | print("split(by blanks): " + str(string.split())) 3 | print("split(by a's): " + str(string.split("a"))) 4 | print("split(by blanks): " + str(string.split(" ", 2))) 5 | print("rsplit(by blanks): " + str(string.rsplit(" ", 2))) 6 | print("partition(by 'few'): " + str(string.partition("few"))) 7 | print("rpartition(by 'r'): " + str(string.rpartition("r"))) 8 | 9 | mult_line = """Some lose all mind and become soul, insane. 10 | some lose all soul and become mind, intellectual. 11 | some lose both and become accepted""" 12 | print("splitlines: " + str(mult_line.splitlines())) -------------------------------------------------------------------------------- /lesson09/string.searching.py: -------------------------------------------------------------------------------- 1 | string = "Some people never go crazy. What truly horrible lives they must lead." 2 | print("startswith: " + str(string.startswith("Some"))) 3 | print("startswith with bounds: " + str(string.startswith("people", 5))) 4 | print("startswith with bounds: " + str(string.startswith("people", 5,8))) 5 | print("endswith: " + str(string.endswith("people",0,11))) 6 | print("find: " + str(string.find("crazy"))) 7 | print("position(s) of \"o\": ", end="") 8 | pos = -1 9 | lpos = string.rfind("o") 10 | while pos != lpos: 11 | pos = string.find("o", pos+1) 12 | print(pos, end=" ") 13 | 14 | print("\nreplace: " + string.replace("o","0",3)) -------------------------------------------------------------------------------- /lesson09/string.tab.length.py: -------------------------------------------------------------------------------- 1 | string = "tabs:\n\t|\t|a\t|aa\t|aaa\t|" 2 | print(string) 3 | new_string = string.expandtabs(5) 4 | print(new_string) -------------------------------------------------------------------------------- /lesson09/string.trimming.justifying.py: -------------------------------------------------------------------------------- 1 | string = " Factotum " 2 | print("lstrip : |" + string.lstrip() + "|") 3 | print("rstrip : |" + string.rstrip() + "|") 4 | print("strip : |" + string.strip() + "|") 5 | print("ljust : |" + string.strip().ljust(30) + "|") 6 | print("rjust : |" + string.strip().rjust(30) + "|") 7 | print("center : |" + string.strip().center(30,"-") + "|") 8 | 9 | string = "abcd" 10 | print("zfill(2) : " + string.zfill(2)) 11 | print("zfill(10): " + string.zfill(10)) -------------------------------------------------------------------------------- /lesson09/string.upper.lower.py: -------------------------------------------------------------------------------- 1 | string = "Burning in Water, Drowning in Flame" 2 | print("capitalize: " + string.capitalize()) 3 | print("lower: " + string.lower()) 4 | print("casefold: " + string.casefold()) 5 | print("upper: " + string.upper()) 6 | print("title: " + string.title()) 7 | print("swapcase: " + string.swapcase()) 8 | -------------------------------------------------------------------------------- /lesson10/compound.py: -------------------------------------------------------------------------------- 1 | #list mutable [ , ] => [ , , ] with elements: mut/imm 2 | l1 = [1,2] 3 | l2 = [1,2] 4 | print(id(l1)) 5 | print(id(l2)) 6 | 7 | l1 = [1,2] 8 | l2 = l1 9 | print(id(l1)) 10 | print(id(l2)) 11 | 12 | # tuple: immutable ( , ) with elements: mut/imm 13 | t = (1,[1,2]) 14 | t[1].append(3) 15 | print(t) 16 | 17 | # set: mutable { , } => { , , } with elements imm 18 | 19 | #dictionary: mutable with elements: (key, value) key: imm, value: imm/mut 20 | -------------------------------------------------------------------------------- /lesson10/data.types.py: -------------------------------------------------------------------------------- 1 | print(type(1)) 2 | print(type(1.2)) 3 | print(type(1+2j)) 4 | print(type(True)) 5 | print(type("Hello!")) 6 | print(type({1,2})) 7 | print(type((1,2))) 8 | print(type({1:2})) 9 | print(type(range(3))) 10 | print(type(frozenset({1,2}))) 11 | 12 | -------------------------------------------------------------------------------- /lesson10/exercise01.py: -------------------------------------------------------------------------------- 1 | subsets = set() 2 | N=4 3 | 4 | for i in range(1,N+1): 5 | for j in range(i+1,N+1): 6 | subsets.add(frozenset({i,j})) 7 | 8 | print(subsets) -------------------------------------------------------------------------------- /lesson10/exercise02.py: -------------------------------------------------------------------------------- 1 | N = 10 2 | 3 | subsets = set() 4 | subsets.add(frozenset()) 5 | 6 | for i in range(1,N+1): 7 | new_subsets = set() 8 | for subset in subsets: 9 | nonfz = set(subset) 10 | nonfz.add(i) 11 | fz = frozenset(nonfz) 12 | new_subsets.add(fz) 13 | subsets.update(new_subsets) 14 | 15 | print(subsets) 16 | print(len(subsets)) 17 | -------------------------------------------------------------------------------- /lesson10/exercise03.py: -------------------------------------------------------------------------------- 1 | x = 1 + 5j 2 | y = 2 + 2j 3 | z = x + y 4 | print(z) 5 | z = x * y 6 | print(z) 7 | print(x.conjugate()) -------------------------------------------------------------------------------- /lesson10/exercise04.py: -------------------------------------------------------------------------------- 1 | x = {frozenset(), frozenset({1}), frozenset({2}), frozenset({1,2})} -------------------------------------------------------------------------------- /lesson10/exercise07.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | seed(datetime.now()) # once, before randint call 6 | 7 | words = [ 8 | "retreat", 9 | "page", 10 | "integrated", 11 | "coincidence", 12 | "mouse", 13 | "trait", 14 | "encourage", 15 | "flour", 16 | "fast", 17 | "attachment", 18 | "idea", 19 | "undress", 20 | "quota", 21 | "miss", 22 | "motif" 23 | ] 24 | 25 | hidden_word = words[randrange(len(words))] 26 | print(hidden_word) 27 | 28 | guessed_letters = [] 29 | max_rounds = 10 30 | for round in range(1, max_rounds+1): 31 | print(f"ROUND {round}") 32 | while True: 33 | letter = input("give a letter: ").lower() 34 | 35 | if len(letter)!=1: 36 | print("Error. Please give ONE letter!!") 37 | elif not letter.isalpha(): 38 | print("Error. Please write letters!") 39 | elif letter in guessed_letters: 40 | print("You have already typed this letter!") 41 | else: 42 | break 43 | 44 | 45 | 46 | 47 | guessed_letters.append(letter) 48 | 49 | print(f"Letter {letter} exists {hidden_word.count(letter)} in hidden word.") 50 | 51 | found = True 52 | for char in hidden_word: 53 | if char in guessed_letters: 54 | print(char, end="") 55 | else: 56 | print("_", end="") 57 | found = False 58 | print("") 59 | 60 | if found: 61 | print("Success! You 've found it!") 62 | break 63 | else: 64 | print("Failure! Maximum rounds reached!") 65 | print("The hidden word was: " + hidden_word) -------------------------------------------------------------------------------- /lesson10/frozen.set.py: -------------------------------------------------------------------------------- 1 | empty = frozenset() 2 | frozen_set_int = frozenset({1, 2, 3}) 3 | fs_in_set = {frozenset(), frozenset({1,2})} 4 | print(empty) 5 | print(fs_in_set) 6 | print(type(frozen_set_int)) -------------------------------------------------------------------------------- /lesson10/id.py: -------------------------------------------------------------------------------- 1 | x = 5 2 | print(f"x: {id(x)}, 5: {id(5)}") 3 | y = 5 4 | print(f"y: {id(y)}") 5 | x = 6 6 | print(f"x: {id(x)}, 6: {id(6)}") 7 | y = x 8 | print(f"x: {id(x)}, y: {id(y)}") -------------------------------------------------------------------------------- /lesson10/id2.py: -------------------------------------------------------------------------------- 1 | x = "Hello!" 2 | print(f"x: {id(x)}") 3 | x = "Hello World!" 4 | print(f"x: {id(x)}") -------------------------------------------------------------------------------- /lesson10/id3.py: -------------------------------------------------------------------------------- 1 | x = [1, 2] 2 | print(f"x:{id(x)}, x[0]:{id(x[0])}, x[1]:{id(x[1])}") 3 | y = x 4 | print(f"y:{id(y)}, y[0]:{id(y[0])}, y[1]:{id(y[1])}") 5 | y = x.copy() # y = x[:] 6 | print(f"y:{id(y)}, y[0]:{id(y[0])}, y[1]:{id(y[1])}") 7 | y[1] = 3 8 | print(f"x:{x} x:{id(x)}, x[0]:{id(x[0])}, x[1]:{id(x[1])}") 9 | print(f"y:{y} y:{id(y)}, y[0]:{id(y[0])}, y[1]:{id(y[1])}") 10 | -------------------------------------------------------------------------------- /lesson10/id4.py: -------------------------------------------------------------------------------- 1 | x = [[1,2], [3,4]] 2 | y = x.copy() 3 | print(f"x:{x} x:{id(x)}, x[0]:{id(x[0])}, x[1]:{id(x[1])}") 4 | print(f"y:{y} y:{id(y)}, y[0]:{id(y[0])}, y[1]:{id(y[1])}") 5 | x[0].append(0) 6 | print(f"x:{x} y:{y}") -------------------------------------------------------------------------------- /lesson10/id5.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | x = [[1,2], [3,4]] 4 | y = copy.deepcopy(x) 5 | print(f"x:{x} x:{id(x)}, x[0]:{id(x[0])}, x[1]:{id(x[1])}") 6 | print(f"y:{y} y:{id(y)}, y[0]:{id(y[0])}, y[1]:{id(y[1])}") 7 | x[0].append(0) 8 | print(f"x:{x} y:{y}") -------------------------------------------------------------------------------- /lesson10/python10.memory.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson10/python10.memory.pdf -------------------------------------------------------------------------------- /lesson11/exercise01.py: -------------------------------------------------------------------------------- 1 | def favorite_movie(movie_title): 2 | if "Batman" in movie_title: 3 | print("good choice") 4 | else: 5 | print("awful taste") 6 | 7 | 8 | favorite_movie("Batman and Robin") 9 | favorite_movie("Superman returns") 10 | -------------------------------------------------------------------------------- /lesson11/exercise02.py: -------------------------------------------------------------------------------- 1 | def favorite_author(author): 2 | if "Tolkien" in author: 3 | for _ in range(500): 4 | print("Tolkien is the best!!") 5 | else: 6 | print(author + "is good!") 7 | 8 | 9 | favorite_author("J.R.R. Tolkien") 10 | favorite_author("Stephen King") -------------------------------------------------------------------------------- /lesson11/exercise03.py: -------------------------------------------------------------------------------- 1 | def input_integer(): 2 | while True: 3 | data = input("Give an integer: ").strip() 4 | if data.isdigit(): 5 | return int(data) 6 | else: 7 | print("Only digits please. ") 8 | 9 | print(input_integer()) -------------------------------------------------------------------------------- /lesson11/exercise04.py: -------------------------------------------------------------------------------- 1 | def input_float(): 2 | while True: 3 | data = input("Give an float: ").strip() 4 | if "." in data: 5 | parts = data.split(".") 6 | if len(parts) > 2: 7 | print("Only one dot at most please.") 8 | elif parts[0].isdigit() and parts[1].isdigit(): 9 | return float(data) 10 | else: 11 | print("Only digits please.") 12 | else: # . not in data 13 | if data.isdigit(): 14 | return float(data) 15 | else: 16 | print("Only digits please. ") 17 | 18 | 19 | print(input_float()) -------------------------------------------------------------------------------- /lesson11/exercise05.py: -------------------------------------------------------------------------------- 1 | def is_odd(number): 2 | return number % 2 == 1 3 | 4 | 5 | def is_even(number): 6 | return number % 2 == 0 7 | 8 | 9 | def is_prime(number): 10 | if number == 0 or number == 1: 11 | return False 12 | for i in range(2, int(number / 2) + 1): 13 | if number % i == 0: 14 | return False 15 | return True 16 | 17 | 18 | def is_square(number): 19 | i = 0 20 | sq = 0 21 | while sq < number: 22 | i += 1 23 | sq = i * i 24 | 25 | return sq == number 26 | 27 | 28 | def is_cube(number): 29 | i = 0 30 | cub = 0 31 | while cub < number: 32 | i += 1 33 | cub = i ** 3 34 | 35 | return cub == number 36 | 37 | 38 | for i in range(1, 100 + 1): 39 | print(f"{i}: ", end="") 40 | if is_odd(i): 41 | print("odd", end=" ") 42 | if is_even(i): 43 | print("even", end=" ") 44 | if is_prime(i): 45 | print("prime", end=" ") 46 | if is_square(i): 47 | print("square", end=" ") 48 | if is_cube(i): 49 | print("cube", end=" ") 50 | print("") 51 | -------------------------------------------------------------------------------- /lesson11/exercise06.py: -------------------------------------------------------------------------------- 1 | PI = 3.14159265359 2 | 3 | def triangle_area(base, height): 4 | return base*height/2 5 | 6 | 7 | def square_perimeter(edge): 8 | return 4*edge 9 | 10 | 11 | def square_area(edge): 12 | return edge ** 2 13 | 14 | 15 | def circle_perimeter(radius): 16 | return 2*PI*radius 17 | 18 | 19 | def circle_area(radius): 20 | return PI * radius ** 2 21 | 22 | 23 | print(circle_area(1)) 24 | -------------------------------------------------------------------------------- /lesson11/exercise07.py: -------------------------------------------------------------------------------- 1 | def digits_print(number): 2 | third = number % 10 3 | number = number // 10 4 | second = number % 10 5 | first = number // 10 6 | print(f"1st digit: {first}") 7 | print(f"2nd digit: {second}") 8 | print(f"3rd digit: {third}") 9 | 10 | 11 | def digits(number): 12 | if number < 100 or number > 999: 13 | return None 14 | else: 15 | third = number % 10 16 | number = number // 10 17 | second = number % 10 18 | first = number // 10 19 | return first, second, third 20 | 21 | digits_print(352) 22 | 23 | f, s, t = digits(352) 24 | print(f) 25 | print(s) 26 | print(t) 27 | 28 | print(digits(44)) -------------------------------------------------------------------------------- /lesson11/exercise08.py: -------------------------------------------------------------------------------- 1 | from random import seed 2 | from random import randrange 3 | from datetime import datetime # all 3 at the beginning 4 | 5 | 6 | def player1_pick(): 7 | player1.add(deck.pop()) 8 | 9 | 10 | def player2_pick(): 11 | player2.add(deck.pop()) 12 | 13 | 14 | def play(): 15 | print("START") 16 | for _ in range(len(deck)): 17 | if randrange(2)==0: 18 | player1_pick() 19 | else: 20 | player2_pick() 21 | print(f"Player 1: {len(player1)}") 22 | print(f"Player 2: {len(player2)}") 23 | if len(player1) > len(player2): 24 | print("Player1 wins") 25 | elif len(player1) == len(player2): 26 | print("draw") 27 | else: 28 | print("Player2 wins") 29 | 30 | seed(datetime.now()) # once, before randint call 31 | 32 | kind = {"heart", "diamond", "spade", "club"} 33 | number = {"ace", 2, 3, 4, 5, 6, 7, 8, 9, 10,"jack", "queen", "king"} 34 | 35 | deck = {(k, n) for k in kind for n in number} 36 | 37 | player1 = set() 38 | player2 = set() 39 | 40 | play() 41 | -------------------------------------------------------------------------------- /lesson11/exercise10.1.py: -------------------------------------------------------------------------------- 1 | # immutable.arguments.py 2 | def f(arg): 3 | print(arg) 4 | print(f"id arg={id(arg)}") 5 | arg="Change!" 6 | print(arg) 7 | print(f"id s={id(arg)}") 8 | 9 | s = "Initial" 10 | print(s) 11 | print(f"id s={id(s)}") 12 | f(s) 13 | print(s) 14 | print(f"id s={id(s)}") -------------------------------------------------------------------------------- /lesson11/exercise10.2.py: -------------------------------------------------------------------------------- 1 | def f(arg): 2 | print(arg) 3 | print(f"id arg={id(arg)}") 4 | arg.append(3) 5 | print(arg) 6 | print(f"id arg={id(arg)}") 7 | 8 | l = [1,2] 9 | print(l) 10 | print(f"id l={id(l)}") 11 | f(l) 12 | print(l) 13 | print(f"id l={id(l)}") 14 | 15 | -------------------------------------------------------------------------------- /lesson11/exercise10.3.py: -------------------------------------------------------------------------------- 1 | def f(arg): 2 | print(arg) 3 | print(f"id arg={id(arg)}") 4 | arg = [3] 5 | print(arg) 6 | print(f"id arg={id(arg)}") 7 | 8 | l = [1,2] 9 | print(l) 10 | print(f"id l={id(l)}") 11 | f(l) 12 | print(l) 13 | print(f"id l={id(l)}") 14 | 15 | 16 | arg = [r1] -------------------------------------------------------------------------------- /lesson11/exercise11.py: -------------------------------------------------------------------------------- 1 | def f(arg): 2 | arg = 5 3 | return arg 4 | 5 | 6 | x = 4 7 | x = f(x) 8 | print(x) 9 | -------------------------------------------------------------------------------- /lesson11/exercise12.py: -------------------------------------------------------------------------------- 1 | def max3(x, y, z): 2 | if x >= y and x >= z: 3 | return x 4 | elif y >= x and y >= z: 5 | return y 6 | else: 7 | return z 8 | 9 | 10 | x = int(input("Give a number: ")) 11 | y = int(input("Give a number: ")) 12 | z = int(input("Give a number: ")) 13 | 14 | n = max3(x, y, z) 15 | 16 | for i in range(1,n+1): 17 | sq = i*i 18 | print(sq) 19 | -------------------------------------------------------------------------------- /lesson11/exercise12_initial.py: -------------------------------------------------------------------------------- 1 | def max3(x, y, z): 2 | if x > y and x > z: 3 | return x 4 | elif y > x and y > z: 5 | return y 6 | else: 7 | return z 8 | 9 | 10 | x = input("Give a number: ") 11 | y = input("Give a number: ") 12 | z = input("Give a number: ") 13 | 14 | n = max3(x, y, z) 15 | 16 | for i in range(n): 17 | sq = i*i 18 | print(sq) -------------------------------------------------------------------------------- /lesson11/function.definition.py: -------------------------------------------------------------------------------- 1 | def hello(): 2 | print("Hello World!") 3 | 4 | 5 | hello() 6 | hello() 7 | -------------------------------------------------------------------------------- /lesson11/global.vs.local.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | x = 3 3 | print(x) 4 | 5 | x = 2 6 | f() 7 | print(x) -------------------------------------------------------------------------------- /lesson11/global1.py: -------------------------------------------------------------------------------- 1 | # global1.py 2 | def f(): 3 | print(x) 4 | 5 | 6 | x = 5 7 | f() -------------------------------------------------------------------------------- /lesson11/global2.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | global x 3 | x = 3 4 | print(x) 5 | 6 | x = 2 7 | f() 8 | print(x) -------------------------------------------------------------------------------- /lesson11/immutable.arguments.py: -------------------------------------------------------------------------------- 1 | # immutable.arguments.py 2 | def f(arg): 3 | print(arg) 4 | arg="Change!" 5 | print(arg) 6 | 7 | s = "Initial" 8 | print(s) 9 | f(s) 10 | print(s) -------------------------------------------------------------------------------- /lesson11/local1.py: -------------------------------------------------------------------------------- 1 | # local1.py 2 | def f(): 3 | x = 5 4 | print(x) 5 | 6 | f() -------------------------------------------------------------------------------- /lesson11/local2.py: -------------------------------------------------------------------------------- 1 | #local2.py 2 | def f(): 3 | x = 5 4 | 5 | 6 | f() 7 | print(x) 8 | -------------------------------------------------------------------------------- /lesson11/main.py: -------------------------------------------------------------------------------- 1 | # constants 2 | PI = 3.14 3 | # globals 4 | deck = {("ace","spade")} 5 | 6 | # functions 7 | def f(): 8 | print("something") 9 | 10 | def g(): 11 | print("something else") 12 | 13 | # main 14 | def main(): 15 | f() 16 | g() 17 | 18 | main() -------------------------------------------------------------------------------- /lesson11/mutable.arguments.assignment.py: -------------------------------------------------------------------------------- 1 | # mutable.arguments.assignment.py 2 | def f(arg): 3 | print(arg) 4 | arg = [3] 5 | print(arg) 6 | 7 | l = [1,2] 8 | print(l) 9 | f(l) 10 | print(l) 11 | -------------------------------------------------------------------------------- /lesson11/mutable.arguments.py: -------------------------------------------------------------------------------- 1 | def f(arg): 2 | print(arg) 3 | arg.append(3) 4 | print(arg) 5 | 6 | l = [1,2] 7 | print(l) 8 | f(l) 9 | print(l) -------------------------------------------------------------------------------- /lesson11/none.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | return 3 | 4 | 5 | ret = f() 6 | print(f"{ret}, {type(ret)}") 7 | -------------------------------------------------------------------------------- /lesson11/parameters.py: -------------------------------------------------------------------------------- 1 | def print_full_name(name, surname, fathers_name): 2 | print(f"{name} {fathers_name[0]}. {surname}") 3 | 4 | 5 | print_full_name("Bruce", "Wayne", "Thomas") 6 | print_full_name("Clark", "Kent", "Jonathan") 7 | -------------------------------------------------------------------------------- /lesson11/python11.functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson11/python11.functions.pdf -------------------------------------------------------------------------------- /lesson11/return.value.py: -------------------------------------------------------------------------------- 1 | def is_odd(number): 2 | if number % 2 == 1: 3 | return True 4 | return False 5 | 6 | 7 | print(f"1 is odd: {is_odd(1)}") 8 | -------------------------------------------------------------------------------- /lesson11/return_values.py: -------------------------------------------------------------------------------- 1 | def square_cube(number): 2 | return number ** 2, number ** 3 3 | 4 | 5 | num = 5 6 | square, cube = square_cube(num) 7 | print(f"{num}^2={square}, {num}^3={cube}") 8 | -------------------------------------------------------------------------------- /lesson12/exercise01.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | p = 1 3 | for i in range(2, n+1): 4 | p = p * i 5 | 6 | return p 7 | 8 | for i in range(2, 50): 9 | print(factorial(i)) -------------------------------------------------------------------------------- /lesson12/exercise02.py: -------------------------------------------------------------------------------- 1 | # fibonacci.py 2 | def fibonacci(n): 3 | if n == 0: 4 | return 0 5 | elif n == 1: 6 | return 1 7 | else: 8 | return fibonacci(n - 1) + fibonacci(n - 2) 9 | 10 | 11 | for i in range(101): 12 | print(f"fibonacci({i})={fibonacci(i)}") 13 | -------------------------------------------------------------------------------- /lesson12/exercise04.py: -------------------------------------------------------------------------------- 1 | def fibonacci(n): 2 | fib = [0, 1] 3 | 4 | for i in range(2,n+1): 5 | fib.append(fib[i-1] + fib[i-2]) 6 | 7 | return fib[n] 8 | 9 | 10 | for i in range(101): 11 | print(f"fib({i})={fibonacci(i)}") -------------------------------------------------------------------------------- /lesson12/exercise05.py: -------------------------------------------------------------------------------- 1 | def euclid(a,b): 2 | if a==b: 3 | return a 4 | elif ab 7 | return euclid(a-b,b) 8 | 9 | 10 | print(euclid(255,155)) -------------------------------------------------------------------------------- /lesson12/exercise06.py: -------------------------------------------------------------------------------- 1 | def print_list(l): 2 | if len(l) > 0: 3 | print(l[0], end=" ") 4 | print_list(l[1:]) 5 | 6 | 7 | def print_list2(l): 8 | if len(l) > 0: 9 | print_list2(l[1:]) 10 | print(l[0], end=" ") 11 | 12 | 13 | print_list([1,2,3]) 14 | print("") 15 | print_list2([1,2,3]) -------------------------------------------------------------------------------- /lesson12/exercise07.py: -------------------------------------------------------------------------------- 1 | def C(n, k): 2 | if n == k: 3 | return 1 4 | elif k == 1: 5 | return n 6 | else: 7 | return C(n-1, k-1) + C(n-1, k) 8 | 9 | 10 | print(C(49, 6)) -------------------------------------------------------------------------------- /lesson12/exercise08.py: -------------------------------------------------------------------------------- 1 | def binary_search(array, x, start, finish): 2 | middle = (start + finish) // 2 3 | 4 | if start<=finish: 5 | if x == array[middle]: 6 | return middle 7 | elif x < array[middle]: 8 | return binary_search(array, x, start, middle-1) 9 | else: # x > array[middle] 10 | return binary_search(array, x, middle+1, finish) 11 | else: 12 | return -1 13 | 14 | 15 | my_list = [i*i for i in range(10)] 16 | print(my_list) 17 | print(binary_search(my_list, 0, 0, len(my_list)-1)) -------------------------------------------------------------------------------- /lesson12/exercise09.py: -------------------------------------------------------------------------------- 1 | def binary_search(array, x): 2 | start = 0 3 | finish = len(array)-1 4 | 5 | while start <= finish: 6 | middle = (start + finish) // 2 7 | if array[middle] == x: 8 | return middle 9 | elif array[middle] > x: 10 | finish = middle - 1 11 | else: 12 | start = middle + 1 13 | else: 14 | return -1 15 | 16 | 17 | 18 | my_list = [i*i for i in range(10)] 19 | print(my_list) 20 | print(binary_search(my_list, 10)) -------------------------------------------------------------------------------- /lesson12/exercise10.py: -------------------------------------------------------------------------------- 1 | def insertion_sort(array): 2 | for i in range(1, len(array)): 3 | for j in range(i,0,-1): 4 | if array[j] < array[j-1]: 5 | array[j], array[j-1] = array[j-1], array[j] 6 | else: 7 | break 8 | 9 | 10 | array = [9, 2, 4, 7, 1, 8, 6, 3] 11 | print(array) 12 | insertion_sort(array) 13 | print(array) -------------------------------------------------------------------------------- /lesson12/exercise11.py: -------------------------------------------------------------------------------- 1 | def insertion_sort(array): 2 | for i in range(1, len(array)): 3 | for j in range(i,0,-1): 4 | if array[j] > array[j-1]: 5 | array[j], array[j-1] = array[j-1], array[j] 6 | else: 7 | break 8 | 9 | 10 | array = [9, 2, 4, 7, 1, 8, 6, 3] 11 | print(array) 12 | insertion_sort(array) 13 | print(array) -------------------------------------------------------------------------------- /lesson12/factorial.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n == 1: 3 | return 1 4 | else: 5 | return n*factorial(n-1) 6 | 7 | for i in range(1,11): 8 | print(f"factorial({i})={factorial(i)} ") -------------------------------------------------------------------------------- /lesson12/factorial_print.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | print((5-n) * "\t" + "CALL factorial(" + str(n) + ")") 3 | if n == 1: 4 | print((5-n+1) * "\t" + "return 1") 5 | return 1 6 | else: 7 | print((5 - n+1) * "\t" + "return " + str(n) + "*factorial(" + str(n-1) + ")") 8 | fact = factorial(n-1) 9 | res = n*fact 10 | print((5 - n+1) * "\t" + "return " + str(n) + "*" + str(fact) ) 11 | return res 12 | 13 | 14 | factorial(5) -------------------------------------------------------------------------------- /lesson12/fibonacci.print.py: -------------------------------------------------------------------------------- 1 | N = 25 2 | calls = 0 3 | 4 | def fibonacci(n): 5 | global calls 6 | calls += 1 7 | 8 | if n == 0: 9 | print(N * "\t" + "return 0") 10 | return 0 11 | elif n == 1: 12 | print(N * "\t" + "return 1") 13 | return 1 14 | else: 15 | print((N - n + 1) * "\t" + "CALL fibonacci(" + str(n - 1) + ")") 16 | prevprev = fibonacci(n-1) 17 | print((N - n + 1) * "\t" + "CALL fibonacci(" + str(n - 2) + ")") 18 | prev = fibonacci(n-2) 19 | print((N - n + 1) * "\t" + "return " + str(prev) + " + " + str(prevprev)) 20 | return prevprev + prev 21 | 22 | fibonacci(N) 23 | print(f"calls = {calls}") 24 | -------------------------------------------------------------------------------- /lesson12/fibonacci.py: -------------------------------------------------------------------------------- 1 | # fibonacci.py 2 | def fibonacci(n): 3 | if n == 0: 4 | return 0 5 | elif n == 1: 6 | return 1 7 | else: 8 | return fibonacci(n - 1) + fibonacci(n - 2) 9 | 10 | 11 | for i in range(11): 12 | print(f"fibonacci({i})={fibonacci(i)}") 13 | -------------------------------------------------------------------------------- /lesson12/func.call.py: -------------------------------------------------------------------------------- 1 | def square(x): 2 | return x*x 3 | 4 | 5 | def cube(x): 6 | return x**3 7 | 8 | 9 | def f(x): 10 | return 3*cube(x)+4*square(x)+x+1 11 | 12 | 13 | print(" x : ", end="") 14 | for i in range(10): 15 | print("| " + str(i).center(6), end="") 16 | print("|\nf(x): ", end="") 17 | for i in range(10): 18 | print("| " + str(f(i)).center(6), end="") 19 | print("|") -------------------------------------------------------------------------------- /lesson12/python12.recursion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson12/python12.recursion.pdf -------------------------------------------------------------------------------- /lesson12/swap.py: -------------------------------------------------------------------------------- 1 | def swap(a,b): 2 | tmp = a 3 | a = b 4 | b = tmp 5 | return a,b 6 | 7 | 8 | a = 3 9 | b = 4 10 | print(f"a={a}, b={b}") 11 | print(type(swap(a, b))) 12 | a, b = swap(a, b) 13 | print(f"a={a}, b={b}") 14 | 15 | a = 3 16 | b = 4 17 | a,b = (b,a) 18 | print(f"a={a}, b={b}") 19 | 20 | a = 3 21 | b = 4 22 | a,b = b,a 23 | print(f"a={a}, b={b}") 24 | -------------------------------------------------------------------------------- /lesson13/args.py: -------------------------------------------------------------------------------- 1 | def my_sum(*numbers): 2 | print(numbers) 3 | s = 0 4 | for number in numbers: 5 | print(number) 6 | s += int(number) 7 | return s 8 | 9 | print(f"sum={my_sum(1,2,3,4,5)}") 10 | -------------------------------------------------------------------------------- /lesson13/default.value.py: -------------------------------------------------------------------------------- 1 | def print_name(first_name, surname, details=False): 2 | if details: 3 | print("First Name: " + first_name) 4 | print("Surname: " + surname) 5 | else: 6 | print(f"{first_name} {surname}") 7 | 8 | 9 | print_name("Charles", "Kane") 10 | print_name("Charles", "Kane", True) -------------------------------------------------------------------------------- /lesson13/default.values.keyword.arguments.py: -------------------------------------------------------------------------------- 1 | # default.values.keyword.arguments.py 2 | def func(par1, par2 =0, par3 = 0, par4 = 0): 3 | print(f"par1={par1} par2={par2} par3={par3} par4={par4}") 4 | 5 | func(1, par3="5") 6 | -------------------------------------------------------------------------------- /lesson13/default.values.order.py: -------------------------------------------------------------------------------- 1 | # default.values.order.py 2 | def func(par1, par2, par3 = 0, par4 = 0): 3 | print(f"par1={par1} par2={par2} par3={par3} par4={par4}") 4 | 5 | func(1,2,3,4) 6 | func(1,2,3) 7 | func(1,2) -------------------------------------------------------------------------------- /lesson13/exercise01.py: -------------------------------------------------------------------------------- 1 | def f(x, y, z): 2 | return 2 * x**3 + 3 * x**2 * y + y ** 2 * z + 1 3 | 4 | 5 | print(f(5, 2, 3)) 6 | print(f(y=2,z=3,x=5)) 7 | print(f(5,z=3,y=2)) 8 | print(f(5,2,z=3)) -------------------------------------------------------------------------------- /lesson13/exercise02.py: -------------------------------------------------------------------------------- 1 | def print_name(first_name, surname, details=False, second_name="", second_surname=""): 2 | if details: 3 | if second_name != "": 4 | print("First Name: " + first_name + "-" + second_name) 5 | else: 6 | print("First Name: " + first_name) 7 | 8 | if second_surname != "": 9 | print("Surname : " + surname + " " + second_surname) 10 | else: 11 | print("Surname : " + surname) 12 | else: 13 | if second_name != "": 14 | print(f"{first_name}-{second_name} {surname} {second_surname}") 15 | else: 16 | print(f"{first_name} {surname} {second_surname}") 17 | 18 | 19 | print_name("Charles", "Kane", second_name="Bob") 20 | print_name("Charles", "Kane", second_surname="Foster") 21 | print_name("Charles", "Kane", second_surname="Foster", second_name="Bob", details=True) -------------------------------------------------------------------------------- /lesson13/exercise03.py: -------------------------------------------------------------------------------- 1 | def float_average(*numbers): 2 | s = 0 3 | for number in numbers: 4 | s += number 5 | return s/len(numbers) 6 | 7 | 8 | print(float_average(2.1,4.2,5.9,7.8)) -------------------------------------------------------------------------------- /lesson13/exercise04.py: -------------------------------------------------------------------------------- 1 | def print_details(*args, **kwargs): 2 | print(kwargs) 3 | s = 0 4 | for number in args: 5 | s += number 6 | average = s/len(args) 7 | print(average) 8 | 9 | dictionary = {} 10 | for key, value in kwargs.items(): 11 | if value in dictionary: 12 | dictionary[value] += [key] 13 | else: 14 | dictionary[value] = [key] 15 | 16 | print(dictionary) 17 | 18 | 19 | 20 | print_details(5,8,13,Banner="Discrete Mathematics", Kane="Discrete Mathematics", Stark="Programming") 21 | -------------------------------------------------------------------------------- /lesson13/exercise05.py: -------------------------------------------------------------------------------- 1 | def selection_sort(array): 2 | for i in range(0, len(array)): 3 | 4 | pos = i 5 | for j in range(i+1, len(array)): 6 | if array[j] < array[pos]: 7 | pos = j 8 | 9 | array[i], array[pos] = array[pos], array[i] 10 | 11 | 12 | array = [1, 5, 2 , 8, 4, 7, 3, 9, 6] 13 | selection_sort(array) 14 | print(array) 15 | -------------------------------------------------------------------------------- /lesson13/exercise06.py: -------------------------------------------------------------------------------- 1 | def selection_sort(array, reverse=False): 2 | if reverse: 3 | for i in range(0, len(array)): 4 | pos = i 5 | for j in range(i+1, len(array)): 6 | if array[j] > array[pos]: 7 | pos = j 8 | array[i], array[pos] = array[pos], array[i] 9 | else: 10 | for i in range(0, len(array)): 11 | pos = i 12 | for j in range(i + 1, len(array)): 13 | if array[j] < array[pos]: 14 | pos = j 15 | array[i], array[pos] = array[pos], array[i] 16 | 17 | 18 | array = [1, 5, 2 , 8, 4, 7, 3, 9, 6] 19 | selection_sort(array) 20 | print(array) 21 | -------------------------------------------------------------------------------- /lesson13/keyword.arguments.py: -------------------------------------------------------------------------------- 1 | def print_full_name(first_name, surname, fathers_name): 2 | print(f"{first_name} {fathers_name[0]}. {surname}") 3 | 4 | 5 | print_full_name("Jack", "Bauer", "Philip") 6 | print_full_name("Jack", fathers_name="Philip", surname="Bauer") -------------------------------------------------------------------------------- /lesson13/kwargs.py: -------------------------------------------------------------------------------- 1 | def create_person(**kwargs): 2 | print(kwargs) 3 | return kwargs 4 | 5 | create_person(name="Jim", occupation="teacher") 6 | create_person(name="Bruce", ability="fly") -------------------------------------------------------------------------------- /lesson13/positional.after.keyword.args.py: -------------------------------------------------------------------------------- 1 | # positional.after.keyword.args.py 2 | def print_full_name(first_name, surname, fathers_name): 3 | print(f"{first_name} {fathers_name[0]}. {surname}") 4 | 5 | 6 | print_full_name(surname="Bauer", first_name="Jack", "Philip") -------------------------------------------------------------------------------- /lesson13/python13.functions.more.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson13/python13.functions.more.pdf -------------------------------------------------------------------------------- /lesson14/built.in.py: -------------------------------------------------------------------------------- 1 | def max(x,y): 2 | return x>y 3 | 4 | print(max(1,2,3)) -------------------------------------------------------------------------------- /lesson14/exercise01.py: -------------------------------------------------------------------------------- 1 | def binary_search(array, x): 2 | def bs(array, x, start, finish): 3 | middle = (start + finish) // 2 4 | 5 | if start<=finish: 6 | if x == array[middle]: 7 | return middle 8 | elif x < array[middle]: 9 | return bs(array, x, start, middle-1) 10 | else: # x > array[middle] 11 | return bs(array, x, middle+1, finish) 12 | else: 13 | return -1 14 | 15 | return bs(array, x, 0, len(array)-1) 16 | 17 | my_list = [i*i for i in range(10)] 18 | print(my_list) 19 | print(binary_search(my_list, 2)) -------------------------------------------------------------------------------- /lesson14/exercise02.py: -------------------------------------------------------------------------------- 1 | def factory(a,b,c): 2 | def polynomial(x): 3 | return a*x**2 + b*x + c 4 | 5 | return polynomial 6 | 7 | 8 | pol = factory(1,1,1) 9 | print(pol(1)) -------------------------------------------------------------------------------- /lesson14/exercise03.py: -------------------------------------------------------------------------------- 1 | def bubble_sort(array): 2 | n = len(array) 3 | for i in range(n): 4 | for j in range(n-1,i,-1): 5 | if array[j] < array[j-1]: 6 | array[j], array[j-1] = array[j-1], array[j] 7 | 8 | 9 | ar = [9,2,4,7,1,8,6,3] 10 | bubble_sort(ar) 11 | print(ar) -------------------------------------------------------------------------------- /lesson14/exercise04.py: -------------------------------------------------------------------------------- 1 | ar = [ 2 | [1,4,7,2,5], 3 | [5,8,5,3,1], 4 | [2,4,7,8,1], 5 | [1,3,4,2,6], 6 | [1,7,4,2,1] 7 | ] 8 | 9 | 10 | def bubble_sort(array): 11 | n = len(array) 12 | for i in range(n): 13 | for j in range(n-1,i,-1): 14 | if array[j] < array[j-1]: 15 | array[j], array[j-1] = array[j-1], array[j] 16 | 17 | 18 | def sort_by_rows(array): 19 | for row in array: 20 | bubble_sort(row) 21 | 22 | 23 | def sort_by_cols(array): 24 | for j in range(len(array)): 25 | col = [row[j] for row in array] 26 | bubble_sort(col) 27 | i = 0 28 | for row in array: 29 | row[j] = col[i] 30 | i += 1 31 | 32 | 33 | 34 | 35 | sort_by_cols(ar) 36 | print(ar) 37 | -------------------------------------------------------------------------------- /lesson14/fibonacci.py: -------------------------------------------------------------------------------- 1 | def fibonacci(n): 2 | def fib_rec(n): 3 | if n == 0: 4 | return 0 5 | elif n == 1: 6 | return 1 7 | else: 8 | return fib_rec(n - 1) + fib_rec(n - 2) 9 | 10 | if n < 0: 11 | return None 12 | else: 13 | return fib_rec(n) 14 | 15 | 16 | for i in range(11): 17 | print(f"fibonacci({i})={fibonacci(i)}") -------------------------------------------------------------------------------- /lesson14/function.data.type.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | print("Hello!") 3 | 4 | 5 | print(type(f)) 6 | -------------------------------------------------------------------------------- /lesson14/function.factory.py: -------------------------------------------------------------------------------- 1 | def factory_power(power): 2 | def nth_power(number): 3 | return number ** power 4 | 5 | return nth_power 6 | 7 | 8 | square = factory_power(2) 9 | print(square(4)) 10 | 11 | cube = factory_power(3) 12 | print(cube(4)) -------------------------------------------------------------------------------- /lesson14/import1.py: -------------------------------------------------------------------------------- 1 | import module 2 | 3 | module.func() -------------------------------------------------------------------------------- /lesson14/import2.py: -------------------------------------------------------------------------------- 1 | from module import func 2 | 3 | func() -------------------------------------------------------------------------------- /lesson14/import3.py: -------------------------------------------------------------------------------- 1 | from module import func as f 2 | 3 | f() -------------------------------------------------------------------------------- /lesson14/inner.function.py: -------------------------------------------------------------------------------- 1 | def f(): 2 | def g(): 3 | print("Hello!") 4 | g() 5 | g() 6 | 7 | f() -------------------------------------------------------------------------------- /lesson14/lesson14.exercise05/mod_teachers.py: -------------------------------------------------------------------------------- 1 | teachers = [ 2 | { 3 | "id": 1001, 4 | "name": "Severus", 5 | "surname": "Snape", 6 | }, 7 | { 8 | "id": 1002, 9 | "name": "Charles", 10 | "surname": "Xavier", 11 | }, 12 | { 13 | "id": 1003, 14 | "name": "Sergio", 15 | "surname": "Marquina", 16 | } 17 | ] 18 | 19 | 20 | def next_id(): 21 | ids = [] 22 | for t in teachers: 23 | ids.append(t["id"]) 24 | return max(ids) + 1 25 | 26 | 27 | def create_teacher(first_name, surname): 28 | for teacher in teachers: 29 | if teacher["name"] == first_name and teacher["surname"] == surname: 30 | print("Error. Teacher already exists! ") 31 | return None 32 | 33 | t = {"name": first_name, "surname": surname, "id": next_id()} 34 | teachers.append(t) 35 | return t 36 | 37 | 38 | def read_teacher(teacher_id): 39 | for teacher in teachers: 40 | if teacher_id == teacher["id"]: 41 | return teacher 42 | else: 43 | return None 44 | 45 | 46 | def update_teacher(teacher_id, key, value): 47 | for teacher in teachers: 48 | if teacher_id == teacher["id"]: 49 | teacher[key] = value 50 | return 51 | 52 | 53 | def delete_teacher(teacher_id): 54 | for i in range(len(teachers)-1): 55 | if teacher_id == teachers[i]["id"]: 56 | teachers.pop(i) 57 | return 58 | 59 | 60 | def print_teacher(teacher): 61 | print(f"Name : {teacher['name']}") 62 | print(f"Surname: {teacher['surname']}") 63 | print(f"id : {teacher['id']}") -------------------------------------------------------------------------------- /lesson14/module.py: -------------------------------------------------------------------------------- 1 | def func(): 2 | print("Hello!") -------------------------------------------------------------------------------- /lesson14/python14.modules.scope.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson14/python14.modules.scope.pdf -------------------------------------------------------------------------------- /lesson14/scope.py: -------------------------------------------------------------------------------- 1 | def outer(): 2 | x = 3 3 | 4 | def inner(): 5 | x = 4 6 | print("inner x= " + str(x)) 7 | 8 | inner() 9 | print("outer x= " + str(x)) 10 | 11 | 12 | x = 2 13 | print("global x= " + str(x)) 14 | outer() -------------------------------------------------------------------------------- /lesson15/binary.dat: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /lesson15/binary.file.py: -------------------------------------------------------------------------------- 1 | numbers = [1,2,3,4] 2 | with open("binary.dat", "wb") as f: 3 | for number in numbers: 4 | f.write(number.to_bytes(4, byteorder='big')) 5 | 6 | with open("binary.dat", "rb") as f: 7 | for i in range(len(numbers)): 8 | b = f.read(4) 9 | print(int.from_bytes(b, byteorder='big')) 10 | -------------------------------------------------------------------------------- /lesson15/bytes.py: -------------------------------------------------------------------------------- 1 | x = 15 2 | b = x.to_bytes(4, byteorder='big') 3 | print(b) 4 | n = int.from_bytes(b, byteorder='big') 5 | print(n) 6 | -------------------------------------------------------------------------------- /lesson15/exercise01.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | 4 | seed(datetime.now()) 5 | 6 | my_list = [randrange(0,100+1) for _ in range(1500)] 7 | 8 | with open("numbers.txt", "w") as f: 9 | for number in my_list: 10 | f.write(str(number) + "\n") 11 | -------------------------------------------------------------------------------- /lesson15/exercise02.py: -------------------------------------------------------------------------------- 1 | 2 | with open("test2.txt", "r") as f: 3 | lines = f.readlines() 4 | 5 | print(lines) 6 | 7 | for i in range(len(lines)): 8 | if lines[i][len(lines[i])-1] == "\n": 9 | lines[i] = lines[i][:len(lines[i])-1] 10 | 11 | print(lines) 12 | -------------------------------------------------------------------------------- /lesson15/exercise03.py: -------------------------------------------------------------------------------- 1 | def copy_file(filename1, filename2): 2 | with open(filename2, "r") as f: 3 | contents = f.read() 4 | 5 | with open(filename1, "w") as f: 6 | f.write(contents) 7 | 8 | 9 | copy_file("test3.txt", "test2.txt") -------------------------------------------------------------------------------- /lesson15/exercise04.py: -------------------------------------------------------------------------------- 1 | with open("overwrite.txt", "w") as f: 2 | f.write("Sample string") 3 | -------------------------------------------------------------------------------- /lesson15/exercise05.py: -------------------------------------------------------------------------------- 1 | with open("overwrite.txt", "a") as f: 2 | f.write("Sample string") 3 | -------------------------------------------------------------------------------- /lesson15/exercise06.py: -------------------------------------------------------------------------------- 1 | with open("lear.txt") as f: 2 | lines = f.readlines() 3 | 4 | for i in range(len(lines)): 5 | if lines[i].isupper(): 6 | lines[i] = "\n" + lines[i] + "\n" 7 | else: 8 | lines[i] = "\t" + lines[i] 9 | 10 | with open("lear2.txt", "w") as f: 11 | for line in lines: 12 | f.write(line) 13 | -------------------------------------------------------------------------------- /lesson15/exercise07.py: -------------------------------------------------------------------------------- 1 | def merge(filename1, filename2, filename): 2 | with open(filename, "w") as f: 3 | with open(filename1) as g: 4 | contents = g.read() 5 | f.write(contents) 6 | 7 | with open(filename2) as g: 8 | contents = g.read() 9 | f.write(contents) 10 | 11 | 12 | merge("test.txt", "test2.txt", "test4.txt") 13 | -------------------------------------------------------------------------------- /lesson15/exercise08.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | try: 4 | with open("reminders.json") as f: 5 | reminders = json.load(f) 6 | except FileNotFoundError: 7 | reminders = [] 8 | 9 | 10 | while True: 11 | print("1-add reminder") 12 | print("2-remove reminder") 13 | print("3-print reminders") 14 | print("4-exit") 15 | choice = int(input("Pick one: ")) 16 | 17 | if choice==1: 18 | reminder = input("New reminder: ") 19 | reminders.append(reminder) 20 | with open("reminders.json", "w") as f: 21 | json.dump(reminders, f) 22 | elif choice==2: 23 | reminder_index = int(input("Give index of the reminder: ")) 24 | reminders.pop(reminder_index) 25 | with open("reminders.json", "w") as f: 26 | json.dump(reminders, f) 27 | elif choice==3: 28 | for i in range(len(reminders)): 29 | print(f"{i}. {reminders[i]}") 30 | elif choice==4: 31 | break -------------------------------------------------------------------------------- /lesson15/exercise09.admin.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | try: 4 | with open("users.json") as f: 5 | users = json.load(f) 6 | except FileNotFoundError: 7 | users = [] 8 | 9 | while True: 10 | print("1-new user") 11 | print("2-exit") 12 | choice = int(input("Pick one: ")) 13 | 14 | if choice == 1: 15 | user = {} 16 | user["full_name"] = input("Give full name: ") 17 | user["username"] = input("Give username: ") 18 | user["password"] = input("Give password: ") 19 | user["role"] = input("Give role (admin, user): ") 20 | users.append(user) 21 | elif choice == 2: 22 | with open("users.json", "w") as f: 23 | json.dump(users, f) 24 | break 25 | -------------------------------------------------------------------------------- /lesson15/exercise09.user.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | try: 4 | with open("users.json") as f: 5 | users = json.load(f) 6 | except FileNotFoundError: 7 | users = [] 8 | 9 | 10 | username = input("Give username: ") 11 | password = input("Give password: ") 12 | 13 | for user in users: 14 | if username == user["username"] and password == user["password"]: 15 | if user["role"] == "admin": 16 | print("Welcome Admin") 17 | elif user["role"] == "user": 18 | print("Welcome " + user["full_name"]) 19 | break 20 | else: 21 | print("Wrong username or password!") 22 | -------------------------------------------------------------------------------- /lesson15/exercise10.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | 3 | def merge_sort(array): 4 | def merge(array, start, finish): 5 | C = [] 6 | 7 | middle = (start+finish)//2 8 | i=start 9 | n=middle 10 | j=middle+1 11 | m=finish 12 | 13 | while i<=n and j<=m: 14 | if array[i] < array[j]: 15 | C += [array[i]] 16 | i += 1 17 | else: 18 | C += [array[j]] 19 | j +=1 20 | 21 | if i == n+1: 22 | C += array[j:m+1] 23 | else: 24 | C += array[i:n+1] 25 | 26 | i = start 27 | j = 0 28 | while i<=finish: 29 | array[i] = C[j] 30 | i+=1 31 | j+=1 32 | 33 | def merge_sort_rec(array,start, finish): 34 | if start == finish: 35 | return 36 | elif start == finish-1: 37 | if array[start] > array[finish]: 38 | array[start], array[finish] = array[finish], array[start] 39 | else: 40 | middle = (start+finish)//2 41 | merge_sort_rec(array,start,middle) 42 | merge_sort_rec(array,middle+1,finish) 43 | merge(array,start,finish) 44 | 45 | merge_sort_rec(array,0,len(array)-1) 46 | 47 | ar = [randrange(100) for _ in range(50)] 48 | merge_sort(ar) 49 | print(ar) 50 | 51 | -------------------------------------------------------------------------------- /lesson15/file.open.close.py: -------------------------------------------------------------------------------- 1 | f = open("test.txt", "w") 2 | f.write("My text goes here") 3 | f.close() 4 | -------------------------------------------------------------------------------- /lesson15/for.readlines.py: -------------------------------------------------------------------------------- 1 | # for.readlines.py 2 | with open("test2.txt", "r") as f: 3 | for line in f: 4 | print(line) 5 | -------------------------------------------------------------------------------- /lesson15/json.dump.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | numbers = [1,2,3,4,5,6,7] 4 | 5 | with open("numbers.json", "w") as f: 6 | json.dump(numbers, f) 7 | -------------------------------------------------------------------------------- /lesson15/json.load.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | with open("numbers.json", "r") as f: 4 | numbers = json.load(f) 5 | 6 | print(numbers) -------------------------------------------------------------------------------- /lesson15/json.multiple.data.py: -------------------------------------------------------------------------------- 1 | import json 2 | from random import randrange 3 | 4 | with open("random.json.data.json", "w") as f: 5 | for _ in range(4): 6 | if randrange(3) == 0: 7 | json.dump([1,2,3], f) 8 | elif randrange(3) == 1: 9 | json.dump({"a":1, "b":2, "c":3}, f) 10 | else: 11 | json.dump(4, f) 12 | f.write("\n") 13 | 14 | with open("random.json.data.json", "r") as f: 15 | for lineObj in f: 16 | print(json.loads(lineObj)) 17 | -------------------------------------------------------------------------------- /lesson15/lesson15.exercise12/mod_teachers.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | teachers = [] 4 | 5 | def init_teachers_data(): 6 | global teachers 7 | try: 8 | with open("teachers.json") as f: 9 | teachers = json.load(f) 10 | except FileNotFoundError: 11 | teachers = [] 12 | 13 | 14 | def save_teachers_data(): 15 | with open("teachers.json", "w") as f: 16 | json.dump(teachers, f) 17 | 18 | 19 | def next_id(): 20 | if not teachers: 21 | return 1001 22 | else: 23 | ids = [] 24 | for t in teachers: 25 | ids.append(t["id"]) 26 | return max(ids) + 1 27 | 28 | 29 | def create_teacher(first_name, surname): 30 | for teacher in teachers: 31 | if teacher["name"] == first_name and teacher["surname"] == surname: 32 | print("Error. Teacher already exists! ") 33 | return None 34 | 35 | t = {"name": first_name, "surname": surname, "id": next_id()} 36 | teachers.append(t) 37 | return t 38 | 39 | 40 | def read_teacher(teacher_id): 41 | for teacher in teachers: 42 | if teacher_id == teacher["id"]: 43 | return teacher 44 | else: 45 | return None 46 | 47 | 48 | def update_teacher(teacher_id, key, value): 49 | for teacher in teachers: 50 | if teacher_id == teacher["id"]: 51 | teacher[key] = value 52 | return 53 | 54 | 55 | def delete_teacher(teacher_id): 56 | for i in range(len(teachers)-1): 57 | if teacher_id == teachers[i]["id"]: 58 | teachers.pop(i) 59 | return 60 | 61 | 62 | def print_teacher(teacher): 63 | print(f"Name : {teacher['name']}") 64 | print(f"Surname: {teacher['surname']}") 65 | print(f"id : {teacher['id']}") -------------------------------------------------------------------------------- /lesson15/lesson15.exercise12/pupils.json: -------------------------------------------------------------------------------- 1 | [{"name": "John", "surname": "Wick", "fathers_name": "Bob", "age": 45, "class": 1, "id_number": "02938409382", "id": 1001}, {"name": "Tony", "surname": "Stark", "fathers_name": "Bob", "age": 38, "class": 7, "id_number": "9238749237", "id": 1002}] -------------------------------------------------------------------------------- /lesson15/lesson15.exercise12/teachers.json: -------------------------------------------------------------------------------- 1 | [{"name": "Severus", "surname": "Snape", "id": 1001}, {"name": "Charles", "surname": "Xavier", "id": 1002}, {"name": "Sergio", "surname": "Marquina", "id": 1003}] -------------------------------------------------------------------------------- /lesson15/numbers.json: -------------------------------------------------------------------------------- 1 | [1, 2, 3, 4, 5, 6, 7] -------------------------------------------------------------------------------- /lesson15/overwrite.txt: -------------------------------------------------------------------------------- 1 | Sample string -------------------------------------------------------------------------------- /lesson15/python15.files.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson15/python15.files.pdf -------------------------------------------------------------------------------- /lesson15/random.access.py: -------------------------------------------------------------------------------- 1 | int_size = 4 2 | with open("binary.dat", "rb+") as f: 3 | while True: 4 | print(f"Current cursor position = {f.tell()}") 5 | print("1-read") 6 | print("2-write") 7 | print("3-exit") 8 | choice = int(input("Choose: ")) 9 | 10 | if choice == 1: 11 | pos = int(input("Give position(0-3) to read: ")) 12 | f.seek(int_size*pos, 0) 13 | b = f.read(int_size) 14 | print(f"value in pos {pos} = {int.from_bytes(b, byteorder='big')}") 15 | elif choice == 2: 16 | pos = int(input("Give position(0-3) to write: ")) 17 | f.seek(int_size*pos, 0) 18 | val = int(input("Give new value: ")) 19 | f.write(val.to_bytes(int_size, byteorder='big')) 20 | print(f"value in pos {pos} changed to {val}") 21 | else: 22 | break 23 | -------------------------------------------------------------------------------- /lesson15/random.json.data.json: -------------------------------------------------------------------------------- 1 | {"a": 1, "b": 2, "c": 3} 2 | [1, 2, 3] 3 | [1, 2, 3] 4 | {"a": 1, "b": 2, "c": 3} 5 | -------------------------------------------------------------------------------- /lesson15/read.py: -------------------------------------------------------------------------------- 1 | # read.py 2 | with open("test2.txt", "r") as f: 3 | contents = f.read() 4 | print(contents) 5 | -------------------------------------------------------------------------------- /lesson15/readlines.py: -------------------------------------------------------------------------------- 1 | # readlines.py 2 | with open("test2.txt", "r") as f: 3 | lines = f.readlines() 4 | print(lines) -------------------------------------------------------------------------------- /lesson15/reminders.json: -------------------------------------------------------------------------------- 1 | ["Make lesson 16"] -------------------------------------------------------------------------------- /lesson15/test.txt: -------------------------------------------------------------------------------- 1 | My text goes here -------------------------------------------------------------------------------- /lesson15/test2.txt: -------------------------------------------------------------------------------- 1 | My text goes here 2 | and another line -------------------------------------------------------------------------------- /lesson15/test3.txt: -------------------------------------------------------------------------------- 1 | My text goes here 2 | and another line -------------------------------------------------------------------------------- /lesson15/test4.txt: -------------------------------------------------------------------------------- 1 | My text goes hereMy text goes here 2 | and another line -------------------------------------------------------------------------------- /lesson15/users.json: -------------------------------------------------------------------------------- 1 | [{"full_name": "Bob Dole", "username": "bob", "password": "123456", "role": "admin"}, {"full_name": "Bill Clinton", "username": "bill", "password": "monica", "role": "user"}] -------------------------------------------------------------------------------- /lesson15/with.py: -------------------------------------------------------------------------------- 1 | with open("test2.txt", "w") as f: 2 | f.write("My text goes here\n") 3 | f.write("and another line") 4 | -------------------------------------------------------------------------------- /lesson16/building.py: -------------------------------------------------------------------------------- 1 | class Storey: 2 | def __init__(self): 3 | self.people = 0 4 | 5 | 6 | class Building: 7 | def __init__(self, number_storeys): 8 | self.storeys = [Storey() for _ in range(number_storeys)] 9 | 10 | def add_people(self, storey, people): 11 | self.storeys[storey].people += people 12 | 13 | def print_people(self): 14 | for i in range(len(self.storeys)): 15 | print(f"Storey {i}: {self.storeys[i].people} people") 16 | 17 | 18 | b = Building(3) 19 | b.add_people(0,2) 20 | b.print_people() -------------------------------------------------------------------------------- /lesson16/class.attribute.py: -------------------------------------------------------------------------------- 1 | # class.attribute.py 2 | class C: 3 | counter = 0 4 | 5 | def __init__(self): 6 | C.counter += 1 7 | 8 | 9 | o1 = C() 10 | o2 = C() 11 | print(C.counter, o1.counter, o2.counter) -------------------------------------------------------------------------------- /lesson16/class.members.py: -------------------------------------------------------------------------------- 1 | # class.members.py 2 | class Cow: 3 | def __init__(self, weight, hunger): 4 | self.weight = weight 5 | self.hunger = hunger 6 | 7 | 8 | molly = Cow(500, 10) 9 | print(type(molly)) 10 | print(molly.hunger) -------------------------------------------------------------------------------- /lesson16/class.methods.py: -------------------------------------------------------------------------------- 1 | # class.methods.py 2 | class Cow: 3 | def __init__(self, weight, hunger): 4 | self.weight = weight 5 | self.hunger = hunger 6 | 7 | def express(self): 8 | if self.hunger > 5: 9 | print("Moooooowwwwwwwwww") 10 | else: 11 | print("Mowww") 12 | 13 | 14 | molly = Cow(500, 10) 15 | molly.express() -------------------------------------------------------------------------------- /lesson16/del.py: -------------------------------------------------------------------------------- 1 | l = [1, 2, 3] 2 | del l[0] 3 | print(l) 4 | 5 | d = {1: 1, 2: 2} 6 | del d[1] 7 | print(d) 8 | 9 | 10 | class C: 11 | c = 0 12 | 13 | def __init__(self): 14 | C.c += 1 15 | 16 | def __del__(self): 17 | C.c -= 1 18 | 19 | 20 | o1 = C() 21 | o2 = C() 22 | print(o1.c) 23 | del o1 24 | print(o2.c) -------------------------------------------------------------------------------- /lesson16/empty.class.py: -------------------------------------------------------------------------------- 1 | class Empty: 2 | pass 3 | 4 | 5 | e = Empty() 6 | print(type(e)) -------------------------------------------------------------------------------- /lesson16/exercise01.py: -------------------------------------------------------------------------------- 1 | class Dog: 2 | def __init__(self, name, weight, breed): 3 | self.name = name 4 | self.weight = weight 5 | self.breed = breed 6 | 7 | 8 | piko = Dog("Piko", 10, "Terrier") 9 | lassie = Dog("Lassie", 30, "Colley") -------------------------------------------------------------------------------- /lesson16/exercise02.py: -------------------------------------------------------------------------------- 1 | class Philosopher: 2 | def __init__(self, full_name, era, books, school): 3 | self.full_name = full_name 4 | self.era = era 5 | self.books = books 6 | self.school = school 7 | self.disputed_books = [] 8 | 9 | 10 | plato = Philosopher("Plato", "Ancient Greece", ["The Republic", "Phaedo"], "Platonism") 11 | baruch = Philosopher("Baruch Spinoza", "Modern Netherlands", ["Ethics", "Political Treatise"], "Modernism") 12 | 13 | print(baruch.disputed_books) -------------------------------------------------------------------------------- /lesson16/exercise03.py: -------------------------------------------------------------------------------- 1 | class Dog: 2 | def __init__(self, name, weight, breed): 3 | self.name = name 4 | self.weight = weight 5 | self.breed = breed 6 | self.mood = 5 7 | 8 | def eat(self): 9 | self.mood += 1 10 | 11 | def bark(self): 12 | if self.mood > 5: 13 | print("Woof woof woof") 14 | else: 15 | print("Woof") 16 | 17 | def walk(self): 18 | self.mood += 1 19 | 20 | 21 | piko = Dog("Piko", 10, "Terrier") 22 | for _ in range(2): 23 | piko.bark() 24 | piko.walk() 25 | piko.bark() 26 | piko.eat() 27 | piko.bark() -------------------------------------------------------------------------------- /lesson16/exercise04.py: -------------------------------------------------------------------------------- 1 | class Point: 2 | def __init__(self, x, y): 3 | self.__x = x 4 | self.__y = y 5 | 6 | def set_x(self, x): 7 | self.__x = x 8 | 9 | def set_y(self, y): 10 | self.__y = y 11 | 12 | def get_x(self): 13 | return self.__x 14 | 15 | def get_y(self): 16 | return self.__y 17 | 18 | def f(x): 19 | return x**2 20 | 21 | 22 | points = [Point(i,f(i)) for i in range(1, 10)] 23 | 24 | 25 | for point in points: 26 | print(f"({point.get_x()},{point.get_y()})") -------------------------------------------------------------------------------- /lesson16/exercise05.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | 4 | 5 | class Student: 6 | def __init__(self, full_name): 7 | self.full_name = full_name 8 | self.grade = -1 9 | 10 | 11 | def grade_student(student): 12 | student.grade = randrange(0,11) 13 | 14 | 15 | def average(students): 16 | s = 0 17 | for student in students: 18 | s += student.grade 19 | 20 | print("Average: " + str(s/len(students))) 21 | 22 | seed(datetime.now()) 23 | 24 | names = ["Kirk Bowman", 25 | "Bear Byers", 26 | "Kurtis Macias", 27 | "Maximus Preece", 28 | "Cecily Ray", 29 | "Cade Parry", 30 | "Jordyn Pitts", 31 | "Liya Christian", 32 | "Ceri Orr", 33 | "Mekhi Hahn"] 34 | 35 | students = [Student(names[i]) for i in range(len(names))] 36 | 37 | for student in students: 38 | grade_student(student) 39 | 40 | average(students) -------------------------------------------------------------------------------- /lesson16/exercise06.py: -------------------------------------------------------------------------------- 1 | class Flat: 2 | def __init__(self): 3 | self.people = 0 4 | 5 | 6 | class Storey: 7 | def __init__(self, number_flats): 8 | self.flats = [Flat() for _ in range(number_flats)] 9 | 10 | 11 | class Building: 12 | def __init__(self, number_storeys, number_flats): 13 | self.storeys = [Storey(number_flats) for _ in range(number_storeys)] 14 | 15 | def add_people(self, storey, flat, people): 16 | self.storeys[storey].flats[flat].people += people 17 | 18 | def print_people(self): 19 | for i in range(len(self.storeys)): 20 | for j in range(len(self.storeys[i].flats)): 21 | print(f"Storey {i}, Flat {j}: {self.storeys[i].flats[j].people} people") 22 | 23 | 24 | b = Building(3,4) 25 | b.add_people(0,1,2) 26 | b.print_people() -------------------------------------------------------------------------------- /lesson16/exercise07.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | 4 | class Point: 5 | def __init__(self, x, y): 6 | self.x = x 7 | self.y = y 8 | 9 | def set_x(self, x): 10 | self.x = x 11 | 12 | def set_y(self, y): 13 | self.y = y 14 | 15 | def print(self): 16 | print(f"({self.x}, {self.y})") 17 | 18 | 19 | class Line: 20 | def __init__(self, point_A=None, point_B=None): 21 | if point_A is None: 22 | self.point_A = Point(0,0) 23 | else: 24 | self.point_A = point_A 25 | if point_B is None: 26 | self.point_B = Point(0,0) 27 | else: 28 | self.point_B = point_B 29 | 30 | def set_point_A(self, point_A): 31 | self.point_A = point_A 32 | 33 | def set_point_B(self, point_B): 34 | self.point_B = point_B 35 | 36 | def length(self): 37 | return sqrt((self.point_A.x - self.point_B.x)**2 + (self.point_A.y - self.point_B.y)**2) 38 | 39 | def print(self): 40 | self.point_A.print() 41 | self.point_B.print() 42 | 43 | 44 | l = Line() 45 | l.print() 46 | 47 | l.set_point_A(Point(1,1)) 48 | l.set_point_B(Point(4,5)) 49 | 50 | l.print() 51 | print(l.length()) 52 | -------------------------------------------------------------------------------- /lesson16/exercise08.py: -------------------------------------------------------------------------------- 1 | class Time: 2 | def __init__(self, hour, minute, second): 3 | self.hour = self.__validate(hour,0,23) 4 | self.minute = self.__validate(minute,0,59) 5 | self.second = self.__validate(second,0,59) 6 | 7 | def set_hour(self, hour): 8 | self.hour = self.__validate(hour,0,23) 9 | 10 | def set_minute(self, minute): 11 | self.minute = self.__validate(minute,0,59) 12 | 13 | def set_second(self, second): 14 | self.second = self.__validate(second,0,59) 15 | 16 | def __validate(self, val, low, upp): 17 | if low <= val <= upp: 18 | return val 19 | else: 20 | return 0 21 | 22 | def total_seconds(self): 23 | return self.hour * 3600 + self.minute * 60 + self.second 24 | 25 | def print(self): 26 | print(f"{str(self.hour).zfill(2)}:{str(self.minute).zfill(2)}:{str(self.second).zfill(2)}") 27 | 28 | def next_second(self): 29 | second = self.second + 1 30 | carry = second // 60 31 | second = second % 60 32 | 33 | minute = self.minute + carry 34 | carry = minute // 60 35 | minute = minute % 60 36 | 37 | hour = (self.hour + carry) % 24 38 | 39 | return Time(hour, minute, second) 40 | 41 | 42 | t = Time(1,45,110) 43 | t.print() 44 | 45 | t1 = Time(11,12,13) 46 | t1.next_second().print() 47 | 48 | t1 = Time(11,12,59) 49 | t1.next_second().print() 50 | 51 | t1 = Time(11,59,59) 52 | t1.next_second().print() 53 | 54 | t1 = Time(23,59,59) 55 | t1.next_second().print() -------------------------------------------------------------------------------- /lesson16/exercise09.py: -------------------------------------------------------------------------------- 1 | class Empty: 2 | def __init__(self): 3 | print(self) 4 | 5 | e = Empty() 6 | print(e) -------------------------------------------------------------------------------- /lesson16/exercise10.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def __init__(self, full_name, age, id_number): 3 | self.full_name = full_name 4 | self.age = age 5 | self.id_number = id_number 6 | 7 | 8 | class Account: 9 | def __init__(self, number, owner, amount): 10 | self.number = number 11 | self.owner = owner 12 | self.amount = amount 13 | 14 | def transfer_to(self, account, amount): 15 | if amount <= self.amount: 16 | self.amount -= amount 17 | account.amount += amount 18 | print(self.owner.full_name + " transfered " + str(amount) + " to " + account.owner.full_name) 19 | else: 20 | print("Not enough credit!") 21 | 22 | 23 | p1 = Person("Bob Hope", "dead", "13219398") 24 | p2 = Person("Donald Trump", 75, "02139484") 25 | a1 = Account("132489480328", p1, 123878.23) 26 | a2 = Account("213094809328", p2, 12388902993.99) 27 | a1.transfer_to(a2,100000) -------------------------------------------------------------------------------- /lesson16/exercise11.py: -------------------------------------------------------------------------------- 1 | class Author: 2 | def __init__(self, full_name, email): 3 | self.full_name = full_name 4 | self.email = email 5 | 6 | 7 | class Book: 8 | def __init__(self, title, authors, price, copies): 9 | self.title = title 10 | self.authors = authors 11 | self.price = price 12 | self.copies = copies 13 | 14 | def print_full_title(self): 15 | authors_names = [author.full_name for author in self.authors] 16 | print("'" + self.title + "' by ", end="") 17 | print(", ".join(authors_names)) 18 | 19 | def add_author(self, author): 20 | self.authors.append(author) 21 | 22 | 23 | p1 = Author("Bob Paterson", "bob@pcookbook.com") 24 | p2 = Author("James McConan", "james@pcookbook.com") 25 | b = Book("The Ultimate Python Cookbook", [p1,p2], 18.25, 43) 26 | b.print_full_title() 27 | 28 | b.add_author(Author("Tom Rossbow", "tom@pcookbook.com")) 29 | b.print_full_title() -------------------------------------------------------------------------------- /lesson16/exercise12.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | 3 | def quick_sort(array): 4 | def partition(array, start, finish): 5 | pivot = array[start] 6 | 7 | less = [elem for elem in array[start+1:finish+1] if elem < pivot] 8 | greaterequal = [elem for elem in array[start+1:finish + 1] if elem >= pivot] 9 | new_array = less + [pivot] + greaterequal 10 | 11 | for i in range(start, finish+1): 12 | array[i]=new_array.pop(0) 13 | 14 | return start + len(less) 15 | 16 | def quick_sort_rec(array, start, finish): 17 | if start < finish: 18 | pos = partition(array,start,finish) 19 | quick_sort_rec(array,start,pos-1) 20 | quick_sort_rec(array,pos+1,finish) 21 | 22 | quick_sort_rec(array,0,len(array)-1) 23 | 24 | ar = [randrange(100) for _ in range(20)] 25 | print(ar) 26 | quick_sort(ar) 27 | print(ar) -------------------------------------------------------------------------------- /lesson16/exercise13.py: -------------------------------------------------------------------------------- 1 | from stack import Stack 2 | 3 | def binary(n): 4 | st = Stack() 5 | 6 | while n > 0: 7 | st.push(n % 2) 8 | n = n//2 9 | 10 | res = [] 11 | while st.length()>0: 12 | res.append(str(st.pop())) 13 | 14 | return "".join(res) 15 | 16 | 17 | for i in range(300): 18 | print(binary(i)) -------------------------------------------------------------------------------- /lesson16/is.py: -------------------------------------------------------------------------------- 1 | class MyClass: 2 | def __init__(self, x): 3 | self.x = x 4 | 5 | 6 | o1 = MyClass(2) 7 | o2 = MyClass(3) 8 | o3 = o2 9 | 10 | print(id(o1), id(o2), id(o3)) 11 | print(o1 is o2) 12 | print(o2 is o3) 13 | print("======") 14 | 15 | print([1,2] is [1,2]) 16 | print([1,2] == [1,2]) 17 | 18 | o1 = MyClass(1) 19 | o2 = MyClass(1) 20 | print(o1 is o2) 21 | print(o1 == o2) # doesn't work right (we 'll fix it later) 22 | print("======") 23 | 24 | print(isinstance([1,2],list)) 25 | print(isinstance(o1,MyClass)) 26 | print(isinstance(o1,float)) 27 | print(isinstance([1,2],(list,int))) 28 | -------------------------------------------------------------------------------- /lesson16/lesson16.exercise14/character.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | 3 | class Character: 4 | def __init__(self, character_name, attack_speed=2, delay=0): 5 | self.character_name = character_name 6 | self.health = 100 7 | self.attack_speed = attack_speed 8 | self.delay = delay 9 | 10 | def attack(self): 11 | self.delay = 5 - self.attack_speed 12 | return randrange(3, 11) 13 | 14 | def is_dead(self): 15 | return self.health <= 0 16 | 17 | def end_round(self): 18 | self.health = self.health+1 if self.health+1<=100 else 100 19 | self.delay -= 1 20 | 21 | def print(self): 22 | print(f"{self.character_name} H:{self.health} D:{self.delay}") 23 | -------------------------------------------------------------------------------- /lesson16/lesson16.exercise14/main.py: -------------------------------------------------------------------------------- 1 | from character import Character 2 | from arena import Arena 3 | from random import randrange, seed 4 | from datetime import datetime 5 | 6 | 7 | def main(): 8 | seed(datetime.now()) 9 | 10 | orcs = [Character("Orc-" + str(i+1),2,randrange(4)) for i in range(5)] 11 | night_elves = [Character("Night-Elf-" + str(i + 1), 3, randrange(3)) for i in range(3)] 12 | 13 | a = Arena(orcs, night_elves) 14 | a.play() 15 | 16 | 17 | main() -------------------------------------------------------------------------------- /lesson16/lesson16.exercise15/pupils.json: -------------------------------------------------------------------------------- 1 | [{"name": "John", "surname": "Wick", "fathers_name": "Bob", "age": 45, "class": 1, "id_number": "02938409382", "id": 1001}, {"name": "Tony", "surname": "Stark", "fathers_name": "Bob", "age": 38, "class": 7, "id_number": "9238749237", "id": 1002}] -------------------------------------------------------------------------------- /lesson16/lesson16.exercise15/teacher.py: -------------------------------------------------------------------------------- 1 | class Teacher: 2 | def __init__(self, first_name="", surname="", teacher_id=-1): 3 | self.first_name = first_name 4 | self.surname = surname 5 | self.teacher_id = teacher_id 6 | 7 | def from_dict(self, teacher_dict): 8 | self.first_name = teacher_dict["first_name"] 9 | self.surname = teacher_dict["surname"] 10 | self.teacher_id = teacher_dict["teacher_id"] 11 | 12 | def to_dict(self): 13 | teacher_dict = {"first_name": self.first_name, 14 | "surname": self.surname, 15 | "teacher_id":self.teacher_id} 16 | return teacher_dict 17 | 18 | def print_teacher(self): 19 | print(f"Name : {self.first_name}") 20 | print(f"Surname: {self.surname}") 21 | print(f"id : {self.teacher_id}") -------------------------------------------------------------------------------- /lesson16/lesson16.exercise15/teachers.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "Severus", "surname": "Snape", "teacher_id": 1001}, {"first_name": "Charles", "surname": "Xavier", "teacher_id": 1002}, {"first_name": "Sergio", "surname": "Marquina", "teacher_id": 1003}] -------------------------------------------------------------------------------- /lesson16/private.py: -------------------------------------------------------------------------------- 1 | # private.py 2 | class Cow: 3 | def __init__(self, weight, hunger): 4 | self.weight = weight 5 | self.__hunger = hunger 6 | 7 | def express(self): 8 | if self.__hunger > 5: 9 | print("Moooooowwwwwwwwww") 10 | else: 11 | print("Mowww") 12 | 13 | 14 | molly = Cow(100, 5) 15 | print(molly.__hunger) 16 | print(molly.weight) 17 | molly.express() 18 | -------------------------------------------------------------------------------- /lesson16/public.py: -------------------------------------------------------------------------------- 1 | # public.py 2 | class Cow: 3 | def __init__(self, weight, hunger): 4 | self.weight = weight 5 | self.hunger = hunger 6 | 7 | def express(self): 8 | if self.hunger > 5: 9 | print("Moooooowwwwwwwwww") 10 | else: 11 | print("Mowww") 12 | 13 | 14 | molly = Cow(100, 5) 15 | print(molly.hunger) 16 | print(molly.weight) 17 | molly.express() -------------------------------------------------------------------------------- /lesson16/python16.oop.classes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson16/python16.oop.classes.pdf -------------------------------------------------------------------------------- /lesson16/stack.py: -------------------------------------------------------------------------------- 1 | class Stack: 2 | def __init__(self): 3 | self.array = [] 4 | 5 | def push(self,elem): 6 | self.array.append(elem) 7 | 8 | def pop(self): 9 | if not self.array: 10 | return None 11 | else: 12 | return self.array.pop() 13 | 14 | def length(self): 15 | return len(self.array) 16 | -------------------------------------------------------------------------------- /lesson17/comparison.py: -------------------------------------------------------------------------------- 1 | class Time: 2 | def __init__(self, hour, minute, second): 3 | self.hour = hour 4 | self.minute = minute 5 | self.second = second 6 | 7 | def __str__(self): 8 | return f"{str(self.hour).zfill(2)}:" \ 9 | f"{str(self.minute).zfill(2)}:" \ 10 | f"{str(self.second).zfill(2)}" 11 | 12 | def __gt__(self, other): 13 | if self.hour > other.hour: 14 | return True 15 | elif self.hour == other.hour: 16 | if self.minute > other.minute: 17 | return True 18 | elif self.minute == other.minute: 19 | if self.second > other.second: 20 | return True 21 | return False 22 | 23 | t = Time(11,2,3) 24 | t2 = Time(11,11,1) 25 | print(f"{t} > {t2}: {t>t2}") 26 | -------------------------------------------------------------------------------- /lesson17/comparison2.py: -------------------------------------------------------------------------------- 1 | class Time: 2 | def __init__(self, hour, minute, second): 3 | self.hour = hour 4 | self.minute = minute 5 | self.second = second 6 | 7 | def __str__(self): 8 | return f"{str(self.hour).zfill(2)}:" \ 9 | f"{str(self.minute).zfill(2)}:" \ 10 | f"{str(self.second).zfill(2)}" 11 | 12 | def __gt__(self, other): 13 | if self.hour > other.hour: 14 | return True 15 | elif self.hour == other.hour: 16 | if self.minute > other.minute: 17 | return True 18 | elif self.minute == other.minute: 19 | if self.second > other.second: 20 | return True 21 | return False 22 | 23 | def __ge__(self, other): 24 | if self.hour > other.hour: 25 | return True 26 | elif self.hour == other.hour: 27 | if self.minute > other.minute: 28 | return True 29 | elif self.minute == other.minute: 30 | if self.second >= other.second: 31 | return True 32 | return False 33 | 34 | def __eq__(self, other): 35 | if self.hour == other.hour and self.minute == other.minute and self.second == other.second: 36 | return True 37 | return False 38 | 39 | 40 | t = Time(11,1,2) 41 | t2 = Time(11,1,1) 42 | print(f"{t} > {t2}: {t>t2}") 43 | print(f"{t} < {t2}: {t= {t2}: {t>=t2}") 45 | print(f"{t} <= {t2}: {t<=t2}") 46 | print(f"{t} == {t2}: {t==t2}") 47 | print(f"{t} != {t2}: {t!=t2}") 48 | -------------------------------------------------------------------------------- /lesson17/del.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self, n): 3 | self.name = n 4 | def __del__(self): 5 | print(self.name + " destroyed") 6 | del self 7 | 8 | 9 | def f(): 10 | t = A("t") 11 | 12 | f() 13 | 14 | x = A("x") 15 | del x 16 | y = A("y") 17 | z = y 18 | print(z, y) 19 | ar = [y, 2] 20 | del y 21 | print(ar,ar[0]) -------------------------------------------------------------------------------- /lesson17/exercise02.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | 4 | 5 | class Student: 6 | def __init__(self, full_name): 7 | self.full_name = full_name 8 | self.grade = -1 9 | 10 | def __lt__(self, other): 11 | return self.grade < other.grade 12 | 13 | def __str__(self): 14 | return f"{self.full_name}({self.grade})" 15 | 16 | 17 | def grade_student(student): 18 | student.grade = randrange(0,11) 19 | 20 | 21 | def average(students): 22 | s = 0 23 | for student in students: 24 | s += student.grade 25 | 26 | print("Average: " + str(s/len(students))) 27 | 28 | seed(datetime.now()) 29 | 30 | names = ["Kirk Bowman", 31 | "Bear Byers", 32 | "Kurtis Macias", 33 | "Maximus Preece", 34 | "Cecily Ray", 35 | "Cade Parry", 36 | "Jordyn Pitts", 37 | "Liya Christian", 38 | "Ceri Orr", 39 | "Mekhi Hahn"] 40 | 41 | students = [Student(names[i]) for i in range(len(names))] 42 | 43 | for student in students: 44 | grade_student(student) 45 | 46 | average(students) 47 | 48 | students.sort(reverse=True) 49 | 50 | for student in students: 51 | print(student) -------------------------------------------------------------------------------- /lesson17/exercise03.py: -------------------------------------------------------------------------------- 1 | class Byte: 2 | def __init__(self, s = ""): 3 | if s == "": 4 | self.array = [0 for i in range(8)] 5 | else: 6 | self.array = [int(c) for c in s] 7 | 8 | def __str__(self): 9 | st = [str(c) for c in self.array] 10 | return "".join(st) 11 | 12 | def __lshift__(self, other): 13 | for i in range(other): 14 | self.array.pop(0) 15 | self.array.append(0) 16 | 17 | def __rshift__(self, other): 18 | for i in range(other): 19 | self.array.pop() 20 | self.array.insert(0,0) 21 | 22 | def __and__(self, other): 23 | new_byte = Byte("") 24 | for i in range(8): 25 | new_byte.array[i] = self.array[i] & other.array[i] 26 | return new_byte 27 | 28 | def __or__(self, other): 29 | new_byte = Byte("") 30 | for i in range(8): 31 | new_byte.array[i] = self.array[i] | other.array[i] 32 | return new_byte 33 | 34 | def __xor__(self, other): 35 | new_byte = Byte("") 36 | for i in range(8): 37 | new_byte.array[i] = self.array[i] ^ other.array[i] 38 | return new_byte 39 | 40 | b = Byte() 41 | b2 = Byte("00010011") 42 | print(b, b2) 43 | 44 | b2 >> 2 45 | print(b2) 46 | 47 | b2 = Byte("00010011") 48 | b3 = Byte("00110101") 49 | print(f"\n{b2}\n{b3}(&)\n{'-'*8}\n{b2&b3}") 50 | print(f"\n{b2}\n{b3}(|)\n{'-'*8}\n{b2|b3}") 51 | print(f"\n{b2}\n{b3}(^)\n{'-'*8}\n{b2^b3}") -------------------------------------------------------------------------------- /lesson17/exercise04.initial.py: -------------------------------------------------------------------------------- 1 | a = Point3D(1,1,1) 2 | b = Point3D(2,2,2) 3 | print(a+b) 4 | c = a + b 5 | print(c) 6 | a = a + b 7 | print(a) 8 | a += b 9 | print(a) -------------------------------------------------------------------------------- /lesson17/exercise04.py: -------------------------------------------------------------------------------- 1 | class Point3D: 2 | def __init__(self,x=0,y=0,z=0): 3 | self.x = x 4 | self.y = y 5 | self.z = z 6 | 7 | def __str__(self): 8 | return f"({self.x},{self.y},{self.z})" 9 | 10 | def __add__(self, other): 11 | new_point = Point3D() 12 | new_point.x = self.x + other.x 13 | new_point.y = self.y + other.y 14 | new_point.z = self.z + other.z 15 | return new_point 16 | 17 | def __iadd__(self, other): 18 | self.x += other.x 19 | self.y += other.y 20 | self.z += other.z 21 | return self 22 | 23 | a = Point3D(1,1,1) 24 | b = Point3D(2,2,2) 25 | print(a+b) 26 | c = a + b 27 | print(c) 28 | print(a, id(a)) 29 | a = a + b 30 | print(a, id(a)) 31 | a += b 32 | print(a, id(a)) -------------------------------------------------------------------------------- /lesson17/exercise05.py: -------------------------------------------------------------------------------- 1 | class Byte: 2 | def __init__(self, s = ""): 3 | if s == "": 4 | self.array = [0 for i in range(8)] 5 | else: 6 | self.array = [int(c) for c in s] 7 | 8 | def __str__(self): 9 | st = [str(c) for c in self.array] 10 | return "".join(st) 11 | 12 | def __lshift__(self, other): 13 | for i in range(other): 14 | self.array.pop(0) 15 | self.array.append(0) 16 | 17 | def __rshift__(self, other): 18 | for i in range(other): 19 | self.array.pop() 20 | self.array.insert(0,0) 21 | 22 | def __and__(self, other): 23 | new_byte = Byte("") 24 | for i in range(8): 25 | new_byte.array[i] = self.array[i] & other.array[i] 26 | return new_byte 27 | 28 | def __or__(self, other): 29 | new_byte = Byte("") 30 | for i in range(8): 31 | new_byte.array[i] = self.array[i] | other.array[i] 32 | return new_byte 33 | 34 | def __xor__(self, other): 35 | new_byte = Byte("") 36 | for i in range(8): 37 | new_byte.array[i] = self.array[i] ^ other.array[i] 38 | return new_byte 39 | 40 | def __invert__(self): 41 | new_byte = Byte("") 42 | for i in range(8): 43 | new_byte.array[i] = 1 if self.array[i]==0 else 0 44 | return new_byte 45 | 46 | b = Byte() 47 | b2 = Byte("00010011") 48 | print(b, b2) 49 | 50 | b2 >> 2 51 | print(b2) 52 | 53 | b2 = Byte("00010011") 54 | b3 = Byte("00110101") 55 | print(f"\n{b2}\n{b3}(&)\n{'-'*8}\n{b2&b3}") 56 | print(f"\n{b2}\n{b3}(|)\n{'-'*8}\n{b2|b3}") 57 | print(f"\n{b2}\n{b3}(^)\n{'-'*8}\n{b2^b3}") 58 | print(f"\n{b3}(~)\n{'-'*8}\n{~b3}") -------------------------------------------------------------------------------- /lesson17/exercise06.py: -------------------------------------------------------------------------------- 1 | class Counter: 2 | def __init__(self): 3 | self.counter = 0 4 | 5 | def __str__(self): 6 | return str(self.counter) 7 | 8 | def __pos__(self): 9 | self.counter += 1 10 | 11 | def __neg__(self): 12 | self.counter -= 1 13 | 14 | c = Counter() 15 | 16 | +c 17 | +c 18 | +c 19 | print(c) 20 | 21 | -c 22 | -c 23 | print(c) -------------------------------------------------------------------------------- /lesson17/exercise07.py: -------------------------------------------------------------------------------- 1 | class Length: 2 | def __init__(self, val, m): 3 | if m == "m": 4 | self.val = val 5 | elif m == "cm": 6 | self.val = val / 100 7 | elif m == "in": 8 | self.val = val * 0.0254 9 | else: 10 | self.val = 0 11 | 12 | def __str__(self): 13 | return f"{self.val}m" 14 | 15 | def __add__(self, other): 16 | return Length(self.val + other.val, "m") 17 | 18 | def __round__(self, n=None): 19 | return round(self.val, n) 20 | 21 | def __int__(self): 22 | return int(self.val) 23 | 24 | l1 = Length(1,"m") 25 | l2 = Length(50, "cm") 26 | l3 = Length(100, "in") 27 | 28 | print(l1 + l2) 29 | print(round(l2 + l3 + l3 + l1,1)) 30 | print(l3, int(l3)) 31 | -------------------------------------------------------------------------------- /lesson17/exercise09.py: -------------------------------------------------------------------------------- 1 | class Point3D: 2 | def __init__(self,x=0,y=0,z=0): 3 | self.x = x 4 | self.y = y 5 | self.z = z 6 | 7 | def __str__(self): 8 | return f"({self.x},{self.y},{self.z})" 9 | 10 | def __add__(self, other): 11 | new_point = Point3D() 12 | new_point.x = self.x + other.x 13 | new_point.y = self.y + other.y 14 | new_point.z = self.z + other.z 15 | return new_point 16 | 17 | def __iadd__(self, other): 18 | self.x += other.x 19 | self.y += other.y 20 | self.z += other.z 21 | return self 22 | 23 | def __getitem__(self, item): 24 | if item == 0: 25 | return self.x 26 | elif item == 1: 27 | return self.y 28 | elif item == 2: 29 | return self.z 30 | return None 31 | 32 | def __setitem__(self, key, value): 33 | if key == 0: 34 | self.x = value 35 | elif key == 1: 36 | self.y = value 37 | elif key == 2: 38 | self.z = value 39 | 40 | a = Point3D(1,1,1) 41 | b = Point3D(2,2,2) 42 | print(a+b) 43 | c = a + b 44 | print(c) 45 | print(a, id(a)) 46 | a = a + b 47 | print(a, id(a)) 48 | a += b 49 | print(a, id(a)) 50 | a[0]=7 51 | print(a) -------------------------------------------------------------------------------- /lesson17/exercise10.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | class Cycle: 4 | def __init__(self, c): 5 | self.c = c 6 | 7 | def __str__(self): 8 | return f"x^2+y^2={c}^2" 9 | 10 | def __eq__(self, other): 11 | return self.c == other.c 12 | 13 | def __lt__(self, other): 14 | return self.c < other.c 15 | 16 | def __call__(self, x, y=None): 17 | if y is None: 18 | if isinstance(x, (int,float)): 19 | if abs(x) < self.c: 20 | return (x,sqrt(self.c**2-x**2)), (x,-sqrt(self.c**2-x**2)) 21 | elif abs(x) == self.c: 22 | return (x, 0) 23 | else: 24 | return None 25 | else: 26 | if isinstance(x, (int, float)): 27 | if x**2+y**2 0] 17 | 18 | if len(not_empty_cash_desks) > 0: 19 | cash_desk = not_empty_cash_desks[randrange(len(not_empty_cash_desks))] 20 | customer = - self.cash_desks[cash_desk] 21 | print(f"{customer} served by cash desk {cash_desk}") 22 | else: 23 | print("No customers.") 24 | 25 | def __str__(self): 26 | st = "\n" + "=" * 20 27 | for i in range(self.N): 28 | st += "\nCash Desk " + str(i) +": " 29 | st += str(self.cash_desks[i]) 30 | st += "\n" + "=" * 20 31 | return st 32 | 33 | 34 | def main(): 35 | seed(datetime.now()) 36 | 37 | bank = Bank(3) 38 | 39 | for i in range(100): 40 | num = randrange(100) 41 | if num <=29: 42 | bank.customer_served() 43 | else: 44 | bank.customer_enters("Cust" + str(randrange(1000))) 45 | 46 | if i % 10 == 0: 47 | print(bank) 48 | 49 | print("BANK CLOSED") 50 | 51 | main() -------------------------------------------------------------------------------- /lesson17/function.py: -------------------------------------------------------------------------------- 1 | class Polynomial: 2 | def __init__(self, *coeff): 3 | self.coeff = [c for c in coeff] 4 | print(self.coeff) 5 | 6 | def __str__(self): 7 | st = [] 8 | for i in range(len(self.coeff)): 9 | st.append(f"{self.coeff[i]}*x^{len(self.coeff)-i-1}") 10 | return " + ".join(st) 11 | 12 | def __call__(self, x): 13 | res = 0 14 | for i in range(len(self.coeff)): 15 | res += self.coeff[i] * x ** (len(self.coeff)-i-1) 16 | return res 17 | 18 | 19 | p = Polynomial(5,1,2) 20 | print(str(p)) 21 | print(p(2)) 22 | 23 | p2 = Polynomial(4,5,4,1,2,3) 24 | print(p2) -------------------------------------------------------------------------------- /lesson17/iterate.py: -------------------------------------------------------------------------------- 1 | class X: 2 | def __init__(self): 3 | self.ar = [x for x in range(5)] 4 | 5 | def __len__(self): 6 | return len(self.ar) 7 | 8 | def __getitem__(self, item): 9 | return self.ar[item] 10 | 11 | obj = X() 12 | for item in obj: 13 | print(item) -------------------------------------------------------------------------------- /lesson17/lesson17.exercise13/character.py: -------------------------------------------------------------------------------- 1 | from random import randrange 2 | 3 | 4 | class Character: 5 | def __init__(self, character_name, equipment, attack_speed=2, delay=0): 6 | self.character_name = character_name 7 | self.equipment = equipment 8 | self.max_health = 100 * self.equipment.cape 9 | self.health = 100 * self.equipment.cape 10 | self.attack_speed = attack_speed 11 | self.delay = delay 12 | 13 | def attack(self): 14 | self.delay = 5 - self.attack_speed 15 | return round(randrange(3, 11) * self.equipment.sword) 16 | 17 | def is_dead(self): 18 | return self.health <= 0 19 | 20 | def end_round(self): 21 | self.health = self.health+1 if self.health+1<=self.max_health else self.max_health 22 | self.delay -= 1 23 | 24 | def __str__(self): 25 | return f"{self.character_name} H:{round(self.health)} D:{self.delay}" 26 | 27 | def __repr__(self): 28 | return f"Character({self.character_name},{self.attack_speed}, {self.delay}, {round(self.max_health)}, {round(self.health)})" 29 | 30 | def __iadd__(self, other): 31 | self.health += other 32 | return self 33 | 34 | def __isub__(self, other): 35 | self.health -= other 36 | return self 37 | -------------------------------------------------------------------------------- /lesson17/lesson17.exercise13/equipment.py: -------------------------------------------------------------------------------- 1 | class Equipment: 2 | def __init__(self, sword, cape): 3 | self.sword = sword 4 | self.cape = cape 5 | 6 | -------------------------------------------------------------------------------- /lesson17/lesson17.exercise13/main.py: -------------------------------------------------------------------------------- 1 | from character import Character 2 | from arena import Arena 3 | from equipment import Equipment 4 | from random import randrange, seed, uniform 5 | from datetime import datetime 6 | 7 | 8 | def main(): 9 | seed(datetime.now()) 10 | 11 | orcs = [Character("Orc-" + str(i+1),Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 2,randrange(4)) for i in range(5)] 12 | night_elves = [Character("Night-Elf-" + str(i + 1),Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 3, randrange(3)) for i in range(3)] 13 | 14 | a = Arena(orcs, night_elves) 15 | a.play() 16 | 17 | 18 | main() -------------------------------------------------------------------------------- /lesson17/lesson17.exercise14/pupils.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "John", "last_name": "Cm", "fathers_name": "Wick", "age": 44, "class_name": 1, "pupil_id": 1001, "id_number": "12131234"}, {"first_name": "Bob", "last_name": "Hope", "fathers_name": "Charles", "age": 12, "class_name": 2, "pupil_id": 1002}, {"first_name": "1", "last_name": "1", "fathers_name": "1", "age": 1, "class_name": 1, "pupil_id": 1003}] -------------------------------------------------------------------------------- /lesson17/lesson17.exercise14/teacher.py: -------------------------------------------------------------------------------- 1 | class Teacher: 2 | def __init__(self, first_name="", surname="", teacher_id=-1): 3 | self.first_name = first_name 4 | self.surname = surname 5 | self.teacher_id = teacher_id 6 | 7 | def from_dict(self, teacher_dict): 8 | self.first_name = teacher_dict["first_name"] 9 | self.surname = teacher_dict["surname"] 10 | self.teacher_id = teacher_dict["teacher_id"] 11 | 12 | def to_dict(self): 13 | teacher_dict = {"first_name": self.first_name, 14 | "surname": self.surname, 15 | "teacher_id":self.teacher_id} 16 | return teacher_dict 17 | 18 | def __str__(self): 19 | st = f"Name : {self.first_name}" 20 | st += f"\nSurname: {self.surname}" 21 | st += f"\nid : {self.teacher_id}" 22 | return st 23 | -------------------------------------------------------------------------------- /lesson17/lesson17.exercise14/teachers.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "Severus", "surname": "Snape", "teacher_id": 1001}, {"first_name": "Charles", "surname": "Xavier", "teacher_id": 1002}, {"first_name": "Sergio", "surname": "Marquina", "teacher_id": 1003}] -------------------------------------------------------------------------------- /lesson17/new.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __new__(cls): 3 | print("A.__new__ called") 4 | return super(A, cls).__new__(cls) 5 | def __init__(self): 6 | print("A.__init__ called") 7 | 8 | A() 9 | -------------------------------------------------------------------------------- /lesson17/python17.oop.methods.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson17/python17.oop.methods.pdf -------------------------------------------------------------------------------- /lesson17/queue.py: -------------------------------------------------------------------------------- 1 | class Queue: 2 | def __init__(self): 3 | self.array = [] 4 | 5 | def enqueue(self, elem): 6 | self.array.append(elem) 7 | 8 | def dequeue(self): 9 | if not self.array: 10 | return None 11 | else: 12 | return self.array.pop(0) 13 | 14 | def __str__(self): 15 | return ", ".join(self.array) 16 | 17 | def __add__(self, other): 18 | new_q = Queue() 19 | new_q.array = self.array[:] 20 | new_q.enqueue(other) 21 | return new_q 22 | 23 | def __iadd__(self, other): 24 | self.enqueue(other) 25 | return self 26 | 27 | def __neg__(self): 28 | return self.dequeue() 29 | 30 | def __len__(self): 31 | return len(self.array) -------------------------------------------------------------------------------- /lesson17/repr.py: -------------------------------------------------------------------------------- 1 | class Time: 2 | def __init__(self, hour, minute, second): 3 | self.hour = hour 4 | self.minute = minute 5 | self.second = second 6 | 7 | def __str__(self): 8 | return f"{str(self.hour).zfill(2)}:" \ 9 | f"{str(self.minute).zfill(2)}:" \ 10 | f"{str(self.second).zfill(2)}" 11 | 12 | def __repr__(self): 13 | return f"Time({self.hour},{self.minute},{self.second})" 14 | 15 | 16 | def __gt__(self, other): 17 | if self.hour > other.hour: 18 | return True 19 | elif self.hour == other.hour: 20 | if self.minute > other.minute: 21 | return True 22 | elif self.minute == other.minute: 23 | if self.second > other.second: 24 | return True 25 | return False 26 | 27 | def __ge__(self, other): 28 | if self.hour > other.hour: 29 | return True 30 | elif self.hour == other.hour: 31 | if self.minute > other.minute: 32 | return True 33 | elif self.minute == other.minute: 34 | if self.second >= other.second: 35 | return True 36 | return False 37 | 38 | def __eq__(self, other): 39 | if isinstance(other, Time): 40 | if self.hour == other.hour and self.minute == other.minute and self.second == other.second: 41 | return True 42 | return False 43 | elif isinstance(other, int): 44 | return self == Time(other, 0, 0) 45 | 46 | 47 | t = Time(11,1,2) 48 | print(t) 49 | print(repr(t)) 50 | -------------------------------------------------------------------------------- /lesson17/str.py: -------------------------------------------------------------------------------- 1 | class Time: 2 | def __init__(self, hour, minute, second): 3 | self.hour = hour 4 | self.minute = minute 5 | self.second = second 6 | 7 | def __str__(self): 8 | return f"{str(self.hour).zfill(2)}:" \ 9 | f"{str(self.minute).zfill(2)}:" \ 10 | f"{str(self.second).zfill(2)}" 11 | 12 | 13 | t = Time(11,2,3) 14 | print(t) 15 | print("The time is " + str(t)) -------------------------------------------------------------------------------- /lesson18/abstract.class.py: -------------------------------------------------------------------------------- 1 | # abstract.class.py 2 | from abc import ABC, abstractmethod 3 | 4 | 5 | class MyAbstractClass(ABC): 6 | def __init__(self, attr): 7 | self.attr = attr 8 | 9 | @abstractmethod 10 | def my_abstract_method(self): 11 | pass 12 | 13 | 14 | ob = MyAbstractClass(1) 15 | -------------------------------------------------------------------------------- /lesson18/exercise01.py: -------------------------------------------------------------------------------- 1 | class Animal: 2 | def __init__(self, weight, height): 3 | self.weight = weight 4 | self.height = height 5 | 6 | 7 | class Horse(Animal): 8 | def __init__(self, weight, height, color, tail): 9 | super().__init__(weight, height) 10 | self.color = color 11 | self.tail = tail 12 | 13 | 14 | class Dog(Animal): 15 | def __init__(self, weight, height, db): 16 | super().__init__(weight, height) 17 | self.db = db 18 | 19 | def bark(self): 20 | print("Woof woof") 21 | 22 | 23 | class Doberman(Dog): 24 | def __init__(self, weight, height, db): 25 | super().__init__(weight, height, db) 26 | 27 | def run(self): 28 | print("Doberman is running") 29 | 30 | 31 | class Bulldog(Dog): 32 | def __init__(self, weight, height, db, ear_length): 33 | super().__init__(weight, height, db) 34 | self.ear_length = ear_length 35 | 36 | def sleep(self): 37 | print("Bulldog is sleeping") 38 | 39 | 40 | dolly = Horse(300,2,"white",1) 41 | tobie = Doberman(50,1.1,8) 42 | buddy = Bulldog(70,1,9,0.4) 43 | 44 | print(dolly.color) 45 | tobie.bark() 46 | tobie.run() 47 | tobie.bark() 48 | buddy.bark() 49 | buddy.sleep() 50 | buddy.sleep() -------------------------------------------------------------------------------- /lesson18/exercise03.py: -------------------------------------------------------------------------------- 1 | class Animal: 2 | def make_sound(self): 3 | print("") 4 | 5 | 6 | class Cat(Animal): 7 | def make_sound(self): 8 | print("Meow") 9 | 10 | 11 | class HimalayanCat(Cat): 12 | def make_sound(self): 13 | super().make_sound() 14 | print("Miouw Miouw") 15 | 16 | 17 | class Dog(Animal): 18 | def make_sound(self): 19 | print("Woof woof") 20 | 21 | 22 | class Doberman(Dog): 23 | pass 24 | 25 | 26 | class KingDoberman(Doberman): 27 | def make_sound(self): 28 | super().make_sound() 29 | print("WOOAAAAAAAFFF") 30 | 31 | 32 | Animal().make_sound() 33 | Cat().make_sound() 34 | HimalayanCat().make_sound() 35 | Dog().make_sound() 36 | Doberman().make_sound() 37 | KingDoberman().make_sound() -------------------------------------------------------------------------------- /lesson18/exercise04.py: -------------------------------------------------------------------------------- 1 | # protected.py 2 | class Base: 3 | def __init__(self): 4 | self.__bpr_attr = 1 5 | 6 | 7 | class Derived(Base): 8 | def __init__(self): 9 | super().__init__() 10 | 11 | 12 | d = Derived() 13 | print(d._Base__bpr_attr) 14 | print(d.__bpr_attr) -------------------------------------------------------------------------------- /lesson18/exercise06.py: -------------------------------------------------------------------------------- 1 | class King: 2 | def __init__(self, realm): 3 | self.realm = realm 4 | 5 | def rule(self): 6 | print("Now, I rule") 7 | 8 | 9 | class Philosopher: 10 | def __init__(self, school): 11 | self.school = school 12 | 13 | def think(self): 14 | print("Now, I think") 15 | 16 | 17 | class PhilosopherKing(King, Philosopher): 18 | def __init__(self, realm, school): 19 | King.__init__(self, realm) 20 | Philosopher.__init__(self, school) 21 | 22 | def routine(self): 23 | self.think() 24 | self.rule() 25 | self.think() 26 | 27 | 28 | marcus_aurelius = PhilosopherKing("Roman Empire", "Stoic") 29 | marcus_aurelius.routine() -------------------------------------------------------------------------------- /lesson18/exercise07.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | 4 | 5 | class Person: 6 | def __init__(self, full_name, salary): 7 | self.full_name = full_name 8 | self.salary = salary 9 | self.served_cnt = 0 10 | 11 | def report(self): 12 | print(self.full_name + " served " + str(self.served_cnt) + " customers.") 13 | 14 | class Waiter(Person): 15 | def __init__(self, full_name, salary): 16 | Person.__init__(self, full_name, salary) 17 | 18 | def serve(self, customers, barista): 19 | self.served_cnt += customers 20 | print("Waiter " + self.full_name + " served " + str(customers) + " customers") 21 | barista.prepare(customers) 22 | 23 | 24 | class Barista(Person): 25 | def __init__(self, full_name, salary): 26 | Person.__init__(self, full_name, salary) 27 | 28 | def prepare(self, customers): 29 | print("Barista " + self.full_name + " served " + str(customers) + " customers") 30 | self.served_cnt += customers 31 | 32 | 33 | class Owner(Waiter, Barista): 34 | def __init__(self, full_name, salary): 35 | Person.__init__(self, full_name, salary) 36 | 37 | 38 | 39 | def main(): 40 | seed(datetime.now()) 41 | 42 | o = Owner("owner", 100000) 43 | w1 = Waiter("waiter-1", 200000) 44 | w2 = Waiter("waiter-1", 200000) 45 | b = Barista("barista", 300000) 46 | 47 | waiters = [o, w1, w2] 48 | baristas = [o, b] 49 | 50 | for _ in range(10): 51 | waiters[randrange(3)].serve(randrange(1,5+1), baristas[randrange(2)]) 52 | 53 | print("") 54 | o.report() 55 | w1.report() 56 | w2.report() 57 | b.report() 58 | 59 | main() 60 | 61 | -------------------------------------------------------------------------------- /lesson18/exercise08.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | 3 | class Animal(ABC): 4 | @abstractmethod 5 | def make_sound(self): 6 | pass 7 | 8 | 9 | class Cat(Animal): 10 | def make_sound(self): 11 | print("Meow") 12 | 13 | 14 | class HimalayanCat(Cat): 15 | def make_sound(self): 16 | super().make_sound() 17 | print("Miouw Miouw") 18 | 19 | 20 | class Dog(Animal): 21 | def make_sound(self): 22 | print("Woof woof") 23 | 24 | 25 | class Doberman(Dog): 26 | pass 27 | 28 | 29 | class KingDoberman(Doberman): 30 | def make_sound(self): 31 | super().make_sound() 32 | print("WOOAAAAAAAFFF") 33 | 34 | 35 | Cat().make_sound() 36 | HimalayanCat().make_sound() 37 | Dog().make_sound() 38 | Doberman().make_sound() 39 | KingDoberman().make_sound() -------------------------------------------------------------------------------- /lesson18/exercise09.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | from abc import ABC, abstractmethod 4 | 5 | class Person(ABC): 6 | def __init__(self, full_name, salary): 7 | self.full_name = full_name 8 | self.salary = salary 9 | self.served_cnt = 0 10 | 11 | def report(self): 12 | print(self.full_name + " served " + str(self.served_cnt) + " customers.") 13 | 14 | class Waiter(Person): 15 | def __init__(self, full_name, salary): 16 | Person.__init__(self, full_name, salary) 17 | 18 | def serve(self, customers, barista): 19 | self.served_cnt += customers 20 | print("Waiter " + self.full_name + " served " + str(customers) + " customers") 21 | barista.prepare(customers) 22 | 23 | 24 | class Barista(Person): 25 | def __init__(self, full_name, salary): 26 | Person.__init__(self, full_name, salary) 27 | 28 | def prepare(self, customers): 29 | print("Barista " + self.full_name + " served " + str(customers) + " customers") 30 | self.served_cnt += customers 31 | 32 | 33 | class Owner(Waiter, Barista): 34 | def __init__(self, full_name, salary): 35 | Person.__init__(self, full_name, salary) 36 | 37 | 38 | 39 | def main(): 40 | seed(datetime.now()) 41 | 42 | o = Owner("owner", 100000) 43 | w1 = Waiter("waiter-1", 200000) 44 | w2 = Waiter("waiter-1", 200000) 45 | b = Barista("barista", 300000) 46 | 47 | waiters = [o, w1, w2] 48 | baristas = [o, b] 49 | 50 | for _ in range(10): 51 | waiters[randrange(3)].serve(randrange(1,5+1), baristas[randrange(2)]) 52 | 53 | print("") 54 | o.report() 55 | w1.report() 56 | w2.report() 57 | b.report() 58 | 59 | main() 60 | 61 | -------------------------------------------------------------------------------- /lesson18/exercise10.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | from math import pi 3 | 4 | 5 | class GeometricObjectInterface(ABC): 6 | @abstractmethod 7 | def area(self): 8 | pass 9 | 10 | @abstractmethod 11 | def perimeter(self): 12 | pass 13 | 14 | 15 | class Cycle(GeometricObjectInterface): 16 | def __init__(self, radius): 17 | self.radius = radius 18 | 19 | def area(self): 20 | return pi * self.radius ** 2 21 | 22 | def perimeter(self): 23 | return 2 * pi * self.radius 24 | 25 | 26 | class Resizable(ABC): 27 | @abstractmethod 28 | def resize(self, param): 29 | pass 30 | 31 | 32 | class CycleResizable(Cycle, Resizable): 33 | def __init__(self, radius): 34 | Cycle.__init__(self, radius) 35 | 36 | def resize(self, param): 37 | self.radius = self.radius * param 38 | 39 | 40 | c = CycleResizable(1) 41 | print(c.area(), c.perimeter()) 42 | c.resize(2) 43 | print(c.area(), c.perimeter()) 44 | -------------------------------------------------------------------------------- /lesson18/inheritance.py: -------------------------------------------------------------------------------- 1 | class Cow: 2 | def __init__(self, weight, hunger): 3 | self.weight = weight 4 | self.hunger = hunger 5 | 6 | def express(self): 7 | if self.hunger > 5: 8 | print("Moooooowwwwwwwwww") 9 | else: 10 | print("Mowww") 11 | 12 | 13 | class TexasLonghorn(Cow): 14 | def __init__(self, weight, hunger, horn_length): 15 | super().__init__(weight, hunger) 16 | self.horn_length = horn_length 17 | 18 | 19 | molly = Cow(500, 10) 20 | molly.express() 21 | 22 | bob = TexasLonghorn(400,20,0.50) 23 | bob.express() 24 | print(f"Bob's horns are {bob.horn_length} meters long") 25 | print(f"Bob's weight={bob.weight}, hunger={bob.hunger}, horn_length={bob.horn_length}") -------------------------------------------------------------------------------- /lesson18/interface.py: -------------------------------------------------------------------------------- 1 | # interface.py 2 | from abc import ABC, abstractmethod 3 | 4 | 5 | class MyInterface(ABC): 6 | @abstractmethod 7 | def interface_method1(self): 8 | pass 9 | 10 | @abstractmethod 11 | def interface_method2(self): 12 | pass 13 | 14 | 15 | class MyRealClass(MyInterface): 16 | ... 17 | -------------------------------------------------------------------------------- /lesson18/lesson18.exercise12/character.py: -------------------------------------------------------------------------------- 1 | from equipment import Equipment 2 | from random import randrange 3 | 4 | 5 | class Character: 6 | def __init__(self, character_name, equipment, attack_speed=2, delay=0): 7 | self.character_name = character_name 8 | self.equipment = equipment 9 | self.max_health = 100 * self.equipment.cape 10 | self.health = 100 * self.equipment.cape 11 | self.attack_range = (3, 11) 12 | self.attack_speed = attack_speed 13 | self.delay = delay 14 | self.max_delay = 5 15 | 16 | def attack(self): 17 | self.delay = self.max_delay - self.attack_speed 18 | return round(randrange(self.attack_range[0], self.attack_range[1]) * self.equipment.sword) 19 | 20 | def is_dead(self): 21 | return self.health <= 0 22 | 23 | def end_round(self): 24 | self.health = self.health+1 if self.health+1<=self.max_health else self.max_health 25 | self.delay -= 1 26 | 27 | def __str__(self): 28 | return f"{self.character_name} H:{round(self.health)} D:{self.delay}" 29 | 30 | def __repr__(self): 31 | return f"Character({self.character_name},{self.attack_speed}, {self.delay}, {round(self.max_health)}, {round(self.health)})" 32 | 33 | def __iadd__(self, other): 34 | self.health += other 35 | return self 36 | 37 | def __isub__(self, other): 38 | self.health -= other 39 | return self -------------------------------------------------------------------------------- /lesson18/lesson18.exercise12/equipment.py: -------------------------------------------------------------------------------- 1 | class Equipment: 2 | def __init__(self, sword, cape): 3 | self.sword = sword 4 | self.cape = cape 5 | 6 | -------------------------------------------------------------------------------- /lesson18/lesson18.exercise12/mage.py: -------------------------------------------------------------------------------- 1 | from character import Character 2 | from random import randrange 3 | 4 | 5 | class Mage(Character): 6 | def __init__(self, character_name, equipment, attack_speed=2, delay=0): 7 | super().__init__(character_name, equipment, attack_speed, delay) 8 | self.attack_range = (8, 17) 9 | self.mana = 100 10 | self.max_mana = 100 11 | 12 | def end_round(self): 13 | super().end_round() 14 | self.mana = self.mana + 1 if self.mana + 1 <= self.mana else self.max_mana 15 | 16 | def lightning_spell(self): 17 | self.mana -= 55 18 | return randrange(30, 50) 19 | 20 | def attack(self): 21 | self.delay = self.max_delay - self.attack_speed 22 | if self.mana >= 55: 23 | return self.lightning_spell() 24 | else: 25 | return round(randrange(self.attack_range[0], self.attack_range[1]) * self.equipment.sword) 26 | -------------------------------------------------------------------------------- /lesson18/lesson18.exercise12/main.py: -------------------------------------------------------------------------------- 1 | from character import Character 2 | from tank import Tank 3 | from mage import Mage 4 | from arena import Arena 5 | from equipment import Equipment 6 | from random import randrange, seed, uniform 7 | from datetime import datetime 8 | 9 | 10 | def main(): 11 | seed(datetime.now()) 12 | 13 | orcs = [Character("Orc-" + str(i+1),Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 2,randrange(4)) for i in range(5)] 14 | orcs += [Tank("Orc Tank",Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 2,randrange(4))] 15 | night_elves = [Character("Night-Elf-" + str(i + 1),Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 3, randrange(3)) for i in range(3)] 16 | night_elves += [Mage("Night Elf Mage",Equipment(uniform(1.1, 1.5), uniform(1.1,1.3)), 2, randrange(4))] 17 | a = Arena(orcs, night_elves) 18 | a.play() 19 | 20 | 21 | main() -------------------------------------------------------------------------------- /lesson18/lesson18.exercise12/tank.py: -------------------------------------------------------------------------------- 1 | from character import Character 2 | 3 | class Tank(Character): 4 | def __init__(self, character_name, equipment, attack_speed=2, delay=0): 5 | super().__init__(character_name, equipment, attack_speed, delay) 6 | self.attack_range = (20, 30) 7 | self.max_health = self.max_health * 2 8 | self.health = self.max_health 9 | -------------------------------------------------------------------------------- /lesson18/linked_list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data, next=None): 3 | self.data = data 4 | self.next = next 5 | 6 | 7 | class LinkedList: 8 | def __init__(self): 9 | self.head = None 10 | 11 | def empty(self): 12 | return self.head is None 13 | 14 | def insert_start(self, data): 15 | n = Node(data) 16 | n.next = self.head 17 | self.head = n 18 | 19 | def insert_after(self, prev, data): 20 | n = Node(data) 21 | n.next = prev.next 22 | prev.next = n 23 | 24 | def delete_start(self): 25 | if self.empty(): 26 | return None 27 | else: 28 | c = self.head 29 | self.head = self.head.next 30 | return c.data 31 | 32 | def delete_after(self, prev): 33 | if prev.next is None: 34 | return None 35 | else: 36 | c = prev.next 37 | prev.next = c.next 38 | return c.data 39 | 40 | def __str__(self): 41 | p = self.head 42 | st = "" 43 | while p is not None: 44 | st += str(p.data) + "-->" 45 | p = p.next 46 | st += "." 47 | return st 48 | 49 | -------------------------------------------------------------------------------- /lesson18/method.overriding.py: -------------------------------------------------------------------------------- 1 | # method.overriding.py 2 | class Cow: 3 | def __init__(self, weight, hunger): 4 | self.weight = weight 5 | self.hunger = hunger 6 | 7 | def express(self): 8 | if self.hunger > 5: 9 | print("Moooooowwwwwwwwww") 10 | else: 11 | print("Mowww") 12 | 13 | 14 | class TexasLonghorn(Cow): 15 | def __init__(self, weight, hunger, horn_length): 16 | super().__init__(weight, hunger) 17 | self.horn_length = horn_length 18 | 19 | def express(self): 20 | if self.hunger > 5: 21 | print("MEEoooEEEwwwwwwwww") 22 | else: 23 | print("MEoEwww") 24 | 25 | molly = Cow(500, 10) 26 | molly.express() 27 | 28 | bob = TexasLonghorn(400,20,0.50) 29 | bob.express() 30 | print(f"Bob's horns are {bob.horn_length} meters long") 31 | print(f"Bob's weight={bob.weight}, hunger={bob.hunger}, horn_length={bob.horn_length}") -------------------------------------------------------------------------------- /lesson18/mixin.py: -------------------------------------------------------------------------------- 1 | # mixin.py 2 | class ComparableMixin: 3 | """This class has methods which use `<=` and `==`, 4 | but this class does NOT implement those methods.""" 5 | def __ne__(self, other): 6 | return not (self == other) 7 | def __lt__(self, other): 8 | return self <= other and (self != other) 9 | def __gt__(self, other): 10 | return not self <= other 11 | def __ge__(self, other): 12 | return self == other or self > other 13 | 14 | class Integer(ComparableMixin): 15 | def __init__(self, i): 16 | self.i = i 17 | def __le__(self, other): 18 | return self.i <= other.i 19 | def __eq__(self, other): 20 | return self.i == other.i 21 | 22 | print(Integer(3)<=Integer(4)) 23 | print(Integer(3) str: 2 | return f"{first_name} {last_name}" 3 | 4 | print(full_name("John", "Wick")) -------------------------------------------------------------------------------- /lesson20/exercise01.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from waiter import Waiter, Barista 3 | 4 | 5 | class WaiterTestCase(unittest.TestCase): 6 | def setUp(self): 7 | self.baristaObj = Barista("Bob", 10000) 8 | 9 | def test_init(self): 10 | w = Waiter("Tom", 5000) 11 | self.assertEqual(w.full_name, "Tom") 12 | self.assertEqual(w.salary, 5000) 13 | self.assertEqual(w.served_cnt, 0) 14 | 15 | def test_serve(self): 16 | w = Waiter("Tom", 5000) 17 | w.serve(50, self.baristaObj) 18 | self.assertEqual(w.served_cnt, 50) 19 | -------------------------------------------------------------------------------- /lesson20/exercise02.py: -------------------------------------------------------------------------------- 1 | from graph import Graph 2 | from queue import Queue 3 | 4 | 5 | def breadth_first_search(graph, start, finish): 6 | q = Queue() 7 | discovered = [start] 8 | q.enqueue(start) 9 | parent = {} 10 | while len(q) > 0: 11 | v = q.dequeue() 12 | if v == finish: 13 | break 14 | for neighbor in graph.neighbors(v): 15 | if neighbor.descr not in discovered: 16 | discovered += [neighbor.descr] 17 | parent[neighbor.descr] = v 18 | q.enqueue(neighbor.descr) 19 | 20 | path = [finish] 21 | while path[0] != start: 22 | path = [parent[path[0]]] + path 23 | 24 | print(path) 25 | 26 | 27 | def main(): 28 | facebook_users = Graph() 29 | for user in ["Bob", "Anne", "Elisa", "Diana", "Carl"]: 30 | facebook_users.add_vertex(user) 31 | 32 | facebook_users.add_edge("Carl", "Bob") 33 | facebook_users.add_edge("Carl", "Elisa") 34 | facebook_users.add_edge("Carl", "Diana") 35 | facebook_users.add_edge("Diana", "Bob") 36 | facebook_users.add_edge("Diana", "Anne") 37 | facebook_users.add_edge("Elisa", "Anne") 38 | facebook_users.add_edge("Anne", "Bob") 39 | print(facebook_users) 40 | print("\n") 41 | breadth_first_search(facebook_users, "Carl", "Anne") 42 | 43 | 44 | main() -------------------------------------------------------------------------------- /lesson20/graph.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, descr="", neighbors=None): 3 | self.descr = descr 4 | if neighbors is None: 5 | self.neighbors = [] 6 | else: 7 | self.neighbors = neighbors 8 | 9 | 10 | class Graph: 11 | def __init__(self): 12 | self.nodes = [] 13 | 14 | def add_vertex(self, descr="", neighbors=None): 15 | if neighbors is None: 16 | neighbors = [] 17 | self.nodes += [Node(descr, neighbors)] 18 | 19 | def __index_of(self, descr): 20 | for i in range(len(self.nodes)): 21 | if descr == self.nodes[i].descr: 22 | return i 23 | 24 | def add_edge(self, descr1, descr2): 25 | index1 = self.__index_of(descr1) 26 | index2 = self.__index_of(descr2) 27 | self.nodes[index1].neighbors += [self.nodes[index2]] 28 | self.nodes[index2].neighbors += [self.nodes[index1]] 29 | 30 | def neighbors(self, descr): 31 | return self.nodes[self.__index_of(descr)].neighbors 32 | 33 | def __str__(self): 34 | st = "" 35 | for node in self.nodes: 36 | st += f"\n{node.descr}: " 37 | for neighbor in node.neighbors: 38 | st += f" {neighbor.descr}" 39 | 40 | return st -------------------------------------------------------------------------------- /lesson20/lesson20.exercise02/lesson.py: -------------------------------------------------------------------------------- 1 | from teachers import Teachers 2 | from pupils import Pupils 3 | 4 | 5 | class Lesson: 6 | def __init__(self, lesson_name="", lesson_id=-1): 7 | self.lesson_name = lesson_name 8 | self.teacher_ids = [] 9 | self.pupil_ids = [] 10 | self.lesson_id = lesson_id 11 | 12 | def from_dict(self, lesson_dict): 13 | self.lesson_name = lesson_dict["lesson_name"] 14 | self.teacher_ids = lesson_dict["teacher_ids"] 15 | self.pupil_ids = lesson_dict["pupil_ids"] 16 | self.lesson_id = lesson_dict["lesson_id"] 17 | 18 | def to_dict(self): 19 | lesson_dict = {"lesson_name": self.lesson_name, 20 | "teacher_ids": self.teacher_ids, 21 | "pupil_ids": self.pupil_ids, 22 | "lesson_id": self.lesson_id 23 | } 24 | 25 | return lesson_dict 26 | 27 | def print_lesson_details(self, teachers, pupils): 28 | print(f"LESSON: {self.lesson_name}") 29 | print("=========") 30 | print("TEACHERS: ") 31 | for teacher_id in self.teacher_ids: 32 | teacher = teachers.read_teacher(teacher_id) 33 | print(f"{teacher.first_name} {teacher.surname}") 34 | print("STUDENTS: ") 35 | for pupil_id in self.pupil_ids: 36 | pupil = pupils.search_pupil_by_id(pupil_id) 37 | print(f"{pupil.first_name} {pupil.last_name}") 38 | 39 | -------------------------------------------------------------------------------- /lesson20/lesson20.exercise02/lessons.json: -------------------------------------------------------------------------------- 1 | [{"lesson_name": "Algorithms", "teacher_ids": [1001], "pupil_ids": [1001, 1002], "lesson_id": 1001}, {"lesson_name": "AS", "teacher_ids": [], "pupil_ids": [], "lesson_id": 1002}] -------------------------------------------------------------------------------- /lesson20/lesson20.exercise02/pupils.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "John", "last_name": "Cm", "fathers_name": "Wick", "age": 44, "class_name": 1, "pupil_id": 1001, "id_number": "12131234"}, {"first_name": "Bob", "last_name": "Hope", "fathers_name": "Charles", "age": 12, "class_name": 2, "pupil_id": 1002}, {"first_name": "1", "last_name": "1", "fathers_name": "1", "age": 1, "class_name": 1, "pupil_id": 1003}] -------------------------------------------------------------------------------- /lesson20/lesson20.exercise02/teacher.py: -------------------------------------------------------------------------------- 1 | class Teacher: 2 | def __init__(self, first_name="", surname="", teacher_id=-1): 3 | self.first_name = first_name 4 | self.surname = surname 5 | self.teacher_id = teacher_id 6 | 7 | def from_dict(self, teacher_dict): 8 | self.first_name = teacher_dict["first_name"] 9 | self.surname = teacher_dict["surname"] 10 | self.teacher_id = teacher_dict["teacher_id"] 11 | 12 | def to_dict(self): 13 | teacher_dict = {"first_name": self.first_name, 14 | "surname": self.surname, 15 | "teacher_id":self.teacher_id} 16 | return teacher_dict 17 | 18 | def __str__(self): 19 | st = f"Name : {self.first_name}" 20 | st += f"\nSurname: {self.surname}" 21 | st += f"\nid : {self.teacher_id}" 22 | return st 23 | -------------------------------------------------------------------------------- /lesson20/lesson20.exercise02/teachers.json: -------------------------------------------------------------------------------- 1 | [{"first_name": "Severus", "surname": "Snape", "teacher_id": 1001}, {"first_name": "Charles", "surname": "Xavier", "teacher_id": 1002}, {"first_name": "Sergio", "surname": "Marquina", "teacher_id": 1003}, {"first_name": "AA", "surname": "AA", "teacher_id": 1004}] -------------------------------------------------------------------------------- /lesson20/python20.testing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/psounis/python/27a51336f9b778106b36fae540b9206f536da271/lesson20/python20.testing.pdf -------------------------------------------------------------------------------- /lesson20/queue.py: -------------------------------------------------------------------------------- 1 | class Queue: 2 | def __init__(self): 3 | self.array = [] 4 | 5 | def enqueue(self, elem): 6 | self.array.append(elem) 7 | 8 | def dequeue(self): 9 | if not self.array: 10 | return None 11 | else: 12 | return self.array.pop(0) 13 | 14 | def __str__(self): 15 | return ", ".join(self.array) 16 | 17 | def __add__(self, other): 18 | new_q = Queue() 19 | new_q.array = self.array[:] 20 | new_q.enqueue(other) 21 | return new_q 22 | 23 | def __iadd__(self, other): 24 | self.enqueue(other) 25 | return self 26 | 27 | def __neg__(self): 28 | return self.dequeue() 29 | 30 | def __len__(self): 31 | return len(self.array) -------------------------------------------------------------------------------- /lesson20/teacher.py: -------------------------------------------------------------------------------- 1 | class Teacher: 2 | def __init__(self, first_name="", surname="", teacher_id=-1): 3 | self.first_name = first_name 4 | self.surname = surname 5 | self.teacher_id = teacher_id 6 | 7 | def from_dict(self, teacher_dict): 8 | self.first_name = teacher_dict["first_name"] 9 | self.surname = teacher_dict["surname"] 10 | self.teacher_id = teacher_dict["teacher_id"] 11 | 12 | def to_dict(self): 13 | teacher_dict = {"first_name": self.first_name, 14 | "surname": self.surname, 15 | "teacher_id":self.teacher_id} 16 | return teacher_dict 17 | 18 | def __str__(self): 19 | st = f"Name : {self.first_name}" 20 | st += f"\nSurname: {self.surname}" 21 | st += f"\nid : {self.teacher_id}" 22 | return st 23 | -------------------------------------------------------------------------------- /lesson20/test_args.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from args import my_sum # import function to be tested 3 | 4 | 5 | class MySumTestCase(unittest.TestCase): 6 | def test_1(self): 7 | self.assertEqual(my_sum(1, 2, 3), 6) 8 | 9 | def test_2(self): 10 | self.assertEqual(my_sum(), 0) 11 | -------------------------------------------------------------------------------- /lesson20/test_teacher.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from teacher import Teacher 3 | 4 | 5 | class TeacherTestCase(unittest.TestCase): 6 | def setUp(self) -> None: 7 | self.bush = Teacher("George", "Bush") 8 | self.clinton = Teacher("Bill", "Clinton", 1001) 9 | 10 | def test_from_dict(self): 11 | dict_arg = { 12 | "first_name": "George", 13 | "surname": "Bush", 14 | "teacher_id": -1 15 | } 16 | t = Teacher() 17 | t.from_dict(dict_arg) 18 | self.assertEqual(self.bush.first_name, t.first_name) 19 | self.assertEqual(self.bush.surname, t.surname) 20 | self.assertEqual(self.bush.teacher_id, t.teacher_id) 21 | 22 | def test_to_dict(self): 23 | dict_arg = { 24 | "first_name": "George", 25 | "surname": "Bush", 26 | "teacher_id": -1 27 | } 28 | 29 | self.assertEqual(self.bush.to_dict()["first_name"], dict_arg["first_name"]) 30 | self.assertEqual(self.bush.to_dict()["surname"], dict_arg["surname"]) 31 | self.assertEqual(self.bush.to_dict()["teacher_id"], dict_arg["teacher_id"]) 32 | -------------------------------------------------------------------------------- /lesson20/waiter.py: -------------------------------------------------------------------------------- 1 | from random import randrange, seed 2 | from datetime import datetime 3 | 4 | 5 | class Person: 6 | def __init__(self, full_name, salary): 7 | self.full_name = full_name 8 | self.salary = salary 9 | self.served_cnt = 0 10 | 11 | def report(self): 12 | print(self.full_name + " served " + str(self.served_cnt) + " customers.") 13 | 14 | class Waiter(Person): 15 | def __init__(self, full_name, salary): 16 | Person.__init__(self, full_name, salary) 17 | 18 | def serve(self, customers, barista): 19 | self.served_cnt += customers 20 | print("Waiter " + self.full_name + " served " + str(customers) + " customers") 21 | barista.prepare(customers) 22 | 23 | 24 | class Barista(Person): 25 | def __init__(self, full_name, salary): 26 | Person.__init__(self, full_name, salary) 27 | 28 | def prepare(self, customers): 29 | print("Barista " + self.full_name + " served " + str(customers) + " customers") 30 | self.served_cnt += customers 31 | 32 | 33 | class Owner(Waiter, Barista): 34 | def __init__(self, full_name, salary): 35 | Person.__init__(self, full_name, salary) 36 | 37 | 38 | 39 | def main(): 40 | seed(datetime.now()) 41 | 42 | o = Owner("owner", 100000) 43 | w1 = Waiter("waiter-1", 200000) 44 | w2 = Waiter("waiter-1", 200000) 45 | b = Barista("barista", 300000) 46 | 47 | waiters = [o, w1, w2] 48 | baristas = [o, b] 49 | 50 | for _ in range(10): 51 | waiters[randrange(3)].serve(randrange(1,5+1), baristas[randrange(2)]) 52 | 53 | print("") 54 | o.report() 55 | w1.report() 56 | w2.report() 57 | b.report() 58 | 59 | main() 60 | 61 | --------------------------------------------------------------------------------