-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.2.ConditionsAndLoops.py
More file actions
173 lines (122 loc) · 4.21 KB
/
3.2.ConditionsAndLoops.py
File metadata and controls
173 lines (122 loc) · 4.21 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# if
if 1 == 1:
print("that's true")
def num_check(number):
if number == 10:
print("number is 10")
else:
print("number is not 10")
num_check(12)
def num_check(number):
if number > 10:
print("number is greater than 10")
elif number < 10:
print("number is less than 10")
else:
print("number is equal to 10")
num_check(23)
# for
students = ["John", "Mark", "Venessa", "Mariam"]
for student in students:
print(student)
salaries = [1000, 2000, 3000, 4000]
for salary in salaries:
print(int(salary * 20 / 100 + salary))
def new_salary(salary, rate):
return int(salary * rate / 100 + salary)
print(new_salary(1500, 10))
for salary in salaries:
print(new_salary(salary, 10))
for salary in salaries:
if salary >= 3000:
print("If salary is equal to 3000 or greater than 3000", new_salary(salary, 10))
else:
print("If salary is less than 3000", new_salary(salary, 20))
# #########################################################################################################################################################
# Ex.: "Alternating"
# Input: "hi my name is john and i am learning python"
# Output: "Hi mY NaMe iS JoHn aNd i aM LearNiNg pYtHoN"
# Tip: Even index is capital letter and odd index is lower case letter.
def alternating(string):
new_string = ""
for i in range(len(string)):
if i % 2 == 0:
new_string += string[i].upper()
else:
new_string += string[i].lower()
print(new_string)
alternating("hi my name is john and i am learning python")
# ############################################################################################################################################################
# BREAK:Skip others if break cond. is true, CONTINUE:Skip just cond. value if cond. is true,
# WHILE: Continue to work until cond. is true
salaries = [100, 200, 300, 400]
for salary in salaries:
if salary == 300:
break
print(salary)
for salary in salaries:
if salary == 300:
continue
print(salary)
number = 1
while number < 5:
print(number)
number += 1
# Enumerate
students = ["John", "Mark", "Venessa", "Mariam"]
for i, student in enumerate(students, 1):
print(i, student)
A = []
B = []
for i, student in enumerate(students):
if i % 2 == 0:
A.append(student)
else:
B.append(student)
print(A, B)
# ######################################################################################################################################################
# Ex.: Define a divide_students function.
# 1st List students who placed in even index and 2nd List students who placed in even index.
# Return a list that combined 2 list.
students = ["John", "Mark", "Venessa", "Mariam"]
def divide_students(students):
groups = [[], []]
for i, student in enumerate(students):
if i % 2 == 0:
groups[0].append(student)
else:
groups[1].append(student)
print(groups)
return groups
divide_students(students)
# ############################################################################################################################################################
# Ex.: Alternating Func. with Enumerate
def alternating_with_enumerate(string):
new_string = ""
for i, letter in enumerate(string):
if i % 2 == 0:
new_string += letter.upper()
else:
new_string += letter.lower()
print(new_string)
alternating_with_enumerate("hi my name is john and i am learning python")
# Zip
students = ["John", "Mark", "Venessa"]
departments = ["engineering", "statistics", "astronomy"]
ages = [21, 19, 20]
print(list(zip(students, departments, ages)))
# Lambda, Map, Filter, Reduce
x = lambda a, b: a + b
print(x(4, 5))
salaries = [100, 200, 300, 400, 500]
def new_salary(x):
return x * 20 / 100 + x
for salary in salaries:
print(new_salary(salary))
print(list(map(new_salary, salaries)))
print(list(map(lambda x: x * 20 / 100 + x, salaries)))
list_store = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list(filter(lambda x: x % 2 == 0, list_store)))
from functools import reduce
list_store = [1, 2, 3, 4]
print(reduce(lambda a, b: a + b, list_store))