1 / 31

Intro to Python

Intro to Python. Python is an interpreted language Can be used interactively Identifiers are case-sensitive Operators : + - * / * * Arbitrarily large integer. Python Reserved Words. and assert break class continue def del elif else except exec finally for from global

traci
Download Presentation

Intro to 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. Intro to Python

  2. Python is an interpreted language • Can be used interactively • Identifiers are case-sensitive • Operators: + - * / ** • Arbitrarily large integer

  3. Python Reserved Words and assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while yield

  4. Output Statements • print • print <expr> • print <expr>, <expr>, ..., <expr> • print <expr>, <expr>, ..., <expr>,

  5. print 3+4 print 3, 4, 3 + 4 print print 3, 4, print “The answer is”, 3+4

  6. Assignment Statements <var> = <expr> <var> = input(<prompt>) <var> = raw_input(<prompt>) <var>, <var>, ... <var> = <expr>, <expr>, ..., <expr>

  7. x = 5y = 3.9 * x * (1 - x)age = input(“Yourage?”)name = raw_input(“Yourname?”) x, y = y, x

  8. Decisions if <condition>: <body> elif<condition>: <body> else: <body> < <= == >= > !=

  9. if x > 5: print “greater than five” elifx < 5: print “less than five” else: print “five”

  10. Definite Loops for <var> in <list>: <body>

  11. Sequences/Lists • [0, 1, 2, 3] • [1, 3, 5, 7, 9] • range(10) • range(<expr>)

  12. for i in [0, 1, 2, 3]: printi for i in range(11): printi

  13. Indefinite Loops while <condition>: <body>

  14. i=0while i <= 10: print i i = i + 1

  15. Strings greet = “Hello Bob” str2 = ‘spam’ print greet, str2 greet[0] greet[-1] greet[-3] greet[5:9] greet[:5] greet[5:]

  16. More with Strings greet = “Hello Bob” str2 = ‘spam’ greet + str2 3 * str2 (3 * str2) + (greet * 2) len(greet) for ch in greet: print ch,

  17. String and List Operations • + Concatenation • * Repetition • <string>[] Indexing • <string>[:] Slicing • len(<string>) Length • for <var> in <string> Iteration

  18. [1, 2] + [3, 4] [1, 2] * 3 grades= [‘A’,’B’,’C’,’D’,’F’] grades[0] grades[2:4] len(grades)

  19. More with Lists • <list>.append(x) • <list>.sort() • <list>.reverse() • <list>.index(x) • <list>.insert(i, x) • <list>.count(x) • <list>.remove(x) • <list>.pop(i)

  20. Other String Operations import string string.capitalize(<string>) string.capwords(<string>) string.lower(<string>) string.upper(<string>) string.replace(<string>, <string>, <string>) string.center(<string>, <int>) string.count(<string>, <string>) string.find(<string>, <string>) string.join(<list>) string.split(<string>)

  21. Math Library pie sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) log(x) log10(x) exp(x) ceil(x) floor(x)

  22. Functions def <function>(<params>): <body>

  23. def main(): celsius= input("What is the Celsius temperature? ") fahrenheit= (9.0 / 5.0) * celsius + 32 print "The temperature is ", fahrenheit, " degrees Fahrenheit." main()

  24. def happy(): print “Happy birthday to you!” defsing(person): happy() happy() print “Happy birthday, dear”, person + “.” happy() defmain(): sing(“Fred”) print sing(“Lucy”) print sing(“Elmer”) main()

  25. import math def main(): print “This program finds the real solutions to a quadratic” print a, b, c = input(“Please enter the coefficients (a, b, c): “) discRoot= math.sqrt(b * b - 4 * a * c) root1 = (-b + discRoot) / (2 * a) root2 = (-b - discRoot) / (2 * a) print print “The solutions are:”, root1, root2 main()

  26. defmain(): # months is used as a lookup table months = “JanFebMarAprMayJunJulAugSepOctNovDec” n = input(“Enter a month number (1-12): “) # compute starting position of month n in months pos= (n-1) * 3 # grab the appropriate slice from the month monthAbbrev= months[pos:pos+3] # print the result print “The month abbreviation is”, monthAbbrev + “.” main()

  27. def main(): # months is a list used as a lookup table months = [“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”] n = input(“Enter a month number (1-12): “) # print the result print “The month abbreviation is”, month[n-1] + “.” main()

  28. File Basics <filevar> = open(<name>, <mode>) <filevar>.read() <filevar>.readline() <filevar>.readlines() <filevar>.write(<string>)

  29. import string defmain(): fname= raw_input(“Enter a filename: “) infile= open(fname, ‘r’) data = infile.read() DATA = string.upper(data); outfile= open(fname + “_CAP”, ‘w’) outfile.write(DATA) main()

  30. Dictionaries <dictionary>[<key>] passwd= {} for line in open(‘passwords’, ‘r’): user, pass = string.split(line) passwd[user] = pass

  31. Dictionaries <dict>.has_key(<key>) <key> in <dict> <dict>.keys() <dict>.values() <dict>.items() <dict>.get(<key>, <default>) del <dict>[<key>] <dict>.clear()

More Related