Variables & Data Types
Checking access...
Python is dynamically typed — variables can hold any type of value, and their type can change.
Variables and Assignment
# No type declarations neededname = "Alice" # strage = 25 # intheight = 1.68 # floatis_student = True # bool
# Dynamic typing — variables can change typevalue = 42 # intvalue = "now text" # str — perfectly validFundamental Data Types
Numbers
# Integercount = 100binary = 0b1010 # 10 in decimalhex_val = 0xFF # 255 in decimal
# Floatpi = 3.14159scientific = 1.5e-4 # 0.00015
# Complex numbersz = 3 + 4jprint(z.real, z.imag) # 3.0 4.0
# Type conversionint("42") # 42float("3.14") # 3.14round(3.14159, 2) # 3.14Booleans
is_ready = Trueis_done = False
# Boolean from comparisonsprint(5 > 3) # Trueprint(10 == 9) # False
# Truthy/falsy valuesbool(0) # Falsebool("") # Falsebool([]) # Falsebool(42) # Truebool("hello") # TrueThe None Type
result = None # Represents absence of a value
# Common pattern: initialize then assignuser = None# ... later ...user = get_user() # might return Noneif user is None: print("User not found")Type Checking
type(42) # <class 'int'>type("hello") # <class 'str'>type(True) # <class 'bool'>
# Check with isinstance() — preferredisinstance(42, int) # Trueisinstance("hi", (str, int)) # True — checks against tuple of typesType Hints (Python 3.6+)
Type hints document expected types (not enforced at runtime):
def greet(name: str) -> str: return f"Hello, {name}"
age: int = 25# Type hint is ignored at runtime:age = "twenty-five" # No error!Variables Are References
a = [1, 2, 3]b = a # b references the SAME listb.append(4)print(a) # [1, 2, 3, 4] — a changed too!
# Create a copy insteadc = a.copy()c.append(5)print(a) # [1, 2, 3, 4] — unchangedKey Takeaways
- Python uses dynamic typing — no type declarations needed
- Core types:
int,float,str,bool,NoneType - Use
isinstance()overtype()for type checking Nonerepresents “no value” — compare withis None- Variables are references to objects, not containers