Get ready for your next interview with our comprehensive question library
Python 3 introduced several breaking changes from Python 2:
print "hello"
vs print("hello")
5/2
returns 2.5
in Python 3, 2
in Python 2range()
returns an iterator in Python 3raw_input()
was renamed to input()
Python 2 reached end-of-life in 2020, so Python 3 is the current standard.
PEP 8 is the official style guide for Python code. It provides conventions for:
Python uses indentation to define code blocks instead of braces {}
. This enforces consistent code structure and improves readability.
if x > 0:
print("Positive") # 4 spaces indentation
if x > 10:
print("Greater than 10") # 8 spaces indentation
else:
print("Non-positive")
Incorrect indentation results in IndentationError
.
Keywords are reserved words with special meaning: if
, else
, for
, while
, def
, class
, import
, etc.
Identifiers are names for variables, functions, classes, etc. Rules:
# Valid identifiers
variable_name = 10
_private_var = 20
ClassName = "example"
# Invalid identifiers
# 123variable = 10 # Cannot start with digit
# class = "test" # Cannot use keyword
Python has several built-in data types:
Numeric: int
, float
, complex
Sequence: str
, list
, tuple
Set: set
, frozenset
Mapping: dict
Boolean: bool
None: NoneType
integer = 42
floating = 3.14
string = "Hello"
boolean = True
none_value = None
List | Tuple |
---|---|
Mutable (can be changed) | Immutable (cannot be changed) |
Uses square brackets [] |
Uses parentheses () |
Slower performance | Faster performance |
More memory usage | Less memory usage |
# List - mutable
my_list = [1, 2, 3]
my_list[0] = 10 # Works
# Tuple - immutable
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # TypeError
Dictionary is a mutable, unordered collection of key-value pairs. Keys must be immutable and unique.
student = {
"name": "John",
"age": 25,
"grades": [85, 90, 78]
}
# Accessing values
print(student["name"]) # John
print(student.get("age")) # 25
# Adding/updating
student["email"] = "john@email.com"
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime