Introduction to programming in Python ubomr NAJDER 22

Introduction to programming in Python Ľubomír ŠNAJDER 22 th -28 st of May 2010, LLP-Erasmus, Technical University Radom, POLAND

Content n Basic characteristics of Python n Python versions, installation, portable version 1. 1 n Python as an interactive calculator n Pythons’ commands and data structures by simple examples (loops, conditions, functions, list, dictionaries) n Graphics and sounds in Python n Guess number game written in Python, Pascal, LOGO – code comparison

Basic characteristics of Python n easy to learn, it has elegant syntax, short code n powerful programming language, it has efficient high-level data structures (list, set, dictionary, tuple …) n simple but effective approach to object-oriented programming n interpreter - ideal language for scripting and rapid application development in many areas on most platforms

Basic characteristics of Python n its interpreter and the extensive standard library are freely available in source or binary form for all major platforms from http: //www. python. org/, where also distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation n its interpreter is easily extended with new functions and data types implemented in C or C++ n Python is also suitable as an extension language for customizable applications

Python versions, installation n Python 2 vs. Python 3 n some ideas from Python 3. 0, 3. 1 (3. 2) were backported to Python 2. 6 (2. 7=last version) n Python Portable 1. 1 (based on Python 2. 6. 1) http: //portablepython. com/wiki/Portable. Python 1. 1 Py 2. 6. 1

Python portable version 1. 1 n Py. Scripter (interpreter, editor, code explorer …)

Python as an interactive calculator n Numbers (integer, float, variables): n 20*3. 9520797 n 2**24 n 2/3 n -2/3 n 2/3. 0 n c=100 f=32+9/5. 0*c f round(_, 2) n n a, b = 1, 2 c=a a=b b=c n a, b = b, a Numbers (complex, variables): n 1 j*1 j n c=3+4 j c. real c. imag abs(c)

Python as an interactive calculator n Strings: n "doesn't" n 'doesn't' n chess = ““". . . . X. X. ““" chess print chess n contact = "name surnamenstreetncity“ print contact n word = "help"+"A“ word '<' + word*5 + '>' word[4] word[0: 2] word[2: 4] word[: 2] word[2: ] word[-1] word[-2: ] word[: -2] len(word) n u"Ľubo Šnajder". encode('utf-8') 'xc 4xbdubo xc 5xa 0 najder'

Pythons’ commands and data structures by simple examples n Short dialog with computer # This program says hello and asks for my name. print('Hello world!') print('What is your name? ') my. Name = raw_input() print('It is good to meet you, ' + my. Name) n Tabelation of the fuction SQRT for i in range(10): print i, math. sqrt(i) n Star triangel for riadok in range(10): for stlpec in range(1, riadok): print "*", print

Pythons’ commands and data structures by simple examples n School marks in Slovakia x = int(raw_input("Please enter an school mark: ")) if x == 1: print '1 -vyborne‘ elif x == 2: print '2 -chvalitebne‘ elif x == 3: print '3 -dobre‘ elif x == 4: print '4 -dostatocne‘ elif x == 5: print '5 -nedostatocne‘ else: print 'You did not enter a natural number from 1 to 5'

