Chapter 1: Data Types and Variables — Your First Conversation with Python¶
How to Run Python¶
There are two ways to run Python.
CLI (Interactive) Mode¶
Type python or py in the terminal to get the >>> prompt.
>>> print("Hello, World!")
Hello, World!
>>> 2 + 3
5
REPL (Read-Evaluate-Print Loop): repeats input → evaluate → output. You can check results immediately, but everything disappears when you quit.
Script Mode¶
Create a .py file and run it.
hello.py → python hello.py
Since it's saved as a file, it's suitable for reuse, distribution, and automation.
| Mode | Feature | Use Case |
|---|---|---|
| CLI (Interactive) | Immediate execution, lost on exit | Quick testing |
| Script | Saved as file, repeatable | Real programming |
What Are Data Types?¶
Computers store everything as 0s and 1s.
01000001 = (int) 65 or (str) 'A'
The same bits can mean different things depending on the type.
Data types tell Python two things: 1. What kind of value it is (a number? a string?...) 2. What operations are available (can you add it? concatenate it?...)
Python's Major Data Types¶
str String "hello", "abc"
int Integer 42, -7, 0
float Float 3.14, -0.5
bool Boolean True, False
list List [1, 2, 3]
tuple Tuple (1, 2, 3)
dict Dictionary {"key": "value"}
C vs Python — Dynamic Typing¶
// C: You must declare the type explicitly
int A = 42;
# Python: Just assign the value (dynamic typing)
A = 42
Python automatically determines the type based on the value.
Checking Types: type()¶
print(type(-3)) # <class 'int'>
print(type(3.0)) # <class 'float'>
print(type("314")) # <class 'str'>
print(type(4 / 2)) # <class 'float'> ← Note!
| Value | Type |
|---|---|
-3 |
int |
3.0 |
float |
3.14 |
float |
"314" |
str |
"" |
str |
4/2 |
float |
str vs int Operations¶
# int: arithmetic
print(10 + 3) # 13
print(10 * 3) # 30
# str: concatenation, repetition
print("10" + "3") # 103 ← string concatenation, not math!
print("10" * 3) # 101010
Variables¶
What Is a Variable?¶
A variable is a name attached to a value.
A = 3
A: variable name=: assignment operator3: value — stored somewhere in memory
A = 3
print(A) # 3
A = 99 # reassignment (rebinding)
print(A) # 99
Checking Memory Address: id()¶
A = 3
print(id(A)) # e.g. 140234567890
print(hex(id(A))) # e.g. 0x7f8b2c3d4e50 (hexadecimal)
id() returns the memory address where the value is stored.
Variable Naming Rules¶
Valid names:
| Name | Explanation |
|---|---|
_a |
Starts with underscore |
abcd |
Lowercase |
a3 |
Letters + digits |
none |
Lowercase is not a keyword |
_1_ |
Underscore and digits combined |
Invalid names:
| Name | Reason |
|---|---|
ab cd |
No spaces allowed |
3a |
Cannot start with a digit |
None |
Python reserved word |
True |
Python reserved word |
if, for |
Python reserved words |
a3.14 |
No special characters |
Rule summary:
- Must start with a letter or _
- Case-sensitive (age ≠ Age)
- No spaces, special characters, or Python reserved words
Built-in Functions¶
print() — Output¶
print("Hello, World!") # Hello, World!
name = "Jimin"
print(name) # Jimin
print("Name:", name) # Name: Jimin
Thinking of it as a function:
f(x) = 2x
print("Hello") → input: "Hello" → output: Hello on screen
input() — Getting Input¶
name = input("Enter your name: ")
print("Hello,", name)
Enter your name: Jimin
Hello, Jimin
input() always returns a string (str).
age = input("Age: ")
print(type(age)) # <class 'str'>
# To use it as a number, convert the type
age = int(input("Age: "))
print(age + 1)
type() — Check Type¶
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
id() — Memory Address¶
x = 10
print(id(x)) # integer (memory address)
print(hex(id(x))) # displayed in hexadecimal
Practice Missions¶
Mission 1: Introduce Yourself¶
Create a program that asks for your name, age, and favorite subject using input(), then prints them out.
Enter your name: Jimin
Enter your age: 17
Favorite subject: Math
---
Hello! I'm Jimin.
Age: 17
Favorite subject: Math
Mission 2: Type Detective¶
Check the type of each value below using type() and compare your predictions with the actual results.
values = [-3, 3.0, 3.14, 314, "314", "", "int64", 4/2, True]
for v in values:
print(v, "→", type(v))
Mission 3: Circle Area¶
Take the radius as input and calculate the area of a circle.
Area = π × r² (π = 3.14)
r = float(input("Radius: "))
# Write your code here
# Radius: 5
# Circle area: 78.5
Mission 4: Variable Swap¶
Swap the values of a and b. (Use a temporary variable or Python's tuple unpacking)
a = 10
b = 20
# Write your code here
print(a) # 20
print(b) # 10
Key Summary¶
| Concept | Description |
|---|---|
| CLI mode | >>> prompt, interactive execution |
| Script mode | Save and run .py files |
| Data type | Kind of value (int, float, str, bool...) |
| Dynamic typing | Python automatically determines types |
| Variable | A name attached to a value stored in memory |
print() |
Output to screen |
input() |
Receives user input as str |
type() |
Check data type |
id() |
Check memory address |