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

Σχετικά έγγραφα
ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Έλεγχος Ροής Προγράμματος

Πληρουορική Γ Γσμμασίοσ

Διάλεξη 1. Πράξεις Τελεστές Έλεγχος Ροής

ΠΑΝΕΠΙΣΤΗΜΙΟ AΙΓΑIΟΥ & ΑΕΙ ΠΕΙΡΑΙΑ Τ.Τ. Τμήματα Ναυτιλίας και Επιχειρηματικών Υπηρεσιών & Μηχ. Αυτοματισμού ΤΕ. Εισαγωγή στη Python

Γλώσσες Προγραμματισμού Εφαρμογών - ΜΕΠΒ20

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Σφάλματα, εξαιρέσεις, εκσφαλμάτωση Χειμερινό Εξάμηνο 2016

Λογικές εκφράσεις και μεταβλητές

ΗΥ-150. Προγραµµατισµός. Εντολές Ελέγχου Ροής

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Σφάλµατα, εξαιρέσεις, εκσφαλµάτωση Χειµερινό Εξάµηνο 2014

ΗΥ-150. Προγραμματισμός

Εισαγωγή στη Matlab 2 Εισαγωγή στην Αριθμητική Ανάλυση Διδάσκων: Γεώργιος Ακρίβης Βοηθός: Δημήτριος Ζαβαντής

