1 / 22

Introduction to programming in Python

Introduction to programming in Python. Ľubomír ŠNAJDER. Content. Basic characteristics of Python Python versions, installation , portable version 1.1 Python as a n interactive calculator

issacn
Download Presentation

Introduction to programming in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 http://www.python.org/, 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) http://portablepython.com/wiki/PortablePython1.1Py2.6.1

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

  7. Numbers (integer, float, variables): 20*3.9520797 2**24 2/3 -2/3 2/3.0 c=100f=32+9/5.0*cfround(_,2) a, b = 1, 2c = aa = bb = c a, b = b, a Numbers (complex, variables): 1j*1j c=3+4jc.realc.imagabs(c) Python as an interactive calculator

  8. Strings: "doesn't" 'doesn\'t' chess = ““" ... .X.X... X.X.... .X.X... X.X.““"chessprint 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' Python as an interactive calculator

  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 SQRTfor i in range(10): print i, math.sqrt(i) Star triangelfor riadok in range(10): for stlpec in range(1,riadok): print "*", print

  10. Pythons’ commands and data structures by simple examples School marks in Slovakiax = 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'

  11. Fibonacci numbers<1000a, b = 0, 1while b < 1000: print b, a, b = b, a + b Is primary number – functiondef 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 – functiondef gcd(a,b): while a != b: if a < b: b = b - a else: a = a - b return a Pythons’ commands and data structures by simple examples

  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 menaprint ", ".join(mena)

  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 menanove_mena=["Peter","Lucia"]print mena+nove_menamena.extend(nove_mena) print mena

  14. Directories in Python tel = {'Janko': 6139, 'Lubo': 6139, 'Marian': 6140}tel{'Janko': 6139, 'Lubo': 6139, 'Marian': 6140}tel['Kubove'] = 6135tel{'Janko': 6139, 'Kubove': 6135, 'Lubo': 6139, 'Marian': 6140}tel['Marian']6140del tel['Kubove']tel['Palove'] = 3333tel{'Janko': 6139, 'Lubo': 6139, 'Marian': 6140, 'Palove': 3333}

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

  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()

  17. Sounds in Python import winsoundfor 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)

  18. Guess number game in Python • import randomp = 1x = 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!')

  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.

  20. Guess number game in LOGO to GUESSlet "x 1 + random 16 let "p 1let "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

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

  22. Contact RNDr. Ľubomír ŠNAJDER, PhD.lubomir.snajder@upjs.skPavol Jozef Šafárik University in KošiceFaculty of ScienceInstitute of Computer ScienceSection 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)

More Related