Data types:
String (str) "Hello" / "Lara" / "123" / "10_000" / "Good morning"
Integer (int) 5 / -10 / 0 / 250 / 10_000
Float (float) 3.14 / 0.5 / -2.75 / 10.0 / 99.99
Boolean (bool) True False
List (list) Stores multiple items in order. Can be changed. ["apple", "banana", "orange"] / [1, 2, 3, 4]
Tuple (tuple) Like a list, but cannot be changed. ("apple", "banana", "orange") / (1, 2, 3, 4)
Dictionary (dict) Stores key : value pairs. {"name": "Lara", "age": 16} / {"color": "blue", "size": "large"}
Quick memory trick: List → [ ] → changeable Tuple → ( ) → not changeable
Dictionary → { key : value } → labels with values
The order of operations in Python is:
1. () Parentheses
2. ** Exponent (powers)
3. *, /, //, % Multiplication, Division, Floor Division, Modulus
4. +, - Addition, Subtraction
5. = Assignment (done after the expression on the right is evaluated)
if Statement: Runs code only if a condition is True.
Syntax:
if condition:
Statement
Example:
age = 18
if age >= 18:
print("Adult")
for Loop: Repeats a set number of times or for each item in a collection.
Syntax:
for variable in sequence:
Statement
Example:
for i in range(5):
print(i)
while Loop: Repeats as long as a condition is True.
Syntax:
while condition:
Statement
Example:
x=0
while x < 5:
print(x)
x += 1
Functions: A function is a reusable block of code.
Syntax
def function_name():
Statements
Example
def greet():
print("Hello")
greet()
Function with Parameters
def greet(name):
print("Hello", name)
greet("Lara")
Function with Return Value
def add(a, b):
return a + b
result = add(3, 4)
Rules
● Start with def
● Function name should follow variable naming rules
● Parentheses () are required
● End the first line with :
● Indent the code inside the function
● Use return if you want the function to give back a value
● Call the function using its name and parentheses
def my_function():
Pass
if, elif, else: Used to make decisions.
Syntax:
if condition1:
statement
elif condition2:
statement
else:
statement
Example:
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
else:
print("C")
Rules
● if comes first
● You can have 0 or more elif
● else is optional and comes last
● Conditions end with :
● Indent the code inside each block
● Only the first true condition runs (otherwise else runs)
Q18-Assuming that the phone_dir dictionary contains name:number pairs, arrange the
code boxes to create a valid line of code which retrieves Martin Eden's phone number
and assigns it to the number variable. &&*
1 point
A. number = phone_dir["Martin Eden"]
B. phone_dir = number["Martin Eden"]
C. number["Martin Eden"] = phone_dir
D. number = ["Martin Eden"] phone_dir