Python uses Dynamic Typing, meaning it determines the data type automatically based on the value you assign to it.
1. Variables
A variable is created the moment you first assign a value to it using the = operator. You can even change a variable's type later in the code (though it's usually better to keep it consistent).
2. Standard Data Types
Python has several built-in data types grouped into these categories:
Category Data Type ExampleNumeric
int, float, complex5, 3.14, 1j
Textstr"Hello"
BooleanboolTrue or False
Sequencelist, tuple, range[1, 2, 3]M
appingdict{"name": "Alice"}
# No need to declare 'int' or 'float'
age = 25 # Integer
price = 99.99 # Float
name = "Python" # String
is_fun = True # Boolean (Must be capitalized!)
# You can check the type of any variable using type()
print(type(age)) # Output: <class 'int'>
print(type(price)) # Output: <class 'float'>
# Dynamic reassignment
x = 10
print(x)
x = "Now I am a string"
print(x)3. Type Casting
If you need to force a variable to be a specific type, you use constructor functions:
int()- converts to an integerfloat()- converts to a decimalstr()- converts to a string.
x = int(3.8) # x becomes 3 (truncates the decimal)
y = str(10) # y becomes "10"
z = float("5") # z becomes 5.0Why this is easier than C:
No Memory Management: You don't have to worry about how many bytes an integer takes; Python handles it.
Infinite Integers: In C, integers have a limit (overflow). In Python, integers can be as large as your computer's memory allows!