-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics_exercises.py
More file actions
98 lines (80 loc) · 3.67 KB
/
Copy pathbasics_exercises.py
File metadata and controls
98 lines (80 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ==========================================
# Python Learning Lab: 01 Basics
# ==========================================
# Instructions:
# 1. Read the instructions for each challenge.
# 2. Replace the # TODO placeholders with your solution.
# 3. Run the script: `python exercises.py` (or execute it in your IDE).
# 4. If all assertions pass, you will see "All tests passed successfully!"
print("--- Running Python Basics Exercises ---")
# ==========================================
# Challenge 1: Variable Assignment & Types
# ==========================================
# Assign an integer value to `age`, a float to `height`, and a string to `name`.
# Then, assign a boolean `is_learning` to True.
# TODO: Assign the variables below
name = "ritshea" # Should be a string
age = 25 # Should be an int
height = 1.75 # Should be a float
is_learning = True # Should be a boolean
# --- Verification ---
assert isinstance(name, str), "name should be a string"
assert isinstance(age, int), "age should be an integer"
assert isinstance(height, float), "height should be a float"
assert is_learning is True, "is_learning should be the boolean True"
print("✓ Challenge 1 passed!")
# ==========================================
# Challenge 2: Basic Arithmetic Operations
# ==========================================
# Calculate:
# - `sum_val`: Sum of 15 and 27.
# - `division_val`: Float division of 100 by 8.
# - `floor_div_val`: Floor division of 100 by 8.
# - `modulo_val`: Modulo of 17 by 5 (remainder).
# - `power_val`: 2 raised to the power of 10.
# TODO: Perform the calculations
sum_val = 15 + 27
division_val = 100 / 8
floor_div_val = 100 // 8
modulo_val = 17 % 5
power_val = 2 ** 10
# --- Verification ---
assert sum_val == 42, f"Expected sum 42, got {sum_val}"
assert division_val == 12.5, f"Expected division 12.5, got {division_val}"
assert floor_div_val == 12, f"Expected floor division 12, got {floor_div_val}"
assert modulo_val == 2, f"Expected modulo 2, got {modulo_val}"
assert power_val == 1024, f"Expected power 1024, got {power_val}"
print("✓ Challenge 2 passed!")
# ==========================================
# Challenge 3: Type Casting
# ==========================================
# Fix the operations below by casting variables to the correct type:
# - Convert string `str_num` to an integer.
# - Convert float `float_num` to an integer (discarding decimals).
# - Convert integer `int_num` to a string.
str_num = "150"
float_num = 99.9
int_num = 456
# TODO: Perform type casting
cast_int = int(str_num) # Convert str_num to int
cast_floor = int(float_num) # Convert float_num to int
cast_str = str(int_num) # Convert int_num to str
# --- Verification ---
assert cast_int == 150, f"Expected integer 150, got {cast_int}"
assert cast_floor == 99, f"Expected integer 99, got {cast_floor}"
assert cast_str == "456", f"Expected string '456', got {repr(cast_str)}"
print("✓ Challenge 3 passed!")
# ==========================================
# Challenge 4: String Formatting (f-strings)
# ==========================================
# Construct a sentence using an f-string:
# "My name is [name], I am [age] years old, and my height is [height]m."
# Use the variables `name`, `age`, and `height` defined in Challenge 1.
# TODO: Define the formatted sentence
# (Make sure to assign values in Challenge 1 first!)
sentence = f"My name is {name}, I am {age} years old, and my height is {height}m."
# --- Verification ---
expected_sentence = f"My name is {name}, I am {age} years old, and my height is {height}m."
assert sentence == expected_sentence, f"\nExpected: '{expected_sentence}'\nGot: '{sentence}'"
print("✓ Challenge 4 passed!")
print("\n🎉 All tests passed successfully!")