FAQ Part 3: Variables and Data Types

How do I declare variables in Python?

Just assign a value: x = 5. Python automatically detects the data type.

What are the basic data types in Python?

int, float, str, bool, list, tuple, dict, and set.

How do I check a variable’s type?

Use type(x).

Can variable types change during execution?

Yes, Python variables are dynamically typed.

What is dynamic typing in Python?

Python uses dynamic typing, which means you don’t have to declare a variable’s type explicitly. The interpreter determines it at runtime:

x = 10        # int
x = "hello"   # now it's a str

What are mutable and immutable data types?

  • Mutable: Can be changed after creation (e.g., list, dict, set).
  • Immutable: Cannot be changed after creation (e.g., int, float, str, tuple).

How do I convert between data types?

Use built-in functions:

int("10")       # Converts string to int
float("3.14")   # Converts string to float
str(100)        # Converts int to string
list("abc")     # Converts string to list

What is the difference between is and ==?

  • == compares values.
  • is compares object identity (memory location).
a = [1, 2]
b = [1, 2]
print(a == b)  # True
print(a is b)  # False

What is the difference between None, False, and 0?

  • None: Represents the absence of a value.
  • False: Boolean value.
  • 0: Numeric zero.
    All are considered “falsy,” but they are not the same.

What is a complex number in Python?

Python has built-in support for complex numbers:

z = 2 + 3j
print(z.real)     # 2.0
print(z.imag)     # 3.0

Can I assign multiple variables at once?

Yes, Python supports multiple assignment:

a, b, c = 1, 2, 3
x = y = z = 0     # All variables assigned 0