Python Basics: A
Beginner's Learning Guide
Welcome back! You've already learned how to use the print() statement - great
job! Now let's take the next steps together. Don't worry if things seem new at
first - we'll go step by step.
Part 1: Python as Your Calculator m
Did you know Python can work like a calculator? Let's try it!
1 2
Addition (+) Subtraction (-)
Type this exactly and press Enter:
print(20 - 5)
print(100 - 30)
print(5 + 3)
What do you see? The answers should be 15 and 70.
What happened? You should see 8 appear on your screen!
Let's try more:
print(10 + 7)
print(25 + 15)
Your Turn: Try adding your age to 10. If you're 16, type
print(16 + 10)
3 4
Multiplication (*) Division (/)
In Python, we use * (asterisk) for multiplication:
print(20 / 4)
print(15 / 3)
print(4 * 5)
print(7 * 8)
Notice: Even when the answer is a whole number, Python
shows .0 at the end (like 5.0). This is normal!
Try this: Calculate how many hours are in 3 days: print(24 *
3)
Let's Practice Together!
Try these calculations and check your answers:
print(6 + 4) # Should show 10
print(12 - 7) # Should show 5
print(3 * 9) # Should show 27
print(18 / 6) # Should show 3.0
Part 2: Variables - Your Data Storage Boxes /
Think of variables like labeled boxes where you can store information.
What is a Variable? Creating Your First Variable Variables in Calculations
A variable is like a box with a name
tag. You put something inside and give name = "Alex" x = 10
it a name so you can find it later. print(name) y=5
print(x + y)
What happened? print(x - y)
print(x * y)
You created a box called name
You put "Alex" inside it Cool, right? The variables remember
You printed what's inside the box the numbers you stored!
Variable Naming Rules (Important!)
' Good variable names: o Bad variable names (these will cause errors):
my_name = "Sarah" 2age = 16 # Can't start with a number
age = 16 my-name = "Tom" # Can't use dashes
favorite_color = "blue" class = "Math" # Can't use special Python words
number1 = 25
Memory Tip: Variable names should be like labels on boxes - clear and descriptive!
Part 3: Data Types - Different Kinds of Information
Just like in real life, we have different types of information. Python recognizes these types automatically!
1 Type 1: Text (String) 2 Type 2: Whole Numbers (Integer)
When you want to store text, wrap it in quotes: For whole numbers, no quotes needed:
student_name = "Emma" student_count = 25
favorite_food = "pizza" test_score = 95
message = "Hello, world!" year = 2024
Important: Always use quotes around text!
3 Type 3: Decimal Numbers (Float) 4 Type 4: True/False (Boolean)
For numbers with decimal points: For yes/no or true/false information:
height = 5.8 is_student = True
price = 12.99 is_raining = False
temperature = 98.6 homework_done = True
Note: True and False must be capitalized exactly like this!
Part 4: Checking Data Types with type() U
Want to know what type of data you have? Use the type() function!
name = "John"
age = 17
height = 5.9
is_tall = True
print(type(name)) # Shows: <class 'str'>
print(type(age)) # Shows: <class 'int'>
print(type(height)) # Shows: <class 'float'>
print(type(is_tall)) # Shows: <class 'bool'>
What do these mean?
str = String (text)
int = Integer (whole number)
float = Float (decimal number)
bool = Boolean (True/False)
Quick Check Exercise
mystery1 = "25"
mystery2 = 25
print(type(mystery1)) # What do you think this will show?
print(type(mystery2)) # What about this one?
Answer: mystery1 is text (str) because it has quotes, mystery2 is a number (int)!
Part 5: Joining Text Together (Concatenation) _
Sometimes you want to combine different pieces of text. This is called "concatenation."
Method 1: Using the + Sign Method 2: F-strings (The Modern Way!)
This is easier and more popular:
first_name = "Taylor"
last_name = "Smith"
name = "Chris"
full_name = first_name + " " + last_name
age = 16
print(full_name) # Shows: Taylor Smith
message = f"My name is {name} and I am {age} years
old"
Notice: We added " " to put a space between the names! print(message) # Shows: My name is Chris and I am
More Examples 16 years old
greeting = "Hello" How f-strings work:
name = "Anna" 1. Put f before the quotes
message = greeting + ", " + name + "!"
2. Put variables inside curly brackets {}
print(message) # Shows: Hello, Anna!
3. Python automatically fills in the values!
Common Mistake to Avoid
name = "Sam"
age = 15
# This will cause an ERROR:
# message = "I am " + name + " and I am " + age + " years old"
# Why? You can't add text and numbers directly!
# CORRECT ways:
message1 = "I am " + name + " and I am " + str(age) + " years old" # Convert number to text
message2 = f"I am {name} and I am {age} years old" # Use f-string (easier!)
print(message1)
print(message2)
Let's Put It All Together!
Here's a complete example using everything we learned:
# Variables with different types
student_name = "Maya"
math_grade = 92
science_grade = 88.5
passed = True
# Calculations
average_grade = (math_grade + science_grade) / 2
# Display information using f-strings
print(f"Student: {student_name}")
print(f"Math grade: {math_grade}")
print(f"Science grade: {science_grade}")
print(f"Average grade: {average_grade}")
print(f"Passed: {passed}")
# Check the data types
print(f"Name type: {type(student_name)}")
print(f"Math grade type: {type(math_grade)}")
print(f"Science grade type: {type(science_grade)}")
print(f"Passed type: {type(passed)}")
Practice Exercises í
Try these step by step:
Exercise 1: About You Exercise 2: Simple Exercise 3: Data Type
Calculator Detective
# Fill in your information
your_name = "Your Name Here" # Create two numbers # What types are these?
your_age = 0 # Put your real age num1 = 15 item1 = "Hello"
your_height = 0.0 # Put your num2 = 4 item2 = 42
height item3 = 3.14
# Do all calculations item4 = True
print(f"Hi! I'm {your_name}") addition = num1 + num2 item5 = "123"
print(f"I am {your_age} years subtraction = num1 - num2
old") multiplication = num1 * num2 # Check each type
print(f"I am {your_height} feet division = num1 / num2 print(f"'{item1}' is type:
tall") {type(item1)}")
# Print results print(f"'{item2}' is type:
print(f"{num1} + {num2} = {type(item2)}")
{addition}") print(f"'{item3}' is type:
print(f"{num1} - {num2} = {type(item3)}")
{subtraction}") print(f"'{item4}' is type:
print(f"{num1} * {num2} = {type(item4)}")
{multiplication}") print(f"'{item5}' is type:
print(f"{num1} / {num2} = {type(item5)}")
{division}")
Quick Reference Card &
Arithmetic Operators Data Types
+ Addition String (str): Text in quotes "hello"
- Subtraction Integer (int): Whole numbers 42
* Multiplication Float (float): Decimal numbers 3.14
/ Division Boolean (bool): True or False
Essential Functions Variable Rules
print() - Display information 1. Start with letter or underscore
type() - Check data type 2. Can contain letters, numbers, underscores
str() - Convert to text 3. No spaces or special characters
4. Case sensitive
What's Next?
Congratulations! You now know: ' How to do calculations in Python ' How
to store information in variables ' The different types of data ' How to
check data types ' How to combine text together
Keep practicing these concepts - they're the foundation for everything else in
Python. In our next lesson, we'll learn about getting input from users and
making decisions with if statements!
Remember: Programming is like learning to ride a bike. It takes practice, but
once you get it, you'll never forget!