Python Basics

Variables & Types

Variables and Data Types

Variables are containers for storing data. Python is dynamically typed, meaning you don't need to declare variable types explicitly.

Basic Data Types

TypeDescriptionExample
strText strings"Hello"
intWhole numbers42
floatDecimal numbers3.14
boolTrue/FalseTrue
NoneNull valueNone

Variable Naming Rules

  • Use lowercase with underscores: agent_name
  • Be descriptive: user_message not um
  • Don't start with numbers
  • Avoid reserved words like class, if, for

Type Hints (Modern Python)

Type hints make code clearer and help catch bugs:

def greet(name: str) -> str:
    return f"Hello, {name}!"

Try It Yourself

Run this code example to practice what you've learned.

example.py
1# String - text data
2model_name: str = "gpt-4"
3prompt: str = "Explain quantum computing"
4
5# Integer - whole numbers
6max_tokens: int = 1000
7retry_count: int = 3
8
9# Float - decimal numbers
10temperature: float = 0.7
11top_p: float = 0.9
12
13# Boolean - True/False
14stream: bool = True
15is_complete: bool = False
16
17# None - absence of value
18response = None
19
20# Check types
21print(f"model_name is {type(model_name).__name__}")
22print(f"temperature is {type(temperature).__name__}")
23
24# Type conversion
25tokens_str = "500"
26tokens_int = int(tokens_str)
27print(f"Converted: {tokens_int + 100}")