Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό

Παρόμοιες παρουσιάσεις


Παρουσίαση με θέμα: "ΜΥΥ105: Εισαγωγή στον Προγραμματισμό"— Μεταγράφημα παρουσίασης:

1 ΜΥΥ105: Εισαγωγή στον Προγραμματισμό
Έλεγχος Ροής Προγράμματος Χειμερινό Εξάμηνο 2016

2 Ροή προγράμματος Μέχρι τώρα έχουμε δει προγράμματα απλής ροής
Οι εντολές εκτελούνται η μία μετά την άλλη σύμφωνα με την ακολουθία γραμμών του προγράμματος Σε τυπικά προγράμματα η ροή είναι πιο σύνθετη Χρήση συνθηκών για τον έλεγχο ροής Χρήση επαναλήψεων

3 Διαγράμματα Ροής Τρόπος σχεδιασμού ενός αλγορίθμου
Ο αλγόριθμος είναι το σχέδιο του προγράμματος Βασικά μέρη ενός διαγράμματος ροής temp > 30 no print(x) yes απόφαση εντολή ροή

4 Έλεγχος ροής με if if δυαδική έκφραση : μπλοκ εντολών
οι εντολές στο μπλοκ είναι στοιχισμένες 4 θέσεις δεξιά temp = eval(input('Enter the current temperature: ')) if temp > 25: print('It is hot!') print('Be sure to drink liquids.') temp = eval(input('Enter the current temperature: ')) temp > 25 no print('It is hot!') print('Be sure to drink liquids.') yes

5 Έλεγχος ροής με if if δυαδική έκφραση : μπλοκ εντολών
temp = eval(input('Enter the current temperature: ')) if temp > 25: print('It is hot!') print('Be sure to drink liquids.') print('Goodbye!') μετά / έξω από το μπλόκ, εκτελείται ανεξάρτητα από την έκβαση της if temp = eval(input('Enter the current temperature: ')) temp > 25 no print('It is hot!') print('Be sure to drink liquids.') yes print('Goodbye!')

6 Έλεγχος ροής με if/else
temp = eval(input('Enter the current temperature: ')) if temp > 25: print('It is hot!') print('Be sure to drink liquids.') else: print('It is not hot.') print('Bring a jacket.') print('Goodbye!')

7 Έλεγχος ροής με if/else
temp = eval(input('Enter the current temperature: ')) temp > 25 no print('It is hot!') print('Be sure to drink liquids.') yes print('It is not hot.') print('Bring a jacket.') print('Goodbye!')

8 Έλεγχος ροής με if/elif/else
x = eval(input('Enter a number: ')) if x > 0: print('The number is positive') elif x < 0: print('The number is negative') else: print('The number is zero')

9 Έλεγχος ροής με if/elif/else
θέλουμε! Το elif σημαίνει ‘else if’ if δυαδική έκφραση : μπλοκ εντολών elif δυαδική έκφραση : μπλοκ εντολών else : μπλοκ εντολών x = eval(input('Enter a number: ')) yes x > 0 no yes print('The number is positive') x < 0 no print('The number is negative') print('The number is zero')

10 Φωλιασμένα if x = eval(input('Enter a number: ')) yes yes x > 0
print('A large positive number') else : print('The number is positive') elif x < 0: print('The number is negative') else: print('The number is zero') x = eval(input('Enter a number: ')) yes yes x > 0 x > 1000 no no yes x < 0 print('A large positive number') no print('The number is negative') print('The number is positive') print('The number is zero')

11 Συγκρίσεις >>> x = [1,2,3] >>> a = 3
>>> y = [1,2,3] >>> x == y True >>> x is y False >>> z = x >>> z is x >>> a = 3 >>> 0<a<5 True >>> b = 3 >>> a ==b >>> a is b >>> s1 = 'hello' >>> s2 = 'hello' >>> s1 is s2 Για λίστες, πλειάδες, λεξικά, True μόνο αν οι μεταβλητές δείχνουν στο ίδιο αντικείμενο Το == ελέγχει αν οι λίστες έχουν τα ίδια στοιχεία.

12 Τελεστές σύγκρισης x == y το x ισούται με το y.
x is y x και y είναι το ίδιο αντικείμενο. x is not y x και y είναι διαφορετικά αντικείμενα. x in y το x είναι μέλος της ακολουθίας y. x not in y το x δεν είναι μέλος της ακολουθίας y.

