Skip to content

Commit 1a7c06b

Browse files
committed
Add why_we_use_types_exercise1.py with type behavior demonstration
- Create exercise file demonstrating why type hints matter - Include half(), double(), and second() functions - Add example showing string multiplication vs numeric multiplication - Document expected behavior: double("22") returns "2222" due to string repetition - Include question and answer explaining the type-dependent behavior
1 parent 2fc8ae2 commit 1a7c06b

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

prep/why_we_use_types_exercise1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def half(value):
2+
return value / 2
3+
4+
def double(value):
5+
return value * 2
6+
7+
def second(value):
8+
return value[1]
9+
10+
11+
print(double("22"))
12+
13+
# Question
14+
# Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did?
15+
16+
# Answer
17+
# The function double("22") will return "2222".
18+
# When I ran the program, it returned what I expected.
19+
# The return value is "2222" because the argument is a string, so the * operator duplicates the string instead of
20+
# multiplying a number. Python repeats the string twice: "22" + "22" = "2222".

0 commit comments

Comments
 (0)