Pythons’ commands and data structures by simple examples n Fibonacci numbers<1000 a, b = 0, 1 while b < 1000: print b, a, b = b, a + b n Is primary number – function def isprime(n): b = True for x in range(2, n): if n % x == 0: b = False return b n Greatest common divisor of two numbers a, b – function def gcd(a, b): while a != b: if a < b: b=b-a else: a=a-b return a
![Lists in Python n mena=["Karol", "Agnieszka", "Michal", "Radek", "Lubo", "Zuzana ", "Maria"] print len(mena) Lists in Python n mena=["Karol", "Agnieszka", "Michal", "Radek", "Lubo", "Zuzana ", "Maria"] print len(mena)](http://slidetodoc.com/presentation_image_h2/eef2d76042f0a3774fd23aa02bd57bad/image-12.jpg)
Lists in Python n mena=["Karol", "Agnieszka", "Michal", "Radek", "Lubo", "Zuzana ", "Maria"] print len(mena) print len(mena[0]) for i in mena: print i print mena print ", ". join(mena)
![Lists in Python n mena. M=[] for i in mena: if i[0]=="M": mena. M. Lists in Python n mena. M=[] for i in mena: if i[0]=="M": mena. M.](http://slidetodoc.com/presentation_image_h2/eef2d76042f0a3774fd23aa02bd57bad/image-13.jpg)
Lists in Python n mena. M=[] for i in mena: if i[0]=="M": mena. M. append(i) print mena. M print sorted(mena) del mena[0] print mena nove_mena=["Peter", "Lucia"] print mena+nove_mena. extend(nove_mena) print mena
![Directories in Python n tel = {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140} tel['Kubove'] = Directories in Python n tel = {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140} tel['Kubove'] =](http://slidetodoc.com/presentation_image_h2/eef2d76042f0a3774fd23aa02bd57bad/image-14.jpg)
Directories in Python n tel = {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140} tel['Kubove'] = 6135 tel {'Janko': 6139, 'Kubove': 6135, 'Lubo': 6139, 'Marian': 6140} tel['Marian'] 6140 del tel['Kubove'] tel['Palove'] = 3333 tel {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140, 'Palove': 3333}

Directories in Python n tel {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140, 'Palove': 3333} tel. keys() ['Lubo', 'Palove', 'Janko', 'Marian'] 'Janko' in tel True for meno, cislo in tel. iteritems(): print meno, cislo Lubo 6139 Palove 3333 Janko 6139 Marian 6140

Graphics in Python n from Tkinter import * master = Tk() c = Canvas(master, width=200, height=200) c. pack() c. create_rectangle(0, 0, 200, width=3, fill="white") c. create_oval(50, 150, width=0, fill="red") c. create_line(150, 200, 200) mainloop()

Sounds in Python n import winsound for i in range (0, 13): f=440*(2**(i/12. 0)) if i in [0, 2, 4, 5, 7, 9, 11, 12]: winsound. Beep(f, 100) winsound. Play. Sound("System. Asterisk", winsound. SND_ALIAS) winsound. Play. Sound("System. Exclamation", winsound. SND_ALIAS) winsound. Play. Sound("System. Exit", winsound. SND_ALIAS) winsound. Play. Sound("System. Hand", winsound. SND_ALIAS) winsound. Play. Sound("System. Question", winsound. SND_ALIAS)

Guess number game in Python n import random p=1 x = random. randint(1, 20) h = int(raw_input()) while h!=x: p=p+1 if h < x: print('Your guess is too low. ') if h > x: print('Your guess is too high. ') h = int(raw_input()) print('Congratulation! You have had ', str(p), ' guesses!')

Guess number game in Pascal n program guess; var x, h, p: integer; begin x: = 1 + random(16); p: = 1; readln(h); while (h <> x) do begin p: = p + 1; if h > x then writeln(’less’); if h < x then writeln(’more’); readln(h); end; writeln(’Congratulation! You have had’, p, ’atempts’); end.

Guess number game in LOGO n to GUESS let "x 1 + random 16 let "p 1 let "h readword while [: h <> : x] [let "p : p + 1 if : h > : x [show "less] if : h < : x [show "more] let "h readword] (show "|Congratulation! You have had| : p "atempts) end

Acknowledgement n These results have achieved in the frame of projects n APVV LPP-0131 -06 Increasing Knowledge Potential / Zvyšovanie vedomostného potenciálu (doc. G. Andrejková) n APVV LPP-0057 -09 Development of gifted pupils by means of correspondency seminars and competetions / Rozvíjanie talentu prostredníctvom korešpondenčných seminárov a súťaží (Dr. Ľ. Šnajder)

Contact RNDr. Ľubomír ŠNAJDER, Ph. D. lubomir. snajder@upjs. sk Pavol Jozef Šafárik University in Košice Faculty of Science Institute of Computer Science Section of Didactics Informatics and Assistive Technologies Jesenná 5, 041 54 Košice, Slovak Republic GPS: 48° 43' 44. 78" N, 21° 14' 50. 83" E Phone: +421 55 234 6139 (my office), +421 55 234 6120 (our secretary)
- Slides: 22