Learning Hub/Python/Session 1

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.

Python
print("Hello, world!")
Output
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.

Python
name = "Amara"
age = 12
print(name)
print(age)
Output
Amara
12

Read 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.

Python
print(type("hi"))
print(type(7))
print(type(3.5))
print(type(True))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
  • str is text, always in quotes.
  • int is a whole number.
  • float is a number with a decimal point.
  • bool is either True or False.

This matters because "2" + "3" joins text and gives 23, while 2 + 3 adds numbers and gives 5.

Python
print("2" + "3")
print(2 + 3)
Output
23
5

Step 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.

Python
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")
Output
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:

Python
age = 12
print(f"Next year you will be {age + 1}.")
Output
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.

Python
age = int(input("How old are you? "))
print(f"In 5 years you will be {age + 5}.")
Output
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.

Python
# 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.

Python
print(Hello)
Output
NameError: name 'Hello' is not defined

Python 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

  1. Ask the user for an animal, a color and a number using input() three times. Store each answer in its own variable.
  2. Print one silly sentence that uses all three, for example: The 5 purple elephants danced all night.
  3. 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.

  1. Ask for the user's name, age and favorite food, storing each answer in a variable.
  2. Convert the age to a number with int().
  3. Print a card with a title line, the three facts, and a last line that says how old they will be next year.
  4. 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.

  1. Question 1What does this program print?print("2" + "3")
  2. Question 2Which line stores the number 10 in a variable called score?
  3. Question 3What type of value is 3.5?
  4. Question 4input() always gives you back…
  5. Question 5What happens when you run this without quotes?print(Hello)