13 Λεξικογραφική σειρά >>> "alpha" < "beta" True
>>> [1, 2] < [2, 1] >>> [2, [1, 4]] < [2, [1, 5]] Σαν να συγκρίνουμε το 12 με το 21 ή το 214 με το 215 >>> [2,1,4] < ['a','b',1] Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> [2,1,4] < ['a','b',1] TypeError: unorderable types: int() < str()

14 Παραδείγματα name = input('What is your name? ') if 's' in name: print('Your name contains the letter "s".') else: print('Your name does not contain the letter "s".')

15 Παραδείγματα number = int(input('Enter a number between 1 and 10: '))
if number <= 10: if number >= 1: print('Great!') else: print('Wrong!') number = int(input('Enter a number between 1 and 10: ')) if 1<=number<=10: print('Great!') else: print('Wrong!')

16 Παραδείγματα (and, or, not)
number = int(input('Enter a number between 1 and 10: ')) if number>=1 and number<=10: print('Great!') else: print('Wrong!') number = int(input('Enter a number between 1 and 10: ')) if number<1 or number>10: print('Wrong!') else: print('Great!') number = int(input('Enter a number between 1 and 10: ')) if number>=1 and not number>10: print('Great!') else: print('Wrong!')

17 Εναλλακτικοί τρόποι χρήσης if
>>> name = input('Please enter your name: ') or '<unknown>' Please enter your name: >>> name '<unknown>' Please enter your name: Joe 'Joe' επιστρέφει το or μόνο αν το input() είναι κενό (False) False είναι τα ακόλουθα: 0,'', (), {}, [], None το ίδιο με: if y>1: x=5 else: x=0 >>> y=0 >>> x = 5 if y>1 else 0 >>> x Γράψτε μια γραμμή κώδικα που βρίσκει τον μέγιστο δύο αριθμών (χωρίς το max)

18 Άσκηση Τί πρόβλημα έχει ο παρακάτω κώδικας;
Πώς θα φτιάξουμε το πρόγραμμά μας; temp = int(input('Please input the current temperature: ')) if temp > 5: print('It is cool') elif temp > 25: print('It is hot!') else: print('It is freezing!') δεν θα εκτελεστεί ποτε! πρέπει να προσέχουμε τη σειρά με την οποία βάζουμε τις συνθήκες! ... if temp > 25: print('It is hot!') elif temp > 5: print('It is cool')

19 Άσκηση Είσοδος: ένας αριθμός από το 1 ως το 4
Έξοδος: τύπωσε ‘John’ αν είναι 1, ‘Paul’ αν είναι 2, ‘Maria’ αν είναι 3, ‘Sue’ αν είναι 4 c = int(input('Please input a number between 1 and 4: ')) if c == 1: print('John') elif c == 2: print('Paul') elif c == 3: print('Maria') elif c == 4: print('Sue')

20 Άσκηση Είσοδος: ένας αριθμός από το 1 ως το 4
Έξοδος: τύπωσε ‘John’ αν είναι 1, ‘Paul’ αν είναι 2, ‘Maria’ αν είναι 3, ‘Sue’ αν είναι 4 Χωρίς να χρησιμοποιήσετε if ! d = {1:'John',2:'Paul',3:'Maria',4:'Sue'} c = int(input('Please input a number between 1 and 4: ')) print(d[c])

21 Επαναλήψεις (loops) Στα προγράμματά μας μπορεί να θέλουμε να τρέξουμε την ίδια σειρά εντολών πολλές φορές με πιθανές παραμετροποιημένες μικροαλλαγές σε κάθε επανάληψη είτε γνωρίζοντας πόσες φορές (for) είτε ενόσω μια συνθήκη είναι αληθής (while) x = 1 no yes x <= 100 print(x) x = x + 1

22 Ο βρόγχος while Eνόσω η συνθήκη είναι αληθής εκτελείται το μπλοκ εντολών που είναι στοιχισμένες κάτω της x =1 while x <= 100: print(x) x += 1 κενό αλφαριθμητικό True, αν το name είναι κενό name = '' while not name: name = input('Please enter your name: ') print('Hello, %s!' % name) Αν θέλουμε το διάστημα ' ' να μην είναι αποδεκτό σαν όνομα, βάζουμε: while not name or name.isspace() ή while not name.strip()

