Control Statements
Loops
Classes and Objects
More Classes and Objects
Hands-On
100
What is printed to the screen? fahrenheit = 50 if fahrenheit > 90: print(“Heat warning!”) if fahrenheit < 30: print(“Cold warning!”)
Nothing!
100
This loop executes how many times? for i in range(10): print(i)
10
100
Briefly define the difference between procedural and object oriented programming.
Object oriented programming uses classes and objects to model real-world entities. Programs are written as a set of interacting objects. Procedural programs execute sequentially from top to bottom.
100
What is the Python symbol used to call (execute) a method of an object? a.) The dot operator (period) b.) The hash tag operator (#) c.) The method operator (! on the keyboard) d.) The dash operator ( - on the keyboard )
The dot operator (period on the keyboard)
100
Create the following list fruits = ["apple","banana","coconut"] and write one Python statement to print coconut using the fruit variable.
print( fruit[2] )
200
Which lines will be executed if the if statement is true? if (a == 5): print("This one?") print("How about this one?") print("Maybe this one?")
The first two print statements
200
What gets printed the first time this loop executes? for i in range(10): print(i)
0
200
True or False Python only supports procedural programming.
False
200
Given the following Python statement from cst171 import AssignmentThree What is the name of the class we are importing and what is the name of the file in which it is stored?
The from statement has the structure from filename import ClassName using this information we can determine that we are importing a class named AssignmentThree from a file named cst171.py
200
Write a for loop that prints "Hello World" to the screen 20 times
for i in range(20): print("Hello World")
300
What is the difference between = and == in Python?
= is the assignment operator. It is used to assign a value to a variable. == is used to test for equality. a = 10 # assigns 10 to the variable named a if ( a==10 ): # tests if a currently contains a value of 10
300
What is the difference between a while loop and a for loop? Under what circumstances might you use one over the other?
for loops are called definite loops. They are used when we define in advance how many times we need to iterate. A while loop is an indefinite loop. It loops until a condition is true. There is no way to tell beforehand when this will occur.
300
In object oriented programming ________ are used to represent an object's attributes (data values) and _______ are used to represent an object's methods (operations the object can perform).
variables and functions
300
Class methods always contain "self". What does self represent and why is it needed? def withdraw(self, amount): self.balance = self.balance - amount
"self" tells the object to update itself and not some other object. This is how we enforce encapsulation. In the above example the self in the definition tells the object to perform the operation on itself. The self in the self.balance tell the object to update its balance and not another object's balance.
300
Write a for loop that prints the numbers from 0 to 20 to the screen
for i in range(20): print(i)
400
Briefly describe how if ( a == 10 ): # statements else: # statements is different than if ( a == 10 ): # statements if ( b == 10 ): # statements
With an if-else combination only one block of statements will get executed. With multiple if statements it is possible, but not required, that all blocks of statements will be executed
400
What is the output of this Python code? a = 5 b = 10 while( a == 5 and b <12): print("Hello World") b = b + 1
"Hello World" will be printed twice. After two iterations b will have a value of 12 and the second while condition will no longer be true.
400
Given the following Python code, what is the name of the class and how many objects are created from it? ba1 = BankAccount() ba2 = BankAccount()
The class name is BankAccount and two objects were created: ba1 and ba2
400
Given the following class initialization method, give an example of how you would create a MyClass object from it class MyClass: def __init__ (self, firstName, lastName)
obj = MyClass("First","Last")
400
Create a class named MyClass with a method named test that accepts two numbers as inputs, adds them together, and returns the results (ignore the __init__ method for now)
class MyClass: def test (self, num1, num2): result = num1 + num2 return result
500
True or False: When using if and elif a final else statement is required? if value < 0: print("\n You entered a negative number.") elif value == 0: print("\n You entered zero.") else: print("\n You entered a positive number.")
False
500
What is the output of this Python code? a = 5 b = 10 while( a == 5 and b <12): print("Hello World") b = b - 1
This is what is called an infinite loop. It will print "Hello World" forever!
500
Encapsulation is an important property of objects. What is it?
All the data and operations of a real-world entity are self-contained. For example, with our BankAccount class, each object contained a name, a balance, and operations to deposit and withdraw money. This was part of the object and independent of other objects - we could update the balance of one bank account object without impacting any other bank account objects.
500
Given the following Python statement obj.updateSomething() What two things can you conclude about obj?
obj is an object and it has a method named updateSomething
500
You are told that SomeClass has a method named "sample" that takes no inputs. Given obj = SomeClass() what would be the Python statement that executes the sample method on the object we created?
obj.sample()