Introduction to Python II
Review of Basic Programming Concepts
- Syntax
- Data types
- Operators
- Variables
- Lists
Review Exercises
Do these exercises in the interpreter
- If you have 3 boxes containing 25 chocolates each, and 10 bags containing 32 sweets each, how many sweets and chocolates do you have in total? (Hint: You can use one equation)
- Create variables for your first and last name. Now create a string and use placeholders (%s) to add your name.
- Make a list of 3 of your favorite drinks and name it drinks. Make a list of 3 of your favorite foods and name it foods. Join these two lists and name the result favorites. Finally print the variable favorites
If-Statements
- 'if' is followed by a 'condition' and a colon (:), and if the condition is met, the following block of code executes
- ex if age > 17:
print('you are too old!')
- Conditional operators
- ==, !=, >, <, >=, <=
- What is the difference between = and ==
What Else is There?
- If-statements can be extended with an if-then-else-statement
- So, if the condition isn't met, it would execute a different block of code (if something is true, then do this, otherwise do that); ex:
>>> age = 18
>>> if age == 12:
. . . print('Hello')
. . . else:
. . . print('Goodbye')
Else-if-statements
- If-statements can be further extended using elif (else-if)
- If the previous if-statement's condition isn't met, then it check the else-if's condition, and so forth.
elif Example
>>> age = 12
>>> if age == 11:
. . . print("You are 11")
. . . elif age == 12:
. . . print("You are 12")
. . . elif age < 13:
. . . print("You are younger than 13")
. . . else:
. . . print("You are older than 13")
What will be printed?
Combining Conditions
- You can have multiple conditions in any conditional statement, by using and and or
>>> if age == 10 or age == 11 or age == 12:
. . . print("You are %s" % age)
- If any of the conditions are met, then the code runs, hence the or
>>> if age >= 10 and age <= 12:
. . . print("You are %s" % age)
- In this case, both of the conditions have to be true for the code to run, hence the and
None
- Another value that can be assigned to a variable is None
- This is an empty value, with nothing in it (also known as null or NIL in other languages)
- None can be used to reset variables and make them unused
- An example is if we don't want to print a message when a variable is empty
- >>> money = None
- >>> if money == None:
- . . . print("You have no money!")
Loops
- Repeatedly executing code written in the loop until/while a condition is met
- for loop & while loop
- How can this be useful?
For Loops
- For loops iterate a limited number of times, with a variable taking on the values in the given list
-
for variable in list:
- The variable iterates through the list and is assigned the value of the current iteration
-
range(x, y) function returns [x, x+1, x+2, ..., y-1]
-
ex range(5, 9) == [5, 6, 7, 8]
- range(4) == [0, 1, 2, 3]
-
ex range(5, 9) == [5, 6, 7, 8]
-
Like if statements, the for loop is defined and then followed by a block of code
- >>> for x in range(5): >>> for s in shoppingList:
- . . . y = x + 1 . . . print(s)
- . . . print(x+y)
- Indentations matter!
Code Blocks & Nested Loops
- A block of code is a group of statements
- White space (tab or space) separate blocks of code, like in the loop example above.
- Consistent spacing!
- The 3 dots (. . . instead of >>>) in the interpreter means you're typing in a block of code
- Blocks placed inside each other require the extra spacing as shown to the right
- Loops placed inside each other are nested loops, and each block's loops fully complete before returning to the outer loop
this is block 1
this is block 1
this is block 2
this is block 2
this is block 2
this is block 3
this is block 3
this is block 2
this is block 1
this is block 1
Example of Nested Loops
- Try typing this into your interpreter:
- >>> myList = [ 'a', 'b', 'c' ]
- >>> for u in myList:
- . . . print(u)
- . . . for v in myList:
- . . . print(v)
- Remember how you'd earn $1820 a year ($35 a week) through your chores and paper route? We can calculate the money earned throughout the weeks!
- >>> savings = 0
- >>> for week in range(1, 53):
- . . . savings = savings + 35
- . . . print("Week %s = $%s" % (week, savings))
More for loops
- Physical exercise
While we're on Loops
-
While loops iterate through the block of code while a condition is met
- Different from for loops, because it's while, not until
- while condition:
- >>> x = 1
- >>> while x < 200
- . . . x = 2*x
- . . . print(x)
Break
- You can break out of a loop before the condition is met
- >>> while True:
- . . . # tons of code
- . . . if someCondition == True:
- . . . break
- Games use while True to keep the game running and refreshing the screen!
Input()
- You can take in user-inputted text in the code, and store it in a variable
- input("text")
- The user types something in and presses Enter
- Whatever they typed in is returned by input() as a string
- >>> name = input("Please enter your name: ")
- >>> print("Hello, %s!" % name)
- If they input a number, it is still returned as a string
- use the int() function to change it to an int
- >>> age = input("What is your age? ")
- >>> age = int(age)
Hack My Planet Introduction to Python II
By jtheadland
Hack My Planet Introduction to Python II
- 664