Session 1 of 5 · 60 minutes
Hello, Python
Write your first program, store information in variables, and ask the user a question.
Goals
By the end of this session you can:
- Run a program that prints text to the screen
- Store information in a variable and use it again
- Tell apart text (str), whole numbers (int), decimals (float) and True/False (bool)
- Ask the user a question with input() and use the answer
Words to know
- program
- A list of instructions that a computer follows, one line at a time.
- variable
- A name that holds a value, like a labeled box.
- string (str)
- Text inside quotation marks, like "hello".
- integer (int)
- A whole number, like 7 or -3.
- input()
- A function that waits for the user to type something and gives it back as text.
The lesson
Step 1: Your first program
A program is a list of instructions. The computer reads them from top to bottom and does each one. The first instruction most people learn is print(), which shows something on the screen.
print("Hello, world!")Hello, world!The words inside the quotation marks are the text you want to show. Change them to your own name and run it again.
Try it: Print two lines, one after the other. Each
print()starts a new line.
Step 2: Variables are labeled boxes
Often you want to keep a value so you can use it later. A variable is a name that holds a value. The = sign puts the value on the right into the name on the left.
name = "Amara"
age = 12
print(name)
print(age)Amara
12Read age = 12 as “age gets 12”. Good variable names are lowercase, use underscores instead of spaces (favorite_color), and say what the value is for.
Variables can change. If you write age = 13 later, the old value is replaced.
Step 3: Kinds of data
Python treats different kinds of information differently. The type() function tells you which kind you have.
print(type("hi"))
print(type(7))
print(type(3.5))
print(type(True))<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>stris text, always in quotes.intis a whole number.floatis a number with a decimal point.boolis eitherTrueorFalse.
This matters because "2" + "3" joins text and gives 23, while 2 + 3 adds numbers and gives 5.
print("2" + "3")
print(2 + 3)23
5Step 4: Asking the user a question
input() pauses the program and waits for the user to type. Whatever they type comes back as text, so you can store it in a variable.
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")What is your name? Amara
Nice to meet you, Amara!Joining text with + works, but an f-string is easier. Put an f before the opening quote and write variables inside curly braces:
age = 12
print(f"Next year you will be {age + 1}.")Next year you will be 13.Because input() always gives text, you must convert it before doing math. int() turns text into a whole number.
age = int(input("How old are you? "))
print(f"In 5 years you will be {age + 5}.")How old are you? 12
In 5 years you will be 17.Step 5: Comments and reading errors
A line that starts with # is a comment. Python ignores it, so it is a place to leave notes for humans.
# Ask the player's name
name = input("Name? ")Everyone’s programs have errors, including experienced programmers. The trick is to read the last line of the message first. It says what went wrong.
print(Hello)NameError: name 'Hello' is not definedPython thought Hello was a variable because it had no quotes. Adding quotes fixes it: print("Hello").
Remember: an error message is a hint, not a punishment. Read the last line, then look at the line number it points to.
Exercise
Mad Libs
- Ask the user for an animal, a color and a number using
input()three times. Store each answer in its own variable. - Print one silly sentence that uses all three, for example:
The 5 purple elephants danced all night. - Run the program twice with different answers and check that the sentence changes.
Need a hint?
Use an f-string: print(f"The {number} {color} {animal}s danced all night.")
Small project
About Me card
Build a small program that interviews the user and then prints a neat card about them.
- Ask for the user's name, age and favorite food, storing each answer in a variable.
- Convert the age to a number with
int(). - Print a card with a title line, the three facts, and a last line that says how old they will be next year.
- Add a comment (a line starting with
#) above each part of your program explaining what it does.
Stretch it: Print a line of dashes above and below your card with print("-" * 30).
Quiz
Check your understanding
Pick one answer for each question. Your score appears right in the page; nothing is sent anywhere.
Feedback on this lesson
Teachers and students: tell us what worked and what didn't. We read this to improve the curriculum.
Received
Thank you for the feedback.
The Education team reviews lesson feedback to improve the curriculum.
Preview mode: this form is not connected to a destination yet, so nothing was sent. Set its web address in src/data/config.json.