Variables & Data Types

Variables are used to store information to be referenced and manipulated in a computer program. Python is dynamically typed, meaning you don't need to declare the type.

1. Variable Naming

  • Must start with a letter or underscore.
  • Cannot start with a number.
  • Case-sensitive (age is different from Age).
  • Convention: Use snake_case (e.g., user_name).

2. Core Data Types

# 1. Integer (int)
score = 100

# 2. Float (float)
price = 19.99

# 3. String (str)
message = "Welcome to Python"

# 4. Boolean (bool)
is_active = True
is_admin = False

# 5. None (NoneType)
result = None

3. Type Checking & Casting

Use type() to find out what you are working with.

x = 10
print(type(x)) # Output: <class 'int'>

# Casting (Conversion)
y = str(x)    # "10"
z = float(x)  # 10.0

4. String Operations

Python strings are powerful and easy to manipulate.

name = "Alice"
greeting = f"Hello, {name}!" # f-strings (best practice)

print(name.upper()) # ALICE
print(len(name))    # 5
Tip: In Python, everything is an object. Even a simple integer has methods and properties attached to it.