【coursera 学习笔记】An Introduction to Interactive Programming in Python--week1

第一周 教学大纲

Week oneHelp

Functions — Functions  函数
  • Functions are reusable pieces of programs that take an input and produce an output.(定义)
  • A function definition is a compound statement consisting of a header and a body.(组成)
  • The header includes the keyword def, a sequence of parameters enclosed by parentheses, followed by a colon :.
  • def  function_name():
               body
               return
    # computes the area of a triangle
    def triangle_area(base, height):     # header - ends in colon
        area = (1.0 / 2) * base * height # body - all of body is indented
        return area                      # body - return outputs value
    


  • The body consists of a sequence of statements, all indented by 4 spaces.(缩进一般为4,注意同一逻辑层的缩进一致)
  • Functions may return a value using the keyword return or have a side effect (e.g., print).
  • To evaluate a function call, replace the function's parameters in the body of the function by their associated values in the call and execute the body of the function.
  • Lecture examples - Functions
  • More examples - Stucture of Functions, Uses of Functions, Scope of Variables, Examples of Functions
Indentation — Functions(缩进)
  • Indentation consists of whitespace formed by blanks, tabs, and newlines.
  • Leading white space indicates indentation level (4 spaces per level)and specifies logical grouping of statements in Python.
  • Incorrect indentation can lead to errors.
  • Lecture examples - Functions
  • More examples - Function Errors
Remainders and modular arithmetic — More Operations
  • Standard long division yields a quotient and a remainder. The integer division operator // computes the quotient. The operator % computes the remainder.
  • For any integers a and ba == b * (a // b) + (a % b).
  • In Python, a % b always returns an answer that is between 0 and b (even if a and/or b is negative).
  • Remainders and modular arithmetic are very useful in games for the purpose of "wrapping" the canvas, i.e; causing objects that pass off of one side of the canvas to reappear on the opposite side of the canvas.
  • Lecture examples - More operations
  • More examples - Modulus, Math Module,
Syntax:
a / b
a // b
Examples:
Code Output
print 12 / 3
4
print 11 / 3
3
print 11 // 3
3
print 12.0 / 3.0
4.0
print 11.0 / 3.0
3.6666666666666665
print 11.0 // 3.0
3.0

/ 为标准除法:若a、b均为integer,则结果为对真值的向下取整(floor)。否则就是float
若a、b均为integer,则结果为对真值的向下取整(floor)。否则 在取整的前提下加上小数位.0
# problem - get the ones digit of a number
num = 49
tens = num // 10
ones = num % 10


Modules — More Operations
  • Modules are libraries of Python code that implement useful operations not included in basic Python.
  • Modules can be accessed via the import statement.
  • CodeSkulptor implements parts of the standard Python modules math and random.
  • Lecture examples - More operations
  • More examples - Math Module, Numbers and Strings, Random Module, Module Errors
  • # Python modules - extra functions implemented outside basic Python
    
    import simplegui	# access to drawing operations for interactive applications
    
    import math	 		# access to standard math functions, e.g; trig
    
    import random   	# functions to generate random numbers
    
    
    # look in Docs for useful functions
    
    print math.pi


Boolean Expressions — Logic and Comparisons
  • The constants True and False of the type bool.
  • These constants can be combined to form Boolean expressions via the logical operators andor, and not.
  • The and of two Boolean expressions is True if both of the expressions are True.
  • The or of two Boolean expressions is True if at least one of the expressions is True.
  • Lecture examples - None
  • More examples - Booleans, Boolean Logic
Relational Operators — Logic and Comparisons
  • The values of two arithmetic expressions can be compared using the operators ==!=<><=>=.
  • These comparisons return either True or False .
  • Lecture examples - None
  • More examples - Comparison, Boolean Expressions
Conditional Statements — Conditionals
  • Conditional statements are compound statements consisting one or more clauses headed by the keywords ifelif, and else.
  • Each if or elif clause is followed by a Boolean expression and a colon :.
  • If the Boolean expression for a clause is True, the body of the clause is executed.
  • Lecture examples - Conditionals
  • More examples - if-elif-else, Examples of Conditionals
  • def num_solutions(discriminant):
        if discriminant > 0:
            return 2
        elif discriminant < 0:
            return 0
        else:
            return 1

    example:做界面
  • # CodeSkulptor runs Python programs in your browser.
    # Click the upper left button to run this simple demo.
    
    # CodeSkulptor runs in Chrome 18+, Firefox 11+, and Safari 6+.
    # Some features may work in other browsers, but do not expect
    # full functionality.  It does NOT run in Internet Explorer.
    
    import simplegui
    
    message = "Welcome!"
    
    # Handler for mouse click
    def click():
        global message
        message = "Good job!"
    
    # Handler to draw on canvas
    def draw(canvas):
        canvas.draw_text(message, [50,112], 48, "Red")
    
    # Create a frame and assign callbacks to event handlers
    frame = simplegui.create_frame("Home", 300, 200)
    frame.add_button("Click me", click)
    frame.set_draw_handler(draw)
    
    # Start the frame animation
    frame.start()


你可能感兴趣的:(Python/Go)