-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForElse.py
More file actions
34 lines (28 loc) · 760 Bytes
/
ForElse.py
File metadata and controls
34 lines (28 loc) · 760 Bytes
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
#1. Basic using of for loop
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
print(fruit.capitalize())
# Output: Apple
# Banana
# Mango
#2.For with a flag
m = int(input('Give n '))
def operation(n):
for n in range(2, m):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n/x)
break
print(operation(m))
#3. To look for prime numbers
m = int(input('Give m '))
def prime(n):
for n in range(2, m):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n/x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
print(prime(m))