Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.

Similar presentations


Presentation on theme: "Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class."— Presentation transcript:

1 Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class Loops for i in range (4): print “Hello” print “Hehe!” Boolean: True/False, Logical op.: not, and, or Conditionals if a: do this else: do that Many choices if a: do this elif b: do that else: do those Module imports from cs1robts import * Every Python data is an object with its type: int, float, str, complex, bool, tuple, class, etc. Empty variable: None (NoneType) Boolean expression: ==, !=, <, etc. Unpack tuples: x, y, x = position while a: hubo.move() >>> type (3) >>> type (Robot()) >>> type ((1, 2))

2 Functions take arguments and return a result: import math def to_radians(deg): #deg is a parameter and a local variable return (deg/180.0*math.pi >>> a = to_radians (90) # 90 is an argument Functions w/o the return keyword return None; it can return tuples for multiple data raw_input ()  takes keyboard input Local vs. global variables Global variables are defined outside functions Variables defined within functions are local “global” keyword enables us to update global variables within functions Importing modules import math print math.pi from math import * print pi from math import pi print pi Mutable vs. immutable objects string and tuples are immutable Functions are also objects print f  # f is a function Lists are mutable Gold = [2, 4] >>> Gold [-1] 4 >>> len(Gold) 2 >> range (2) [0, 1] # range func. returns a list Built-in functions on lists len, sum, max, min, sort, reverse, append, etc. Slicing: mylist [i:j] # return i, i+1,.., j-1 Lists, tuples, strings are a kind of sequence. Conversion is possible >>> lists (t) >>> tuple (Gold) >>> t = Gold >>> t is Gold True

3 Arguments are mapped to parameters one-by- one, left-to-right Default parameters: def creat_f (radius = 30, color = “yellow”): … >>> create (2), but not create (“silver”) Named parameters: create (color = “silver”) Formatting operator: % E.g., %.2f (2 digits after the period), %3d, %-3d, %g, %s String methods Islapha(), rstrip(), relpace (s1, s2), find (str), etc. File access f = open (..), f.readline (), f.write (), f.close () Loop control: break, continue Object creation: class Card(object): """A Blackjack card.""" def value(self): if type(self.face) == int: … # constructor def __init__(self, face, suit): … # string coversion def __str__(self): …. #equality def __eq__(self, rhs):.. card = Card(8, “Clubs”)

4 Interpreter vs. compiler Compiler is faster, but interpreter is more convenient for testing and debugging Designing smarter algorithms can be better than using faster computer or using compiler instead of interpreter Recursion: calls itself There should be a termination condition You can use programs to answer to questions!


Download ppt "Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class."

Similar presentations


Ads by Google