








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Name: ______________ Grade: ______ Let's code!
(^1) Hello, Python!
print("Hello, Emma!") print("I am learning Python!")
Output: Hello, Emma! I am learning Python!
Tip: Words go inside quotes "like this". Don't forget them!
(^3) Ask a Person: input()
name = input("What is your name? ") age = int(input("How old are you? ")) print("Hi", name) print("Next year you will be", age + 1)
Output: What is your name? Jake How old are you? 11 Hi Jake Next year you will be 12
int() = text to whole number. float() = text to decimal.
(^4) Lists = A Line of Friends
friends = ["Emma", "Jake", "Mia"] print(friends[0]) friends.append("Sam") friends.remove("Jake") print(friends) print(len(friends))
Output: Emma ['Emma', 'Mia', 'Sam'] 3
friends[-1] gives the LAST friend in the line.
(^6) If / Else Decisions
height = 50 # inches if height >= 48: print("Jake can ride the coaster!") else: print("Sorry, maybe next year.")
Output: Jake can ride the coaster!
score = 85 if score >= 90: print("A") elif score >= 80: print("B") else: print("Keep trying!")
Output: B
Only ONE branch runs - the first one that is True.
(^7) Loops: Do It Again!
for kid in ["Emma", "Jake", "Mia"]: print("Hi,", kid + "!")
Output: Hi, Emma! Hi, Jake! Hi, Mia!
minutes = 3 while minutes > 0: print(minutes, "min to recess") minutes = minutes - 1 print("Recess time!")
Output: 3 min to recess 2 min to recess 1 min to recess Recess time!
Forget to change minutes? The loop never stops! Oops!
(^9) Classes = Person Blueprint
class Person: def init(self, name, age): self.name = name self.age = age def say_hello(self): print("Hi! I am", self.name)
emma = Person("Emma", 10) jake = Person("Jake", 11) emma.say_hello()
Output: Hi! I am Emma
One blueprint, unlimited people. That's the power of classes!
(^10) Inheritance: Kinds of People
class Student(Person): def init(self, name, age, grade): super().init(name, age) self.grade = grade def study(self): print(self.name, "is studying!")
mia = Student("Mia", 9, 4) mia.say_hello() # from Person mia.study()
Output: Hi! I am Mia Mia is studying!
Child classes get everything from the parent for free!
(^12) Mini Project: Class Directory
people = [] while True: choice = input("add / show / find / quit: ") if choice == "add": n = input("Name: ") people.append({"name": n}) elif choice == "show": for p in people: print(p["name"]) elif choice == "find": s = input("Search: ") print(any(p["name"] == s for p in people)) elif choice == "quit": break
Challenge: add age and favorite color to every ID card!
(^13) Quiz Time + Cheat Sheet
_1. print() shows words on the screen. T / F
print() - show on screen input() - ask a person [ ] list { } dictionary if / elif / else - decide for / while - repeat def - make a function class - make a blueprint try / except - handle errors
Answers: 1-T 2-F 3-T 4-T
I Can Code! This certifies that
is an awesome Python coder. Great job!