C
PY

C to Python

10 lessons

Progress0%
1Variables & Types2Functions3Arrays → Lists4Structs → Classes & Dicts5Memory Management6String Handling7File I/O8Object-Oriented Programming9Exceptions and Context Managers10Standard Library and Tools
All Mirror Courses
C
PY
Variables & Types
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
C
From C:

In C, you're familiar with static typed c vs dynamically typed python.

PY
In 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:

PY
Python 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 = 0

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#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;
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C requires explicit type declarations; Python infers types dynamically

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C has no bool — uses int (0/1); Python has True/False

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

C's int/float/double → Python's int/float (arbitrary precision)

Mirror Card
C
From C:

You may be used to different syntax or behavior.

PY
In Python:

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.

C
C
int age = 30;
float score = 9.5f;
PY
Python
age = 30
score = 9.5

2. Boolean Type

C uses int (0 = false, non-zero = true). Python has a dedicated bool type with True and False literals.

C
C
int is_valid = 1;
if (is_valid) { /* ... */ }
PY
Python
is_valid = True
if is_valid:
    pass

3. String Formatting

C uses printf with format specifiers. Python uses f-strings for clean, readable string interpolation.

C
C
printf("Hello, %s! Age: %d\n", name, age);
PY
Python
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)
Common Pitfall
Don't assume Python works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Python to practice these concepts.
OverviewNext