-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_loops.py
More file actions
83 lines (68 loc) · 2.45 KB
/
Copy pathfor_loops.py
File metadata and controls
83 lines (68 loc) · 2.45 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
# A for loop is used to iterate through a sequence like list, tuples or string [iterables] ..
""" Syntax:
l = [1,4,7]
for item in l:
print(item) # print 1,4,7
"""
# print tuples using for loops ..
t = (6,231,75,122)
for i in t:
print(i)
# print list using for loop ..
l = [1,4,6,234,6,764]
for i in l:
print(i)
# print string using for loop ..
s = "sumit"
for i in s:
print(i)
# write a program to print multiplication table of a given number using for loop ..
n = int(input("Enter the number: "))
for i in range(1,11):
print(f"{n} x {i} = {n*i}")
# write a program to calculate the factorial of a given number using for loop ..
n = int(input("Enter the number: "))
product = 1
for i in range (1,n+1):
product = product*1
print(f"The factorial of {n} is {product}")
# Write a program to find whether a given number is prime or not ..
n = int(input("Enter the number: "))
for i in range (2,n):
if (n%i) == 0:
print("Number is not prime")
break
else:
print("number is prime")
# write a program to print multiplication table of a n using for loop in reversed order ..
n = int(input("Enter the number: "))
for i in range (1,11):
print(f"{n} x {11-i} = {n*(11-i)}")
""" For Loop with ELSE:
An optional else can be used with a for loop if the code is to be executed when the loop exhausts ..
Syntax:
l = [create anything..]
for condition(s):
print()
else:
print() # this is printed when the loop exhausted!
"""
# write a program for loop with else ..
l = [1,7,8]
for item in l:
print(item)
else:
print("done") # this is printed when the loop exhausted!
""" Range function in python:
The range() function in python is used to generate a sequence of number, we can also specify the start, stop and step - size as follows:
range (start, stop, steps_size)
# step_size is usually not used with range()
"""
# An example: Demonstrating range() function ..
for i in range(0,7): # range (7) can also be used ..
print(i) # print 0 to 6 ..
# example of step - size ..
for i in range (0,100,4): # 4 ka jum hoga 0 to 100 tak me jitna bhi output return ho ..
print(i)
# output: 0 4 8 12 etc..
print("code will ending ..")