Docsity

Python Notes for Student, Study Guides, Projects, Research of Computer science

Partial preview of the text

Download Python Notes for Student and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

Python for Kids

Meet Emma, Jake and Mia!

My first coding notebook - learn Python with people we know

Name: ______________ Grade: ______ Let's code!

(^1) Hello, Python!

Coding means giving a computer step-by-step instructions.

Python is a friendly language - it reads almost like English!

print() makes the computer say something on the screen.

Anything after # is a comment - a note just for humans.

My very first program

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()

input() waits for a person to type an answer.

It always gives back text - use int() to turn it into a number.

Let's find out how old a friend will be next year!

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

A list keeps many things in order, inside [ square brackets ].

Counting starts at 0! The first friend is number 0.

append() adds a friend. remove() takes one away. len()

counts them.

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

Computers make choices with if, elif (else if) and else.

Use == to check if two things are equal, > for greater than.

Remember the colon : and the indent (4 spaces)!

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!

A for loop repeats once for every item in a list.

A while loop repeats as long as something is True.

range(5) gives 0, 1, 2, 3, 4.

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

A class is a blueprint. An object is something built from it.

init sets up each new person. self means 'this person'.

Methods are actions a person can do, like say_hello().

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

Student and Teacher are both kinds of Person.

They inherit name and age, and can add their own things.

super() borrows the parent's setup code.

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

Plan: show a menu, then Add, Show or Find a friend.

We use a list of dictionaries - one ID card per person.

A while loop keeps the menu going until we choose Quit.

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

True or False? (Circle your answer)

_1. print() shows words on the screen. T / F

  1. Lists start counting at 1. T / F
  2. A class is a blueprint for objects. T / F
  3. Variables can store a person's name. T / F_

Cheat Sheet

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!