Lists
[Pages:48]Lists
Chris Piech and Mehran Sahami CS106A, Stanford University
Piech + Sahami, CS106A, Stanford University
Housekeeping I
? Assignment #2 due today ? Assignment #3 goes out today (Due on Wed., Oct. 14)
? Can do Part 1 after today's class ? Practice with lists (which will be on diagnostic)
? Can study for diagnostic and get part of assignment done! ? Can do Part 2 after this coming Monday's class
Piech + Sahami, CS106A, Stanford University
Housekeeping II
? Diagnostic assessment on Wed., Oct. 7
? Takes place during class time ? Covers through today's material (i.e., lists are fair game) ? Please download BlueBook software before the exam ? There is a practice diagnostic (and instructions) on class website ? If you have OAE accommodations or are in time zone (outside the
Americas) that requires rescheduling, and haven't heard from Juliette, please email her
Piech + Sahami, CS106A, Stanford University
Global Variables: Bad Style
# Constant ? visible to all functions NUM_DAYS_IN_WEEK = 7
# Global variable ? visible to all functions balance = 0
Different variables with the same name!
def main():
Super confusing!
balance = int(input("Initial balance: "))
while True:
amount = int(input("Deposit (0 to quit): "))
if amount == 0:
break deposit(amount)
? Also, really BAD style
? So bad, that Python won't even let you do
it unless you basically add a command
def deposit(amount): balance += amount
that says "I want to have bad style"
? I'm not going to show you that command in Python
? But, if you know it already, DON'T use it!
? We're in polite company
Using Parameters: Good Style
Don't want using your toaster to impact your refrigerator!
def main(): balance = int(input("Initial balance: ")) while True: amount = int(input("Deposit (0 to quit): ")) if amount == 0: break balance = deposit(balance, amount)
def deposit(balance, amount): balance += amount return balance
Encapsulation Principle: Data used by a function should be a parameter or encapsulated in function
The Python Console
? Can run Python interactively using the "console"
? In PyCharm click "Python Console" tab at bottom of window ? In Terminal, run Python (e.g., typing "py" or "python3" or
"python", depending on your platform) to get console
? Console has prompt: >>>
? Can type and execute Python statements (and see results) ? Example:
>>> x = 5 >>> x 5
? Easy way to try things out to answer questions you may have ? Use exit() to leave console
Piech + Sahami, CS106A, Stanford University
Let's Take the Console Out For a Spin...
Piech + Sahami, CS106A, Stanford University
And Then There Were None
? The term None is used in Python to describe "no value"
? For example, it is the value you would get from a function that doesn't return anything
? WHAT?! ? Example:
>>> x = print("hi") >>> print(x) None
? Comparing anything to None (except None) is False
? Why does None exist?
? Denotes when the suitcase for a variable has "nothing" in it
Piech + Sahami, CS106A, Stanford University
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- drug lists a thru z
- lists of synonyms words list
- word lists synonyms antonyms
- vocabulary lists for college students
- lists of synonyms for kids
- lists to write when bored
- third grade book lists printable
- bible hangman word lists ideas
- second grade spelling lists printable
- reading lists for 3rd grade
- reading lists for elementary students
- printable word lists for kids