๐ Python for Teens & Beginners ยท Lesson 2 of 7
Variables and input
Store information and ask the user.
Variables
name = "Maya"
age = 12
print(name)
print(age + 1)A variable is a name for a value. = means โstoreโ (not โequalsโ like in math). Now name holds "Maya" and age holds 12.
Naming rules
| Letters, digits, _ | favorite_color, score2 |
|---|---|
| Canโt start with a digit | 2score is an error |
| No spaces | Use snake_case: high_score |
| Meaningful | total_price, not x |
input
name = input("What is your name? ")
print("Nice to meet you, " + name + "!")input waits for the user to type and press Enter, and returns what they typed โ always as a string.
Numbers from input
age = int(input("How old are you? "))
print("Next year you will be", age + 1)int() turns the string "12" into the number 12. Use float() for decimals like 3.5. print with commas adds spaces between items.
Tip
๐ก f-strings make printing easy: print(f"Hi {name}, you are {age}") โ put an f before the quotes and variables in {curly braces}.
โ๏ธ Practice: your turn
- Ask the user for their name and favorite animal, and print a sentence with both
- Ask for a birth year and print the age they turn this year
- Rewrite one print using an f-string
- Try int("hello") and read the error
๐ง Quick quiz
Answer all 4 questions correctly to mark this lesson as done.
0 of 4 correct
All lessons in Python for Teens & Beginners
- ๐ 1. Your first programprint, quotation marks, and error messages
- ๐ฆ 2. Variables and inputStore information and ask the user
- ๐งฎ 3. Math in PythonOperators, division, remainder and powers
- ๐ 4. Conditions: if and elseThe program decides by itself
- ๐ 5. Loops: for and whileRepeat code without copying it
- ๐ 6. ListsMany values in one variable
- ๐ฒ 7. Project: a guessing gameThe computer picks a number, and you guess