Lecture 03 - CS61A fa2022

Side Effect (and Return Values)

  1. Side effect: The action besides returning value is a side effect of a function.
  2. Fucntions can deal with the params either way. e.g. print() will have a side effect of printing things on the monitor and return "nothing", which actually is None.
    • None is a type of NoneType, which is unsupported for operand. e.g. add(None, None) and None + 1 both returns TypeError
    • None will be returned if no value has been explicitly returned.
    • Is this better?

      Revised Version by Chatgpt

      Your sentence is already clear and grammatically correct. However, you can make it slightly more concise and idiomatic: "None will be returned if no value is explicitly provided." This version conveys the same meaning in a more streamlined manner.

    • print(None) and print(print(1), print(2)) both return None, while the latter one also has side effects to print 1 and 2

More Function Features

  1. Default value: Operands of Python can have defined default value, just like C++
  2. Multiple return values: Python supports return val1, val2, which is not supported in C++
  3. Doctests: A powerful tools for case test. See more here

Boolean Expression

  1. Google Maps uses a boolean to decide whether to avoid highways in driving directions:
    avoid_highways = True
    Twitter uses a boolean to remember where the user allows personalized ads:
    personalized_ads = False

    • How do they know? Twitter is not be a open source software, is it?
    • Stupid Question! Answer: Docs! e.g. Twitter params - dnt
  2. Comparison, logical and their compound operation.
  3. Use == for if_equal!
    • Why?
    • Inspired by CS50P, compiler cannot tell the difference between assign and if_equal.
  4. Trick: return grade > 65 without if and else

Statement

  1. Revice that, for structure of a function:
    def <name>(<parameters>):        # ← Function signature
        return <return expression>   # ← Function body
    
  2. For compound statements, which is a statement that contains other statements:
    <header>:                        # CLAUSE
        <statement>                  # SUITE
        <statement>                  # a sequence of statements
    
  3. Conditional statement:
    1. if, else: no ( and ).
    2. elif supported.
  4. Loops:
    1. while, no ( and ).
    2. break and continue
      • Why continue is not mentioned in ppt?
    3. for is somehow complicated:
      • Syntax: for <index_val> in <array>:. Beware to append :.
      • Usage:
        1. for x in ["apple", "banana", "cherry"]:
        2. for x in fruits: # fruits=["apple", "banana", "cherry"]
        3. for x in range(<from>, <to>, <increasment>):
        4. for x in "string" # x will be 's' 't' 'r' 'i' 'n' and 'g'
      • Why for is not mentioned?
  5. pass: placeholder statement, avoiding errors for somewhere not implemented. Note:
    1. ; in C is of the same use
    2. idle in OS is somehow similar
    3. ;; in SHELL is not related, which is used only after the end of each case suite

Terms

en zh_CN Supplement Example

Vocabs and Phrases

en Explanation Example