§2.1 Variables and Data Types
KM
bykarutt
2025-06-03
Variables and Data Types
In Python, you use "variables" to handle data. Let's learn about variables and the "types (data types)" of data they can hold.
1. What is a Variable?
- Overview: A variable is like a "box" for storing data, each with its own name.
- What You Can Do: Temporarily store values like numbers, text, or
True/Falseand use them as many times as you want in your program. You can name variables freely (with some rules).
age = 20
name = "Taro"
is_student = True
print(age) # Outputs 20
print(name) # Outputs Taro
print(is_student) # Outputs True2. What is a Data Type?
- Overview: The "type" of data a variable can hold.
- What You Can Do: The type determines what operations you can perform.
Here are the four main data types commonly used in Python:
| Type | Description | Main Uses & Features | Example |
|---|---|---|---|
| int | Integer | Used for arithmetic operations | x = 10 |
| float | Decimal | Used for calculations with decimals | pi = 3.14 |
| str | String (text) | Used for text like sentences or names | s = "hello" |
| bool | Boolean (True/False) | Used for conditionals and logic operations | flag = False |
x = 5 # int
y = 2.5 # float
s = "Python" # str
flag = True # bool3. Checking the Type
- Use the
type()function to check a variable's data type.
print(type(x)) # <class 'int'>
print(type(s)) # <class 'str'>