Level: Beginner | Prerequisite: Week 1 (Environment Setup,
print())
Learning Goals
- Understand the concept of variables and assignment syntax
- Distinguish the four basic data types (
int,float,str,bool) - Check the type of a variable using
type() - Perform type conversion using
int(),float(),str() - Understand the meaning of
None
1. What is a Variable
A variable is a name given to a value. It stores data in a certain location in memory and allows you to access that location using the name.
age = 20 #Store 20 in the variable called age
name = "Hong Gil-dong" #Store a string in name
print(age) #Print 20
= is not the mathematical 'equals' but an assignment. It means putting the value on the right into the variable on the left.
Variable Naming Rules
- Can only use letters, numbers, and
_. The first character cannot be a number. - Case-sensitive (
age≠Age) - Cannot use reserved words like
if,for - Convention: snake_case (
user_name,total_score)
2. Basic Data Types
| Type | Example | Description |
|---|---|---|
int |
10, -3, 0 |
Integer |
float |
3.14, -0.5 |
Float (decimal) |
str |
"hello", 'Hello' |
String (both Single/Double-Quotes are possible) |
bool |
True, False |
True/False (starts with a capital letter!) |
NoneType |
None |
Indicates "no value" |
→ Refer to examples/01_variables.py, 02_types.py.
3. Type Casting
Forces a type change. The input value is always a string, so it must be converted to a number to perform calculations.
num_str = "123"
num_int = int(num_str)
print(num_int + 7) # 130
| Function | Description |
|---|---|
int(x) |
Converts x to an integer |
float(x) |
Converts x to a float |
str(x) |
Converts x to a string |
bool(x) |
Converts x to a boolean |
Impossible conversions result in ValueError:
int("abc") # ValueError: invalid literal for int() with base 10: 'abc'
4. type() Function
print(type(10)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hi")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
Key Examples (examples/)
| File | Content |
|---|---|
01_variables.py |
Variable declaration and usage |
02_types.py |
Check type with type() |
03_casting.py |
Successful type conversion |
04_casting_error.py |
Type conversion failure (intentional error) |
⚠️ Common Mistakes
- Variable names starting with a number
2nd_player = "Bob" # SyntaxError
→ Change to second_player or player2.
True/Falsein lowercaseis_ok = true # NameError
→ First letter capitalized: True, False, None.
- Missing quotes in a string
name = Hong Gil-dong #NameError: name 'Hong Gil-dong' is not defined
→ Should be enclosed in quotes like `name = "Hong Gil-dong"` to be a string.
4. **Integer/String mixed addition**
```python
age = 20
print("Age:" + age) # TypeError
→ Either print("Age: " + str(age)) or f-string f"Age: {age}".
- Calculate
input()result as isn = input("Number:") #Always a string! print(n + 1) # TypeError
→ Convert to n = int(input("Number: ")).
❓ Frequently Asked Questions (FAQ)
Q1. Is there a difference between ' (single quote) and " (double quote)?
A. Functionally, they are the same. However, it is convenient to use the other type if you want to include quotes inside the string ("It's me").
Q2. Can any type be reassigned to a variable?
A. Yes. Python is a dynamically typed language, so you can x = 10 and then x = "hello". However, it is not recommended for readability.
Q3. Are 0 and False the same?
A. Comparing (==) returns True, but the types are different. int vs bool. Detailed equality will be studied later.
Q4. Can you put commas in numbers? (1,000,000)
A. No. You can use underscores instead of commas: 1_000_000 (for readability).
📝 Exercises (exercises/)
exercise1.md— Age calculatorexercise2.md— Output 5 variables + typeexercise3.md— A short program that uses all four data types
Answers (solutions/)
First try to solve it yourself and refer only when stuck.
Next Week
Week 3. Operators and Input/Output — Receiving user input and arithmetic operations