23 Ο βρόγχος for Χρησιμοποιείται αν θέλουμε να επαναλάβουμε ένα μπλοκ κώδικα, για κάθε τιμή ενός συνόλου numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for x in numbers: print(x) words = ['this', 'is', 'an', 'ex', 'parrot'] for x in words: print(x[-1]) ορίζει ένα διάστημα τιμών, όπως και στο slicing for x in range(1,101): print(x)

24 Άσκηση Γράψτε ένα πρόγραμμα που θα παίρνει ένα αλφαριθμητικό από το χρήστη και θα τυπώνει τα φωνήεντα του s = input('Please input a string: ') for c in s: if c in 'aeoiu': print(c)

25 Άσκηση Τύπωσε τους ζυγούς αριθμούς μέχρι το 10
range(11)=range(0,11)= [0,1,2,3,4,5,6,7,8,9,10] for x in range(11): if x % 2 == 0: print(x) for x in range(0,11,2): print(x)

26 Άσκηση Δίνεται μια λίστα lst αριθμών. Βρές το γινόμενό τους
Πως θα υπολογίσουμε το n! ? lst = [3,4,6,5] prod = 1 for x in lst: prod *= x print(prod) το ίδιο με prod = prod*x n = eval(input(“n:”)) prod = 1 for x in range(n): prod *= x+1 print(prod)

27 Επαναλήψεις σε λεξικά Εναλλακτικά: d = {'x': 1, 'y': 2, 'z': 3}
for key in d: print(key, 'corresponds to', d[key]) z corresponds to 3 y corresponds to 2 x corresponds to 1 Εναλλακτικά: d = {'x': 1, 'y': 2, 'z': 3} for key, value in d.items(): print(key, 'corresponds to', value) Υπενθύμηση: στα λεξικά δεν υπάρχει συγκεκριμένη σειρά προσπέλασης των ζευγαριών

28 Παράλληλες επαναλήψεις
names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] for i in range(len(names)): print (names[i], 'is', ages[i], 'years old') range(4)=range(0,4) anne is 12 years old beth is 45 years old george is 32 years old damon is 102 years old

29 Παράλληλες επαναλήψεις - zip
names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] for name, age in zip(names, ages): print(name, 'is', age, 'years old') anne is 12 years old beth is 45 years old george is 32 years old damon is 102 years old names = ['anne', 'beth', 'george', 'damon', 'paul', 'sue'] ages = [12, 45, 32, 102] for name, age in zip(names, ages): print(name, 'is', age, 'years old') Τι θα τυπώσει; Το ίδιο! Η zip σταματά στην κοντύτερη σειρά

30 Παράλληλες επαναλήψεις - zip
names = ['anne', 'beth', 'george', 'damon'] ages = [12, 45, 32, 102] lst = (3,45,66) d = {'1':3, '2':4} for n,a,l,di in zip(names, ages, lst, d): print(n,a,l,d[di]) anne beth

31 Διακοπή βρόγχου με break
Χρησιμοποιούμε break όταν θέλουμε να σταματήσουμε ένα loop Π.χ. βρες το μεγαλύτερο ακέραιο τετράγωνο < 100 from math import sqrt for n in range(99, 0, -1): root = sqrt(n) if root == int(root): print (n) break κατέβασμα από το 99 στο 1 με αρνητικό βήμα (-1) Π.χ. προσπαθήστε: list(range(0,100,5)) είναι το root ακέραιος;

32 While True με break Όταν θέλουμε να βγούμε από το βρόγχο ακριβώς μόλις ικανοποιηθεί μια συνθήκη εξόδου, χωρίς να εκτελέσουμε τις υπόλοιπες εντολές του βρόγχου πάντα αληθές while True: x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!" μόνη πιθανή έξοδος

33 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!"

34 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40

35 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 x == 40

36 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! x == 40

37 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! x == 40

38 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 x == 40

39 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 x == 60

40 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 x == 60

41 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 x == 60

42 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 How many states are in the U.S.? 50 x == 60

43 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 How many states are in the U.S.? 50 x == 50

44 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 How many states are in the U.S.? 50 Correct! There are 50 states in the U.S. x == 50

