05. Python Practice
Python Practice
Question:
Please check off the following topics to confirm that you already understand them! You're only expected to understand the basics of how to manipulate them—details about usage will be explained later if needed. If anything looks unfamiliar, you can check out one of our introductory Python classes . This course will also default to Python 2 where applicable, though it's very similar to Python 3 !
Comments
# This is a one-line Python comment - code blocks are so useful!
"""This type of comment is used to document the purpose of functions and classes."""
Declaration/Initialization
# Remember values, not variables, have data types.
# A variable can be reassigned to contain a different data type.
answer = 42
answer = "The answer is 42."
Data Types
boolean = True
number = 1.1
string = "Strings can be declared with single or double quotes."
list = ["Lists can have", 1, 2, 3, 4, "or more types together!"]
tuple = ("Tuples", "can have", "more than", 2, "elements!")
dictionary = {'one': 1, 'two': 2, 'three': 3}
variable_with_zero_data = None
Simple Logging
print "Printed!"
Conditionals
if cake == "delicious":
return "Yes please!"
elif cake == "okay":
return "I'll have a small piece."
else:
return "No, thank you."
Loops
for item in list:
print item
while (total < max_val):
total += values[i]
i += 2
Functions
def divide(dividend, divisor):
quotient = dividend / divisor
remainder = dividend % divisor
return quotient, remainder
def calculate_stuff(x, y):
(q, r) = divide(x,y)
print q, r
Classes
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def birthday(self):
self.age += 1
You shouldn't need to run Python code outside the classroom, so don't worry if you don't have a development environment set up!
Start Quiz:
"""You can use this class to represent how classy someone
or something is.
"Classy" is interchangable with "fancy".
If you add fancy-looking items, you will increase
your "classiness".
Create a function in "Classy" that takes a string as
input and adds it to the "items" list.
Another method should calculate the "classiness"
value based on the items.
The following items have classiness points associated
with them:
"tophat" = 2
"bowtie" = 4
"monocle" = 5
Everything else has 0 points.
Use the test cases below to guide you!"""
class Classy(object):
def __init__(self):
self.items = []
# Test cases
me = Classy()
# Should be 0
print me.getClassiness()
me.addItem("tophat")
# Should be 2
print me.getClassiness()
me.addItem("bowtie")
me.addItem("jacket")
me.addItem("monocle")
# Should be 11
print me.getClassiness()
me.addItem("bowtie")
# Should be 15
print me.getClassiness()
Solution:
Here's the solution:
class Classy(object):
def __init__(self):
self.items = []
def addItem(self, item):
self.items.append(item)
def getClassiness(self):
classiness = 0
if len(self.items) > 0:
for item in self.items:
if item == "tophat":
classiness += 2
elif item == "bowtie":
classiness += 4
elif item == "monocle":
classiness += 5
return classiness
If you thought that quiz was really hard, you likely won't be able to understand the coding examples ahead. The videos are all language agnostic, meaning you don't need to know about a particular coding language to understand them, so you should still be able to watch the videos!