Variables & Types
Static typed C vs dynamically typed Python
Introduction
In this lesson, you'll learn about variables & types in Python. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with static typed c vs dynamically typed python.
Python has its own approach to static typed c vs dynamically typed python, which we'll explore step by step.
The Python Way
Let's see how Python handles this concept. Here's a typical example:
# No type declarations needed
age = 30
score = 9.5
pi = 3.14159
letter = 'A'
active = True # Python has a real bool type
# Constants (by convention, UPPER_CASE)
MAX = 100
print(f"age={age}, score={score:.1f}")
# Type hints (optional but recommended)
name: str = "Alice"
count: int = 0Comparing to C
Here's how you might have written similar code in C:
#include <stdio.h>
int main() {
int age = 30;
float score = 9.5f;
double pi = 3.14159;
char letter = 'A';
int active = 1; /* C has no bool, use int */
/* Constants */
const int MAX = 100;
printf("age=%d, score=%.1f\n", age, score);
return 0;
}You may be used to different syntax or behavior.
C requires explicit type declarations; Python infers types dynamically
You may be used to different syntax or behavior.
C has no bool — uses int (0/1); Python has True/False
You may be used to different syntax or behavior.
C's int/float/double → Python's int/float (arbitrary precision)
You may be used to different syntax or behavior.
Python variables can change type; C variables are fixed
Step-by-Step Breakdown
1. No Type Declarations
In Python you just assign a value — the interpreter figures out the type. In C you must declare the type first.
int age = 30;
float score = 9.5f;age = 30
score = 9.52. Boolean Type
C uses int (0 = false, non-zero = true). Python has a dedicated bool type with True and False literals.
int is_valid = 1;
if (is_valid) { /* ... */ }is_valid = True
if is_valid:
pass3. String Formatting
C uses printf with format specifiers. Python uses f-strings for clean, readable string interpolation.
printf("Hello, %s! Age: %d\n", name, age);print(f"Hello, {name}! Age: {age}")Common Mistakes
When coming from C, developers often make these mistakes:
- C requires explicit type declarations; Python infers types dynamically
- C has no bool — uses int (0/1); Python has True/False
- C's int/float/double → Python's int/float (arbitrary precision)
Key Takeaways
- C explicit types → Python dynamic typing
- C int 0/1 for booleans → Python True/False
- printf format strings → Python f-strings
- Python types can change; C types are fixed at declaration