0% found this document useful (0 votes)
2 views3 pages

Python Midterms Study Guide

The Python Midterms Study Guide covers key concepts such as tuples, dictionaries, loops, conditionals, and functions, providing definitions and examples for each topic. It includes problems and solutions to reinforce understanding of how to implement these concepts in Python. Final tips emphasize the importance of understanding print vs return, practicing loops, and mastering conditional logic.

Uploaded by

kjudani1207
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Python Midterms Study Guide

The Python Midterms Study Guide covers key concepts such as tuples, dictionaries, loops, conditionals, and functions, providing definitions and examples for each topic. It includes problems and solutions to reinforce understanding of how to implement these concepts in Python. Final tips emphasize the importance of understanding print vs return, practicing loops, and mastering conditional logic.

Uploaded by

kjudani1207
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Midterms Study Guide

(Based on Course Notes + Midterm Problems)

1. Tuples
From Notes:
- Tuples are sequences like lists, but immutable (cannot be modified).
- Defined with parentheses: ("a", "b", "c").
- You can unpack tuples into separate variables.

Problems & Solutions:


# 1. Create a tuple
Cars = ("BMW", "Dodge", "Ford")

# 2. Access the second element


print(Cars[1]) # Dodge

# 3. Unpack name and age


person = ("Alice", 30)
name, age = person
print(name, age)

# 4. Function: Area & Perimeter


def rectangle_metrics(length, width):
area = length * width
perimeter = 2 * (length + width)
return area, perimeter

print(rectangle_metrics(2, 10)) # (20, 24)

2. Dictionaries
From Notes:
- Store data as key-value pairs.
- Access with dict[key], not index.
- Can add, update, or replace values.

Problems & Solutions:


Menu = {'meal_1':'Spaghetti', 'meal_2':'Fries',
'meal_3':'Hamburger', 'meal_4':'Lasagna'}

# 1. Access second meal


print(Menu['meal_2'])

# 2. Add "Soup"
Menu['meal_5'] = "Soup"

# 3. Replace Hamburger
Menu['meal_3'] = "Cheeseburger"

# 4. Add desserts as meal_6


Menu['meal_6'] = ['Cake', 'Ice Cream']

# 5. Price list for first 5 meals


keys = ['meal_1','meal_2','meal_3','meal_4','meal_5']
prices = [10, 5, 8, 12, 5]
Price_list = {k: p for k, p in zip(keys, prices)}
print(Price_list)

3. For Loops
From Notes:
- Iterate over a list or sequence.
- Syntax: for x in list: print(x)

Problems & Solutions:


digits = [0,1,2,3,4,5,6,7,8,9]

# Print on new lines


for d in digits:
print(d)

# Print on one line


for d in digits:
print(d, end=" ")

4. While Loops and Incrementing


From Notes:
- while condition: repeats until condition is false.
- Use x += n for incrementing.

Problem & Solution:


# Print odd numbers 0–30
i = 1
while i <= 30:
print(i, end=" ")
i += 2

5. range() Function
From Notes:
- range(stop) → numbers from 0 to stop-1
- range(start, stop, step) → flexible

Problems & Solutions:


print(list(range(1, 11))) # 1 to 10
print(list(range(20))) # 0 to 19
print(list(range(0, 31, 2))) # even numbers 0 to 30

6. Conditionals + Loops
From Notes:
- if, elif, else control the flow.
- Often combined with loops.

Problems & Solutions:


# 1. Multiply list items by 2
for x in range(1, 11):
print(x * 2)

# 2. Print odd numbers, "Even" for evens


for x in range(1, 31):
if x % 2 == 1:
print(x)
else:
print("Even")

# 3. Multiply list items by 10


n = [1,2,3,4,5,6]
for v in n:
print(v * 10)

7. Functions + Loops + Conditionals


From Notes:
- Define functions with def.
- Use return to give back values.

Problem & Solution:


nums = [1,35,12,24,31,51,70,100]

count = 0
i = 0
while i < len(nums):
if nums[i] < 20:
count += 1
i += 1

print(count) # 2

8. Iterating over Dictionaries


From Notes:
- Use .items() to loop over keys & values.

Problem & Solution:


prices = { "box_of_spaghetti": 4, "lasagna": 5, "hamburger": 2 }
quantity = {"box_of_spaghetti": 6, "lasagna": 10, "hamburger": 0}

total = 0
for item, price in [Link]():
if price >= 5:
total += price * quantity[item]

print(total) # 50

■ Final Tips
- Review print vs return in your notes → critical for functions.
- Practice both for and while loops → they are interchangeable but used in different cases.
- Re-read the conditional logic flow (if → elif → else).
- Use range() often for controlled iterations.

You might also like