1 / 12

computer science

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 .

ronda
Download Presentation

computer science

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. CSCI 1001 overview of computer science PYTHON II

  2. Need to Know Data types Expressions Variables Functions Input and Output Libraries Control structures

  3. 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)

  4. lists: [‘a’, ‘b’, ‘c’], [] tuples: (‘a’, ‘b’, ‘c’) dictionaries: d = {} d[‘key’] = ‘value’

  5. 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

  6. 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

  7. IF/ELIF/ELSE if x < 18: print ‘kid’ elifx < 21: print ‘not a kid’ elifx < 34: print ‘grown up’ else: print ‘really old’

  8. WHILE i = 0 while i < 10: print i i = i+1 print ‘finally, i=’, i

  9. FOR for i in range(0,10): print i # down here, what’s i?

  10. 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

  11. 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

  12. http://cs1001.us/ Please read Sections 3-5 of the Python chapter for Friday

More Related