This repository was archived by the owner on Oct 20, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy path07_04_BonusExaples.py
More file actions
116 lines (82 loc) · 2.42 KB
/
Copy path07_04_BonusExaples.py
File metadata and controls
116 lines (82 loc) · 2.42 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
filenames = ["1.doc","1.report","1.presentation"]
filenames = [filename.replace(".","-") + ".txt" for filename in filenames]
print(filenames)
Quiz:
new = []
for i in [1, 2, 3]:
new.append(i + 10)
print(new)
new = [i for i in ['a', 'b', 'c']]
print(new)
# Coding Exercises:
# #1, two solutions
# Harder
names = ["john smith", "jay santi", "eva kuki"]
for index, name in enumerate(names):
names[index] = name.title()
print(names)
# Easier
names = ["john smith", "jay santi", "eva kuki"]
names = [name.title() for name in names]
print(names)
# Coding Exercise 2
#
# usernames = ["john 1990", "alberta1970", "magnola2000"]
#
# Extend the code above so the code prints out a list containing the number of characters for each username.
#
# The output of your code should be as below:
# 9, 11, 11
usernames = ["john 1990", "alberta1970", "magnola2000"]
lengths = [name.__len__() for name in usernames]
print(lengths)
# Coding Exercise 3
#
# user_entries = ['10', '19.1', '20']
#
# Extend the code above so the code prints out a list containing the same items as floats.
#
# The output of your code should be as below:
#
# [10.0, 19.1, 20.0]
user_entries = ['10', '19.1', '20']
floats = [float(entry) for entry in user_entries]
print(floats)
# Coding Exercise 4
#
# user_entries = ['10', '19.1', '20']
#
# Extend the code above so the code prints out the sum of the numbers.
#
# The output of your code should be as below:
#
# 49.1
#
# Hint: Use the sum() function.
# The function gets a list of numbers as input and produces the sum of all numbers. For more info, try help(sum).
sum(floats)
# Lol, that was 2 e-z
#
# Bug-Fixing Exercise 1
#
# The code below tries to write the items of temperatures each in one line in the file.txt list.
# However, the code has an error. Try to fix the error.
temperatures = [10, 12, 14]
file = open("file.txt", 'w')
file.writelines(temperatures)
# Solution:
temperatures = [10, 12, 14]
temperatures = [str(i) + '\n' for i in temperatures]
file = open("file.txt", 'w')
file.writelines(temperatures)
# Bug-Fixing Exercise 2
#
# The code below tries to convert all the numbers to integers. However, the code has an error. Try to fix the error.
#
# numbers = [10.1, 12.3, 14.7]
# numbers = [int(number) for item in numbers]
# print(numbers)
# Solution:
numbers = [10.1, 12.3, 14.7]
numbers = [int(number) for number in numbers] # can't define it as both number and item
print(numbers)