Goal

You’ll be able to create variables of the core Python data types, and understand why Python’s approach to typing is genuinely different from many other languages, not just a syntax difference.

Learn

A variable is a name that points to a value in memory. In Python, creating one is simple — no separate declaration step, just assignment:

name = "Ahmed"
age = 25
price = 19.99
is_active = True

Python has several core built-in data types, each genuinely different in what it can represent:

  • str (string) — text, always written in quotes: "Ahmed"
  • int (integer) — whole numbers: 25
  • float — decimal numbers: 19.99
  • bool (boolean) — exactly two possible values, True or False

Python is called dynamically typed: you never declare a variable’s type explicitly — Python figures it out automatically from the value you assign, and a variable can even be reassigned to a completely different type later:

x = 5        # x is now an int
x = "hello"  # x is now a str — completely legal in Python

You can check any variable’s actual type using the built-in type() function:

type(age)   # returns <class 'int'>

This flexibility is genuinely useful for quick scripting, but it also means Python won’t catch a type mismatch for you automatically the way some other languages would — a real trade-off worth understanding, not just a convenience.

Decision Task

You write quantity = "5" intending to use it in a math calculation later. Before reading on: what type is quantity actually, and what real problem will this cause if you try to do quantity + 1?

Show Answer

quantity is a str (string), not an int, because it’s wrapped in quotes — Python treats “5” as text, not a number. Attempting quantity + 1 will raise a TypeError, since Python won’t automatically combine a string and an integer with the + operator; they need to be the same compatible type first.

Common Mistake

Assuming Python automatically converts between types when it seems “obvious” to a human, like adding a string number to an integer. Python’s dynamic typing means it doesn’t require you to declare types upfront, but it still enforces real type rules during operations — it infers types automatically, but it doesn’t silently convert between incompatible ones without being explicitly told to.

Practice Questions

1. What type would Python assign to a variable created as score = 95?

Show Answer

int — a whole number with no decimal point.

2. Write a variable assignment for a boolean value representing whether a user is logged in, set to False.

Show Answer

is_logged_in = False

3. What does the type() function do?

Show Answer

Returns the actual data type of a given variable or value.

4. True or False: once a variable is created with one type in Python, it can never be reassigned to a different type later.

Show Answer

False — Python variables can be reassigned to a completely different type at any point; this is exactly what “dynamically typed” means.

5. Why might dynamic typing be described as both a convenience and a real risk?

Show Answer

It’s convenient because you never need to declare types upfront, but risky because Python won’t automatically catch a type mismatch for you the way some statically-typed languages would, potentially causing runtime errors that a stricter language might catch earlier.

Try It Yourself

Without looking back, create four variables representing a product: name (string), price (float), quantity (int), and in_stock (boolean) — using genuinely appropriate values.

Show Answer

Example: name = "Notebook", price = 4.99, quantity = 12, in_stock = True — check that each value matches its intended type (quotes for string, decimal for float, whole number for int, True/False for boolean).

Quick Check

1. What are the four core data types covered in this lesson?

Show Answer

str, int, float, bool.

2. What does “dynamically typed” mean in Python?

Show Answer

You never declare a variable’s type explicitly; Python infers it automatically from the assigned value, and it can change on reassignment.

3. How do you check a variable’s type in Python?

Show Answer

Using the built-in type() function.

4. What character wraps around text to make it a string?

Show Answer

Quotes (either single ‘ or double “).

5. What are the only two valid values for a bool?

Show Answer

True and False.

تحميل هذا الباب / Download this Chapterنسخة كاملة للدراسة بدون إنترنت، مع الأسئلة والإجابات والصور المتاحة.