0% found this document useful (0 votes)
18 views3 pages

Day 30 Python For Loops

The document contains a series of Python code snippets that demonstrate various programming tasks such as printing numbers, calculating sums, generating patterns, and reversing numbers. Each task is accompanied by its corresponding output. The examples cover basic concepts like loops, conditionals, and arithmetic operations.

Uploaded by

ramu ramu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Day 30 Python For Loops

The document contains a series of Python code snippets that demonstrate various programming tasks such as printing numbers, calculating sums, generating patterns, and reversing numbers. Each task is accompanied by its corresponding output. The examples cover basic concepts like loops, conditionals, and arithmetic operations.

Uploaded by

ramu ramu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] numbers 1–100.

for x in range(1,101):
print(x,end=' ')

Output:
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

[Link] even numbers 1–50.


for x in range(1,51):
if x%2==0:
print(x,end=' ')

Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

[Link] odd numbers 50–100.


for x in range(50,101):
if x%2!=0:
print(x,end=' ')

Output:
51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

[Link] square of numbers 1–10.


for x in range(1,11):
print(x**2,end=' ')

Output:
1 4 9 16 25 36 49 64 81 100

[Link] of first n natural numbers.


n=int(input("Enter a number: "))
sum1=0
for x in range(n):
sum1+=x
print(sum1)

Output:
Enter a number: 10
55
[Link] multiplication table.
n=5
for x in range(1,11):
print(n,'x',x,'=',n*x)

Output:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

[Link] a number using while loop.


n=4567
reverse=0
while n>0:
d=n%10
reverse=reverse*10+d
n//=10
print(reverse)

Output:
7654

8. Print triangle star pattern.


n=5
for i in range(1,n+1):
for j in range(i):
print(i,end=' ')
print()

Output:
*
* *
* * *
* * * *
* * * * *

9. Print triangle number pattern


n=5
for i in range(1,n+1):
for j in range(i):
print(i,end=' ')
print()
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5

10. Print pyramid pattern of numbers.


n=5
for i in range(1,n+1):
for s in range(n-i):
print(' ',end=' ')
for j in range(2*i-1):
print('*',end=' ')
print()

Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

11. Print diamond pattern using stars.


n=3
for i in range(1,n+1):
for s in range(n-i):
print(' ',end=' ')
for j in range(2*i-1):
print('*',end=' ')
print()
for i in range(n-1,0,-1):
for s in range(n-i):
print(' ',end=' ')
for j in range(2*i-1):
print('*',end=' ')
print()

Output:
*
* * *
* * * * *
* * *
*

You might also like