Download presentation
Presentation is loading. Please wait.
Published byTabitha Hamon Modified over 9 years ago
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 (,,..., )
5
print (3+4) print (3, 4, 3 + 4) print () print (“The answer is”, 3+4)
6
Assignment Statements = = input( ) = raw_input( ),,... =,,...,
7
x = 5 y = 3.9 * x * (1 - x) age = input(“Your age?”) name = raw_input(“Your name?”) x, y = y, x
8
Decisions if : elif : else: = > !=
9
if x > 5: print “greater than five” elif x < 5: print “less than five” else: print “five”
10
Definite Loops for in :
11
Sequences/Lists [0, 1, 2, 3] [1, 3, 5, 7, 9] range(10) range( )
12
for i in [0, 1, 2, 3]: print i for i in range(11): print i
13
Indefinite Loops while :
14
i=0 while 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 [] Indexing [:] Slicing len( ) Length for in 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.append(x).sort().reverse().index(x).insert(i, x).count(x).remove(x).pop(i)
20
Other String Operations import string string.capitalize( ) string.capwords( ) string.lower( ) string.upper( ) string.replace(,, ) string.center(, ) string.count(, ) string.find(, ) string.join( ) string.split( )
21
Math Library pi e 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 ( ):
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!” def sing(person): happy() print “Happy birthday, dear”, person + “.” happy() def main(): 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
def main(): # 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 = open(, ).read().readline().readlines().write( )
29
import string def main(): 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 [ ] passwd = {} for line in open(‘passwords’, ‘r’): user, pass = string.split(line) passwd[user] = pass
31
Dictionaries.has_key( ) in.keys().values().items().get(, ) del [ ].clear()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.