Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to programming in Python

Similar presentations


Presentation on theme: "Introduction to programming in Python"— Presentation transcript:

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

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

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

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

5 Python versions, installation
Python 2 vs. Python 3 some ideas from Python 3.0, 3.1 (3.2) were backported to Python 2.6 (2.7=last version) Python Portable 1.1 (based on Python 2.6.1) 5

6 Python portable version 1.1
PyScripter (interpreter, editor, code explorer …) 6

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

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

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

10 Pythons’ commands and data structures by simple examples
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' 10

11 Pythons’ commands and data structures by simple examples
Fibonacci numbers<1000 a, b = 0, 1 while b < 1000: print b, a, b = b, a + b Is primary number – function def isprime(n): b = True for x in range(2, n): if n % x == 0: b = False return b 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 11

12 Lists in Python 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) 12

13 Lists in Python menaM=[] for i in mena: if i[0]=="M": menaM.append(i) print menaM print sorted(mena) del mena[0] print mena nove_mena=["Peter","Lucia"] print mena+nove_mena mena.extend(nove_mena) print mena 13

14 Directories in Python tel = {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140} 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} 14

15 Directories in Python 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 15

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

17 Sounds in Python 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.PlaySound("SystemAsterisk", winsound.SND_ALIAS) winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS) winsound.PlaySound("SystemExit", winsound.SND_ALIAS) winsound.PlaySound("SystemHand", winsound.SND_ALIAS) winsound.PlaySound("SystemQuestion", winsound.SND_ALIAS) 17

18 Guess number game in Python
import random p = 1 x = random.randint(1, 20) h = int(raw_input()) while h!=x: p = p 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!')

19 Guess number game in Pascal
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. 19

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

21 Acknowledgement These results have achieved in the frame of projects
APVV LPP Increasing Knowledge Potential / Zvyšovanie vedomostného potenciálu (doc. G. Andrejková) APVV LPP 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)

22 Contact RNDr. Ľubomír ŠNAJDER, PhD. Pavol Jozef Šafárik University in Košice Faculty of Science Institute of Computer Science Section of Didactics Informatics and Assistive Technologies Jesenná 5, Košice, Slovak Republic GPS: 48° 43' 44.78" N, 21° 14' 50.83" E Phone: (my office), (our secretary)


Download ppt "Introduction to programming in Python"

Similar presentations


Ads by Google