4.4 KiB
Intro to Python pt. 2
Lesson Objectives
- Get user input
- Repeatedly perform a set of commands
- Use a for loop
- Define a function
- Create a class for an object
- Have a class inherit from another
- Create a factory for objects
Get user input
You can get user input from the command like so:
user_input = input("Please enter your name: ")
print("Hello, " + user_input + "!")
Repeatedly perform a set of commands
a = 10
while a < 20:
print("the value of a is currently: " + str(a))
a += 1
ACTIVITY
Use a for loop
The process of looping through an array can be simplified with a for loop:
foods = ['hot dogs', 'beer', 'bald eagles']
for food in foods:
print(food)
You can loop through a set of numbers using a range
for x in range(0, 3):
print(x)
ACTIVITIES
-
Given the following list [70, 95, 97, 55, 3, 24, 89, 97, 84, 11]
- Write a program that loops through each value in the list and prints it
- Write a program that loops through each value in the list and adds them all together
- Write a program that loops through each value in the list and prints the average
- Write a program that loops through each value in the list and prints the minimum
- Write a program that loops through each value in the list and prints the maximum
-
Combine all the programs from the previous step into one program that asks the user what operation they would like to do
-
Alter the last program so that it performs the operations for only numbers that are greater than a number specified by the user
Define a function
If you have a routine that you run over and over again, you can define your own function:
def greet():
print('hi')
greet()
Functions can take parameters which alter their functionality:
def greet(name):
print('hi, ' + name)
greet('bob')
Functions can return values:
def add(value1, value2):
return value1 + value2
print(add(1,3))
ACTIVITIES
Create a calculator program that continually asks a user what operations they want to perform, until the user says 'quit'
Create a class for an object
You can use a class or blueprint for objects that you'll use
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name + ". My age is " + str(self.age))
me = Person("Hunter", 29)
me.greet()
sally = Person("Sally", 53)
sally.greet()
__init__is a function that gets called when a new object is created. Make sure you use two underscores on either side of theinitor it will break!selfis the object that's created
Have a class inherit from another
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name + ". My age is " + str(self.age))
def work(self):
print("Boring...")
class SuperHero(Person): # tell it to inherit from Person
def __init__(self, name, age, powers):
super().__init__(name,age) # call Person's __init__()
self.powers = powers
def greet(self):
super().greet() # call Person's greet()
self.listPowers()
def listPowers(self):
for power in self.powers:
print(power)
def work(self): # override Person's work()
print("To action!")
superman = SuperHero('Clark Kent', 200, ['flight', 'strength', 'invulnerability'])
superman.greet()
superman.work()
Create a factory for objects
class Car:
def __init__(self, maker, model, serial):
self.maker = maker
self.model = model
self.serial = serial
class CarFactory:
def __init__(self, name):
self.name = name
self.cars = []
def makeCar(self, model):
self.cars.append(Car(self.name, model, len(self.cars)))
def listCars(self):
for car in self.cars:
print(car.maker + " " + car.model + " serial#: " + str(car.serial))
def findCar(self, serial):
for car in self.cars:
if(car.serial == serial):
return car
toyota = CarFactory('Toyota')
toyota.makeCar('Prius')
toyota.makeCar('Rav 4')
toyota.listCars()
print(toyota.findCar(1).model)