45 While True με break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!") How many states are in the U.S.? 40 The answer is NOT correct! How many states are in the U.S.? 60 How many states are in the U.S.? 50 Correct! There are 50 states in the U.S. x == 50

46 Άσκηση Ποια η διαφορά μεταξύ των 2 προγραμμάτων; while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; else: print("The answer is NOT correct!" while True: x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; print("The answer is NOT correct!"

47 Άσκηση Γράψε το ίδιο πρόγραμμα χωρίς χρήση break while True:
x = int(input("How many states are in the U.S.? ")) if x == 50: print("Correct! There are 50 states in the U.S.") break; print("The answer is NOT correct!" x = 0 while x !=50 : x = int(input("How many states are in the U.S.? ")) if x !=50 : print("The answer is NOT correct!") print("Correct! There are 50 states in the U.S.")

48 Άσκηση Δίνεται μια λίστα lst. Γράψε ένα πρόγραμμα που να ελέγχει αν η λίστα είναι ταξινομημένη βοηθητική μεταβλητή lst = [3,4,6,5,7] is_sorted = True for i in range(0,len(lst)-1): if lst[i] > lst[i+1]: print('list',lst, 'is not sorted!') is_sorted = False break if is_sorted: print('list' , lst , 'is sorted!') έλεγχος αν υπαρχει στοιχείο μεγαλύτερο από το επόμενο

49 H break σπάει τον εσωτερικότερο βρόγχο
Τι θα τυπώσει το παρακάτω πρόγραμμα; for i in range(3): for j in range(3): print(i,j) if j==1: break 0 0 0 1 1 0 1 1 2 0 2 1 Δες το στο

50 Η εντολή continue Τερματίζει την τρέχουσα επανάληψη και πάει στην επόμενη Παράδειγμα: τύπωσε όλους τους χαρακτήρες ενός αλφαριθμητικού, εκτός από τα φωνήεντα s = input('Please input a string: ') for c in s: if c in 'aeoiu': continue print(c) Πήγαινε κατευθείαν στην επόμενη επανάληψη

51 Χρήση else στη for Εκτελείται μόνο όταν το for-loop δεν έχει «σπάσει» λόγω break from math import sqrt for n in range(99, 90, -1): root = sqrt(n) if root == int(root): print (n) break else: print("Didn't find it") Εκτελείται αν το for τερματίσει χωρίς break

52 Σύνθεση λίστας με for/if
>>> [x*x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> [x*x for x in range(10) if x % 3 == 0] [0, 9, 36, 81] List comprehension >>> [(x, y) for x in range(3) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] >>> girls = ['alice', 'bernice', 'clarice'] >>> boys = ['chris', 'arnold', 'bob'] >>> [b+'+'+g for b in boys for g in girls if b[0] == g[0]] ['chris+clarice', 'arnold+alice', 'bob+bernice']

53 Η εντολή pass Χρησιμοποιείται αν δεν θέλουμε να κάνουμε τίποτα σε ένα block if name == 'Ralph Auldus Melish': print 'Welcome!' elif name == 'Enid': # Not finished yet... pass elif name == 'Bill Gates': print 'Access Denied'

54 Άσκηση Τι θα κάνουν τα παρακάτω προγράμματα; for i in range(10):
if i % 2 == 0: pass else: print(i) for i in range(10): pass else: print('ok')

55 Η συνάρτηση enumerate Για κάθε στοιχείο μιας ακολουθίας (λίστας, πλειάδας, κλπ.), επιστρέφει το στοιχείο και ένα δείκτη στη θέση του. Π.χ. τύπωσε τα διακριτά στοιχεία μιας λίστας lst = [1,2,4,5,6,3,2,3,6,2,6,3,2] lst2 = [] for idx,elem in enumerate(lst): if elem not in lst[0:idx]: lst2.append(elem) print(lst2) Αν δεν εμφανίζεται στα προηγούμενα [1, 2, 4, 5, 6, 3]

56 Σημαντικό! Εξασκηθείτε στο να «τρέχετε» ένα πρόγραμμα μόνοι σας και στο να παρακολουθείτε τις τιμές των μεταβλητών και τη ροή του προγράμματος Θα σας βοηθήσει και ο Python visualizer:


Κατέβασμα ppt "ΜΥΥ105: Εισαγωγή στον Προγραμματισμό"

Παρόμοιες παρουσιάσεις


Διαφημίσεις Google