for for for for( . */

ΗΥ-150. Προγραμματισμός

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

Εισαγωγή στην Επιστήμη Υπολογιστών. Εισαγωγή στην Python

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Λεξικά Χειµερινό Εξάµηνο 2014

ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΥΠΟΛΟΓΙΣΤΩΝ & ΥΠΟΛΟΓΙΣΤΙΚΗ ΦΥΣΙΚΗ

Κεφάλαιο : Επαναλήψεις (for, do-while)

ΕΞΕΤΑΖΟΜΕΝΟ ΜΑΘΗΜΑ: ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΥΠΟΛΟΓΙΣΤΩΝ ΣΥΝΟΛΟ ΣΕΛΙΔΩΝ: ΕΞΙ (6)

if(συνθήκη) {... // οµάδα εντολών } C: Από τη Θεωρία στην Εφαρµογή 5 ο Κεφάλαιο

Ενότητα: Δυαδική Αναζήτηση Σχέδιο Δραστηριότητας: Παιχνίδι: Βρες τον αριθμό

Εξαιρέσεις Εισαγωγή. Εξαιρέσεις. ηµήτρης Λεβεντέας

Άσκηση 1. Α. Υπολογίστε χωρίς να εκτελέσετε κώδικα FORTRAN τα παρακάτω: Ποιά είναι η τελική τιμή του Z στα παρακάτω κομμάτια κώδικα FORTRAN:

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Λεξικά Χειμερινό Εξάμηνο 2016

Εισαγωγή στον Προγραμματισμό

Στόχοι και αντικείμενο ενότητας. Προτάσεις επανάληψης. Έλεγχος ροής προγράμματος. #5.. Εντολές Επανάληψης

Εισαγωγή στην C. Μορφή Προγράµµατος σε γλώσσα C

ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΥΠΟΛΟΓΙΣΤΩΝ & ΥΠΟΛΟΓΙΣΤΙΚΗ ΦΥΣΙΚΗ

Θέματα Προγραμματισμού Η/Υ

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Επαναληπτικές Ασκήσεις Χειµερινό Εξάµηνο 2014

Σημειωματάριο Δευτέρας 9 Οκτ. 2017

print( x is positive ) Στο διερμηνευτή Python, προσοχή στη στοίχιση, διότι γίνεται από το χρήστη:

ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΥΠΟΛΟΓΙΣΜΟΣ Εντολές επιλογής και αποφάσεων 1 ο Φύλλο Εργασιών Εισαγωγικές ασκήσεις για την εντολή if ΑΠΑΝΤΗΣΕΙΣ

ΕισαγωγήστουςΗ/Υ. PHP Hypertext Preprocessor

Είδη εντολών. Απλές εντολές. Εντολές ελέγχου. Εκτελούν κάποια ενέργεια. Ορίζουν τον τρόπο με τον οποίο εκτελούνται άλλες εντολές

ΠΛΗΡΟΦΟΡΙΚΗ Ι JAVA Τμήμα θεωρίας με Α.Μ. σε 3, 7, 8 & 9 22/11/07

ΣΤΗΛΗ Β ΑΠΟΤΕΛΕΣΜΑ 1. float(10) α pow(2,3) β abs(-10) γ int(5.6) δ. 10 ε. 5.6 Μονάδες 8 ΣΤΗΛΗ Α ΣΥΝΑΡΤΗΣΗ

Στη C++ υπάρχουν τρεις τύποι βρόχων: (a) while, (b) do while, και (c) for. Ακολουθεί η σύνταξη για κάθε μια:

Σημειώσεις του εργαστηριακού μαθήματος Πληροφορική ΙΙ. Εισαγωγή στην γλώσσα προγραμματισμού

Προγραµµατισµός στην Basic

Σημειωματάαριο Δευτέρας 16 Οκτ. 2017

Ο βρόχος for Η εντολή for χρησιμοποιείται για τη δημιουργία επαναληπτικών βρόχων στη C

Σημειωματάριο Τετάρτης 18 Οκτ. 2017

Ο πιο κάτω πίνακας περιγράφει σε ποιες περιπτώσεις χρησιμοποιούμε τους τρεις πιο πάνω τρόπους:

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Αλφαριθμητικά Χειμερινό Εξάμηνο 2016

Στόχοι και αντικείμενο ενότητας. Εκφράσεις. Η έννοια του τελεστή. #2.. Εισαγωγή στη C (Μέρος Δεύτερο) Η έννοια του Τελεστή

Εισαγωγή στους Η/Υ και τις Εφαρμογές Ενότητα 5: Επεξεργασία δεδομένων με τη γλώσσα προγραμματισμού python Υπο-ενότητα 5.2: Συμβολοσειρές-Έλεγχος Ροής

Διαδικασιακός Προγραμματισμός

Διάλεξη 5η: Εντολές Επανάληψης

Επιλογή και επανάληψη. Λογική έκφραση ή συνθήκη

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Αναδροµικές Συναρτήσεις Χειµερινό Εξάµηνο 2014

ΕΝΟΤΗΤΑ 4 Λήψη Αποφάσεων και Συναρτήσεις Ελέγχου

Εντολές Επανάληψης. int sum = 0, i=1; sum += i++ ; sum += i++ ; Η πράξη αυτή θα πρέπει να επαναληφθεί Ν φορές!

Διαδικασιακός Προγραμματισμός

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Εισαγωγή στη Java II

Προγραμματισμός ΗΥ και Υπολογιστική Φυσική. Χρήστος Γκουμόπουλος

Θέματα Προγραμματισμού Η/Υ

Προσέξτε πως η παράσταση μπορεί να περιλαμβάνει όχι μόνο μία τιμή ή μια άλλη μεταβλητή αλλά μια ολόκληρη μαθηματική παράσταση, όπως η εξής:

Κεφάλαιο 5ο: Εντολές Επανάληψης

Φύλλο Εργασίας 3. Μια γρήγορη επανάληψη από τα προηγούμενα

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Δημιουργώντας δικές μας Κλάσεις και Αντικείμενα

Κεφάλαιο : Επαναλήψεις (oι βρόγχοιfor, do-while) (Διάλεξη 10) Εντολές Επανάληψης που θα καλυφθούν σήμερα

Κεφάλαιο : Επαναλήψεις (oι βρόχος While) ( ιάλεξη 9) ιδάσκων: ηµήτρης Ζεϊναλιπούρ

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Λίστες και Πλειάδες Χειµερινό Εξάµηνο 2014

Γιώργος Μανής Επίκουρος Καθηγητής Τμήματος Μηχανικών Η/Υ και Πληροφορικής 3 Μαϊου 2015

ΑΡΧΗ 1ΗΣ ΣΕΛΙΔΑΣ Γ ΤΑΞΗΣ ΗΜΕΡΗΣΙΩΝ & Δ ΤΑΞΗΣ ΕΣΠΕΡΙΝΩΝ ΥΠΟΛΕΙΠΟΜΕΝΕΣ ΠΑΝΕΛΛΑΔΙΚΕΣ ΕΞΕΤΑΣΕΙΣ HMEΡΗΣΙΩΝ ΚΑΙ ΕΣΠΕΡΙΝΩΝ ΕΠΑΓΓΕΛΜΑΤΙΚΩΝ ΛΥΚΕΙΩΝ

Κεφάλαιο : Επαναλήψεις (o βρόγχος While) (Διάλεξη 9) Δομές Έλεγχου Προγράμματος

ΠΛΗΡΟΦΟΡΙΚΗ ΤΕΧΝΟΛΟΓΙΑ ΚΑΙ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ Η/Υ

Υπολογισμός - Εντολές Επανάληψης

Εισαγωγή στον Προγ/μό Υπολογιστών

11/23/2014. Στόχοι. Λογισμικό Υπολογιστή

Παράδειγμα. print (random.sample('christos drosos',3)) #3 στοιχεία από το δείγμα print (random.gauss(0,1)) #μέση τιμή 0 και απόκλιση 1

Συστήματα Γνώσης. Πρακτικό Κομμάτι Μαθήματος Συναρτήσεις στο CLIPS. Νίκος Βασιλειάδης, Αναπλ. Καθηγητής Τμήμα Πληροφορικής

Εισαγωγή στον Προγραμματισμό Python Μάθημα 3: πίνακες και βρόγχος επανάληψης for (για) Νοέμβριος 2014 Χ. Αλεξανδράκη, Γ.

Αλγοριθμική & Δομές Δεδομένων- Γλώσσα Προγραμματισμού Ι (PASCAL) (PASCAL ) Εντολές Ελέγχου & Επανάληψης

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Αναζήτηση και Ταξινόμηση Χειμερινό Εξάμηνο 2016

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Αναζήτηση και Ταξινόµηση Χειµερινό Εξάµηνο 2014

Αναφορά (1/2) Μπορούμε να ορίσουμε μια άλλη, ισοδύναμη αλλά ίσως πιο σύντομη, ονομασία για ποσότητα (μεταβλητή, σταθερή, συνάρτηση, κλπ.

Λειτουργικά. Συστήματα Ι. Φ ρ ο ν τ ι σ τ ή ρ ι ο. Αριστείδης Ηλίας. Εργαστήριο Ηλεκτρονικών Υπολογιστών

ΕΙΣΑΓΩΓΗ ΣΤΟΝ ΔΟΜΗΜΕΝΟ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ

Πληροφορική ΙΙ Θεματική Ενότητα 6

ΠΛΗΡΟΦΟΡΙΚΗ Ι JAVA Τμήμα θεωρίας με Α.Μ. σε 3, 7, 8 & 9 8/11/07

Εισαγωγή στην Επιστήμη Υπολογιστών. Εισαγωγή στην Python

Γλώσσα προγραμματισμού python

Εντολές επιλογής Επαναλήψεις (if, switch, while)

Η εντολή if-else. Η απλή μορφή της εντολής if είναι η ακόλουθη: if (συνθήκη) { Η γενική μορφή της εντολής ifelse. εντολή_1; εντολή_2;..

Προγραμματισμός Η/Υ. Ενότητα 5: Εντολές Επανάληψης

Εισαγωγή στον Προγραµµατισµό. Διάλεξη 3 η : Επίλυση Προβληµάτων Χειµερινό Εξάµηνο 2011

ΜΥΥ105: Εισαγωγή στον Προγραμματισμό. Επαναληπτικές Ασκήσεις Χειμερινό Εξάμηνο 2016

Ενότητα 3 ΕΙΣΑΓΩΓΗ ΣΤΗ ΓΛΩΣΣΑ PYTHON

Οι εντολές ελέγχου της ροής ενός προγράμματος.

Διάλεξη 2. Μεταβλητές - Δομές Δεδομένων - Eίσοδος δεδομένων - Έξοδος: Μορφοποίηση - Συναρτήσεις. Διοργάνωση : ΚΕΛ ΣΑΤΜ

Θέματα Προγραμματισμού Η/Υ

Δομημένος Προγραμματισμός. Τμήμα Επιχειρηματικού Σχεδιασμού και Πληροφοριακών Συστημάτων

Βασικάχαρακτηριστικάτηςγλώσσας. Πίνακες, Έλεγχος Ροής και Βρόχοι

Γραφικά υπολογιστών Εργαστήριο 1 Εισαγωγή στην Python

Εισαγωγή στον Προγ/μό Υπολογιστών

ΜΥΥ105: Εισαγωγή στον Προγραµµατισµό. Εισαγωγή στην Python Χειµερινό Εξάµηνο 2014

ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΥΠΟΛΟΓΙΣΤΩΝ Κεφάλαιο 10 : Εντολές επιλογής και αποφάσεων

2.1. Εντολές Σχόλια Τύποι Δεδομένων

Transcript:

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

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

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

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

Έλεγχος ροής µε if n 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 yes print('it is hot!') print('be sure to drink liquids.') print('goodbye!') 5

Έλεγχος ροής µε if/else n if δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) if temp > 25: print('it is hot!') print('be sure to drink liquids.') print('it is not hot.') print('bring a jacket.') print('goodbye!') 6

Έλεγχος ροής µε if/else n if δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών temp = eval(input('enter the current temperature: ')) no print('it is not hot.') print('bring a jacket.') temp > 25 yes print('it is hot!') print('be sure to drink liquids.') print('goodbye!') 7

Έλεγχος ροής µε if/elif/else n 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') print('the number is zero') 8

Έλεγχος ροής µε if/elif/else n if δυαδική έκφραση : µπλοκ εντολών elif δυαδική έκφραση : µπλοκ εντολών else : µπλοκ εντολών µπορούµε να βάλουµε όσα διαδοχικά elif θέλουµε! Το elif σηµαίνει else if x = eval(input('enter a number: ')) yes print('the number is negative') x > 0 no x < 0 no yes print('the number is positive') print('the number is zero') 9

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

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

Τελεστές σύγκρισης n x == y το x ισούται µε το y. n x < y το x είναι µικρότερο από το y. n x > y το x είναι µεγαλύτερο από το y. n x >= y το x είναι µεγαλύτερο ή ίσο από το y. n x <= y το x είναι µικρότερο ή ίσο από το y. n x!= y το x είναι διάφορο του y. n x is y x και y είναι το ίδιο αντικείµενο. n x is not y x και y είναι διαφορετικά αντικείµενα. n x in y το x είναι µέλος της ακολουθίας y. n x not in y το x δεν είναι µέλος της ακολουθίας y. 12

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

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

Παραδείγµατα number = int(input('enter a number between 1 and 10: ')) if number <= 10: if number >= 1: print('great!') print('wrong!') print('wrong!') number = int(input('enter a number between 1 and 10: ')) if 1<=number<=10: print('great!') print('wrong!') 15

Παραδείγµατα (and, or, not) number = int(input('enter a number between 1 and 10: ')) if number>=1 and number<=10: print('great!') print('wrong!') number = int(input('enter a number between 1 and 10: ')) if number<1 or number>10: print('wrong!') print('great!') number = int(input('enter a number between 1 and 10: ')) if number>=1 and not number>10: print('great!') print('wrong!') 16

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

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

Άσκηση n Είσοδος: ένας αριθµός από το 1 ως το 4 n Έξοδος: τύπωσε 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') 19

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

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

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

Ο βρόγχος for n Χρησιµοποιείται αν θέλουµε να επαναλάβουµε ένα µπλοκ κώδικα, για κάθε τιµή ενός συνόλου 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]) for x in range(1,101): print(x) ορίζει ένα διάστηµα τιµών, όπως και στο slicing 23

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

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

Άσκηση n Δίνεται µια λίστα lst αριθµών. Βρές το γινόµενό τους lst = [3,4,6,5] prod = 1 for x in lst: prod *= x print(prod) το ίδιο µε prod = prod*x 26

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

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

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

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

Διακοπή βρόγχου µε break n Χρησιµοποιούµε break όταν θέλουµε να σταµατήσουµε ένα loop n Π.χ. βρες το µεγαλύτερο ακέραιο τετράγωνο < 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 ακέραιος; 31

While True µε break n Όταν θέλουµε να βγούµε από το βρόγχο ακριβώς µόλις ικανοποιηθεί µια συνθήκη εξόδου, χωρίς να εκτελέσουµε τις υπόλοιπες εντολές του βρόγχου πάντα αληθές 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!" µόνη πιθανή έξοδος 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; 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; print("the answer is NOT correct!") How many states are in the U.S.? 40! 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; print("the answer is NOT correct!") How many states are in the U.S.? 40! x == 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; print("the answer is NOT correct!") How many states are in the U.S.? 40! The answer is NOT correct!! 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; 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; 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 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; 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 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; 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! The answer is NOT correct!! 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; 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! The answer is NOT correct!! 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; 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! The answer is NOT correct!! How many states are in the U.S.? 50! 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; 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! The answer is NOT correct!! How many states are in the U.S.? 50! x == 50 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; 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! The answer is NOT correct!! How many states are in the U.S.? 50! Correct! There are 50 states in the U.S.! 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; 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! The answer is NOT correct!! How many states are in the U.S.? 50! Correct! There are 50 states in the U.S.! x == 50 45

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

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

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

H break σπάει τον εσωτερικότερο βρόγχο n Τι θα τυπώσει το παρακάτω πρόγραµµα; 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 49

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

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

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

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

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

Η συνάρτηση enumerate n n Για κάθε στοιχείο µιας ακολουθίας (λίστας, πλειάδας, κλπ.), επιστρέφει το στοιχείο και ένα δείκτη στη θέση του. Π.χ. τύπωσε τα διακριτά στοιχεία µιας λίστας 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] 55

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