๐ Lesson 3: Python Variables, Data Types & Input / Output
Welcome to Lesson 3! Today, we’ll explore one of the most important foundations of Python: variables, data types, and getting input from users. By the end of this lesson, you’ll know how to store data, print it, and interact with users through the terminal.
⭐ What You Will Learn in This Lesson
- How to use variables to store information
- Understanding Python’s basic data types
- How to print output in Python
- How to get input from users
- Converting input to the correct data type
๐ฅ Who Is This Lesson For?
- Beginners who want to understand how Python stores and handles data
- Anyone learning to make interactive Python programs
- Students and professionals starting Python scripting
๐ฆ What Are Variables?
Variables are like boxes where you can store information. You can put text, numbers, or other types of data inside them. Once stored, you can use or manipulate this data in your program.
Example:
name = "Alice"
age = 20
Here, name stores text (string), and age stores a number (integer).
๐ Python Data Types
Python has several basic data types you’ll use frequently:
- str – text strings (e.g., "Hello")
- int – whole numbers (e.g., 5, 100)
- float – decimal numbers (e.g., 5.7, 3.14)
- bool – True or False
type() function.
๐จ Printing Output
You’ve already seen this function:
print("Hello!")
You can also print variables:
name = "Alice"
print(name)
⌨️ Getting Input from Users
Use the input() function to ask the user for information:
name = input("Enter your name: ")
print("Hello, " + name)
input() always returns a string. You may need to convert it if you want numbers.
๐ง Type Conversion
If you want to perform calculations, convert input to the right type:
age = int(input("Enter your age: "))
print(age + 1)
Without conversion, Python would show an error if you try to add a number to a string.
๐งช Practice
Try these exercises to strengthen your understanding:
- Create a Python file named user_info.py.
- Ask the user for:
- Their name
- Their age
- Their city
- Print everything in a readable sentence using
print()and type conversion:
print("Hello " + name + "! You are " + str(age) + " years old and live in " + city + ".")
❌ Common Mistakes
- Forgetting to convert input to int or float when performing calculations
- Using variable names that start with numbers or special characters
- Trying to add strings and numbers without conversion
❓ Frequently Asked Questions (FAQ)
1. What is a variable?
A variable is a named storage for data. You can store text, numbers, or other types in it.
2. Do I need to declare the type of variable in Python?
No. Python automatically detects the type when you assign a value.
3. Why do I get errors when adding numbers to input?
Because input() always returns a string. You must convert it using int() or float().
4. Can I store multiple data types in the same variable?
Yes, but the variable’s type will change depending on the last value assigned.
๐ What’s Next?
In the next lesson, you’ll learn about:
- Python operators
- Mathematical expressions
- Performing calculations in Python
Comments
Post a Comment