-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_Loops_If_Else.py
More file actions
157 lines (92 loc) · 2.24 KB
/
04_Loops_If_Else.py
File metadata and controls
157 lines (92 loc) · 2.24 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
#!/usr/bin/env python
# coding: utf-8
# 1 : Take a variable x and print "Even" if the number is divisible by 2, otherwise print "Odd".
# In[105]:
x= 9
# check if number is divisible by 2
if(x%2==0):
print("Even")
else:
print("Odd")
# 2 : Take a variable y and print "Grade A" if y is greater than 90, "Grade B" if y is greater than 60 but less than or equal to 90 and "Grade F" Otherwise.
# In[9]:
y=90
if(y>90):
print("Grade A")
elif(y>60):
print("Grade B")
else:
print("Grade F")
# In[101]:
list = [10, 20, 30, 40, 50]
# reverse list
new_list = reversed(list)
# iterate reversed list
for item in new_list:
print(item, end=" ")
# In[53]:
n = 5
k = 5
for i in range(0,n+1):
for j in range(k-i, 0, -1):
print(j,end=' ')
print()
# In[18]:
numbers = [12, 75, 150, 180, 145, 525, 50]
# iterate each item of a list
for item in numbers:
if item > 500:
break
elif item > 150:
continue
# check if number is divisible by 5
elif item % 5 == 0:
print(item)
# In[55]:
# first two numbers
num1 = 0
num2 = 1
print("Fibonacci sequence:")
# run loop 10 times
for i in range(10):
# print next number of a series
print(num1, end=" ")
# add last two numbers to get next number
res = num1 + num2
# update values
num1 = num2
num2 = res
# In[46]:
num = 6
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
# run loop 5 times
for i in range(1, num + 1):
# multiply factorial by current number
factorial = factorial * i
print("The factorial of", num, "is", factorial)
# In[70]:
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
for item in my_list:
if item%20 == 0:
print(item, end=" ")
# In[75]:
input_number = 6
cube = 1
for i in range(cube, input_number+1):
cube = i*i*i
print('Current number is :', i, 'and the cube is', cube)
# In[91]:
rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
for i in range(rows, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print("\r")