ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ

Slides:



Advertisements
Παρόμοιες παρουσιάσεις
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ Εισαγωγή στην Ιστορία και Θεωρία της Τέχνης Επιφάνεια και βάθος - Απόλυτη και Σχετική Σαφήνεια. Διδάσκων.
Advertisements

ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ Εισαγωγή στην Πληροφορική και Εκπαίδευση - Νέες Τεχνολογίες Παράλληλος προγραμματισμός Διδάσκουσα: Καθηγήτρια.
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ Εκπαιδευτικά Προγράμματα με Χρήση Η/Υ ΙΙ Θέμα «παιγνίδια» (website address) Διδάσκουσα: Καθηγήτρια Τζένη.
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΜΥΥ105: Εισαγωγή στον Προγραμματισμό
Τεχνολογία και Προγραμματισμός Υπολογιστών
Αντικειμενοστραφής Προγραμματισμός Ι
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Εργαστήριο 9 : Scratch (Μέρος 9_Β) Δημήτριος Νικολός ΤΕΕΑΠΗ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Αρχεσ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ Η/Υ ΤΑξη Β΄
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Εισαγωγή στους Η/Υ Ενότητα 11: Αλγεβρικές πράξεις στους Η/Υ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Εισαγωγή στους Η/Υ Ενότητα 12: Το διάγραμμα ροής και η λειτουργία του
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Διδάσκων: Γεώργιος Στεφανίδης
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ
Μεταγράφημα παρουσίασης:

ΠΑΝΕΠΙΣΤΗΜΙΟ ΙΩΑΝΝΙΝΩΝ ΑΝΟΙΚΤΑ ΑΚΑΔΗΜΑΪΚΑ ΜΑΘΗΜΑΤΑ Eισαγωγή στον προγραμματισμό Έλεγχος ροής προγράμματος (Έλεγχος Ροής) Διδάσκων: Ν. Μαμουλής

Άδειες Χρήσης Το παρόν εκπαιδευτικό υλικό υπόκειται σε άδειες χρήσης Creative Commons. Για εκπαιδευτικό υλικό, όπως εικόνες, που υπόκειται σε άλλου τύπου άδειας χρήσης, η άδεια χρήσης αναφέρεται ρητώς.

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

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

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

Έλεγχος ροής με 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

Έλεγχος ροής με 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!')

Έλεγχος ροής με 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!')

Έλεγχος ροής με 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!')

Έλεγχος ροής με 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')

Έλεγχος ροής με 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')

Φωλιασμένα 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')

Συγκρίσεις >>> 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 μόνο αν οι μεταβλητές δείχνουν στο ίδιο αντικείμενο.

Τελεστές σύγκρισης 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.

Λεξικογραφική σειρά >>> "alpha" < "beta" True >>> [1, 2] < [2, 1] >>> [2, [1, 4]] < [2, [1, 5]]

Παραδείγματα 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".')

Παραδείγματα 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!')

Παραδείγματα (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!')

Εναλλακτικοί τρόποι χρήσης 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

Άσκηση Τί πρόβλημα έχει ο παρακάτω κώδικας; Πώς θα φτιάξουμε το πρόγραμμά μας; 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')

Άσκηση Είσοδος: ένας αριθμός από το 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')

Άσκηση Είσοδος: ένας αριθμός από το 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])

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

Ο βρόγχος 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().

Ο βρόγχος 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)

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

Άσκηση Τύπωσε τους ζυγούς αριθμούς μέχρι το 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)

Άσκηση Δίνεται μια λίστα lst αριθμών. Βρές το γινόμενό τους. prod = 1 for x in lst: prod *= x print(prod) το ίδιο με prod = prod*x

Επαναλήψεις σε λεξικά Εναλλακτικά: 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) Υπενθύμηση: στα λεξικά δεν υπάρχει συγκεκριμένη σειρά προσπέλασης των ζευγαριών.

Παράλληλες επαναλήψεις 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

Παράλληλες επαναλήψεις - 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 σταματά στην κοντύτερη σειρά.

Παράλληλες επαναλήψεις - 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 12 3 4 beth 45 45 3

Διακοπή βρόγχου με 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 ακέραιος;

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!" μόνη πιθανή έξοδος

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!"

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

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

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

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

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

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

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

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

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

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

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

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

Άσκηση Ποια η διαφορά μεταξύ των 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!"

Άσκηση Γράψε το ίδιο πρόγραμμα χωρίς χρήση 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.")

Άσκηση Δίνεται μια λίστα 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!') έλεγχος αν υπαρχει στοιχείο μεγαλύτερο από το επόμενο

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 Δες το στο http://pythontutor.com/visualize.html

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

Χρήση 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

Σύνθεση λίστας με 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] >>> [(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']

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

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

Η συνάρτηση 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]

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

Τέλος Ενότητας

Χρηματοδότηση Το παρόν εκπαιδευτικό υλικό έχει αναπτυχθεί στα πλαίσια του εκπαιδευτικού έργου του διδάσκοντα. Το έργο «Ανοικτά Ακαδημαϊκά Μαθήματα στο Πανεπιστήμιο Ιωαννίνων» έχει χρηματοδοτήσει μόνο τη αναδιαμόρφωση του εκπαιδευτικού υλικού. Το έργο υλοποιείται στο πλαίσιο του Επιχειρησιακού Προγράμματος «Εκπαίδευση και Δια Βίου Μάθηση» και συγχρηματοδοτείται από την Ευρωπαϊκή Ένωση (Ευρωπαϊκό Κοινωνικό Ταμείο) και από εθνικούς πόρους.

Σημειώματα

Σημείωμα Ιστορικού Εκδόσεων Έργου Το παρόν έργο αποτελεί την έκδοση 1.0. Έχουν προηγηθεί οι κάτωθι εκδόσεις: Έκδοση 1.0 διαθέσιμη εδώ. http://ecourse.uoi.gr/course/view.php?id=1179 .

Σημείωμα Αναφοράς Copyright Πανεπιστήμιο Ιωαννίνων, Διδάσκων: Ν. Μαμουλής. «Εισαγωγή στον Προγραμματισμό. Έλεγχος ροής προγράμματος (Έλεγχος Ροής)». Έκδοση: 1.0. Ιωάννινα 2014. Διαθέσιμο από τη δικτυακή διεύθυνση: http://ecourse.uoi.gr/course/view.php?id=1179 .

Σημείωμα Αδειοδότησης Το παρόν υλικό διατίθεται με τους όρους της άδειας χρήσης Creative Commons Αναφορά Δημιουργού - Παρόμοια Διανομή, Διεθνής Έκδοση 4.0 [1] ή μεταγενέστερη. [1] https://creativecommons.org/licenses/by-sa/4.0/