120 likes | 210 Views
CSCI 1001. overview of. computer science. PYTHON II. Need to Know. Data types. Expressions. Variables. Functions. Input and Output. Libraries. Control structures. functions :. def area(r ): return 3.14 * r * r. def example(n ): for i in range(1,n+1): print i return .
E N D
CSCI 1001 overview of computer science PYTHON II
Need to Know Data types Expressions Variables Functions Input and Output Libraries Control structures
functions: def area(r): return 3.14 * r * r def example(n): for i in range(1,n+1): print i return example(10) • example(1)
lists: [‘a’, ‘b’, ‘c’], [] tuples: (‘a’, ‘b’, ‘c’) dictionaries: d = {} d[‘key’] = ‘value’
libraries: import math print math.pi, math.log(2) import time print time.time(), time.ctime() import random print random.random() a = range(0,10) random.shuffle(a) http://docs.python.org/library/index.html
Comments def max(a1, a2): # find the max of a1,a2 if a1 > a2: return a1 else: return a2 def silly(s): return s + “# still here!” Comments help people to read code
IF/ELIF/ELSE if x < 18: print ‘kid’ elifx < 21: print ‘not a kid’ elifx < 34: print ‘grown up’ else: print ‘really old’
WHILE i = 0 while i < 10: print i i = i+1 print ‘finally, i=’, i
FOR for i in range(0,10): print i # down here, what’s i?
finding maximum input: integers a1,a2,…,an output: max 1. set max = a1 2. set i = 1 3. while i ≤ n do 4. if ai > max then 5. set max = ai 6. set i = i+1 7. output max and stop
in python… def find_max(a) n = len(a) i = 0 max = a[0] while i < n: if a[i] > max: max = a[i] i += 1 return max
http://cs1001.us/ Please read Sections 3-5 of the Python chapter for Friday