Variables, built-in data types, dynamic vs strong typing, and Python's core equality and mutability rules.
Question 1: What is a variable in Python?
Ans
A Python variable is a name bound to an object. Assignment does not declare a fixed variable type; the same name can later refer to an object of another type.
Example
python
x = 10
x = "Python"
print(x)
Important Point
The object has a type; the name is a reference/binding to that object.
Question 2: What are Python data types?
Ans
Common built-in types include int, float, complex, bool, str, list, tuple, set, dict, bytes, bytearray, range, and NoneType. Python also lets developers create custom classes.
Example
python
age = 25
name = "Amit"
marks = [80, 90]
Important Point
Use `type()` or `isinstance()` when type inspection is actually needed.
Question 3: What is dynamic typing?
Ans
Dynamic typing means the type associated with a value is determined at runtime and a name can be rebound to objects of different types.
Example
python
value = 10
value = "ten"
Important Point
Dynamic typing does not remove type checking; many type errors are discovered when the relevant code executes.
Question 4: What is strong typing in Python?
Ans
Python generally does not perform arbitrary implicit conversions between unrelated types. For example, adding a string and an integer raises TypeError.
PEP 8 is a style guideline, not a rule that changes Python's execution semantics.
Question 7: What is None?
Ans
None is Python's singleton value representing the absence of a value or a deliberate 'no result' state.
Example
python
def find_user(user_id):
return None
print(find_user(10) is None)
Important Point
Use `is None` rather than `== None` for the usual identity check.
Question 8: What is mutable vs immutable?
Ans
Mutable objects can be changed after creation, while immutable objects cannot. Lists and dictionaries are mutable; strings, tuples, integers, and frozensets are immutable.
Example
python
items = [1, 2]
items.append(3) # mutation
name = "Py"
name += "thon" # creates/rebinds a string
Important Point
A tuple is immutable as a container, but it can contain a mutable object whose state changes.
Question 9: What is type conversion?
Ans
Type conversion changes a value from one type to another using constructors or conversion functions such as int(), float(), str(), list(), and tuple().
Example
python
age = int("25")
price = float("19.5")
print(age + 1)
Important Point
Conversion can fail, such as `int("abc")`, which raises ValueError.
Question 10: What is the difference between == and is?
Ans
== compares values for equality, while is checks whether two references point to the same object. Use == for value comparison and is mainly for identity checks such as None.
Example
python
a = [1, 2]
b = [1, 2]
print(a == b) # True
print(a is b) # False
Important Point
Do not use `is` as a general replacement for `==`.