technical skills grow

Responsive Ads Here

Friday, July 30, 2021

2.Python Assignment : FOR LOOP

 

Assignment: Program to print the table of the given number .

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = 5
for i in list:
c = i * 5
print(c)
OUTPUT
5
10
15
20
25
30
35
40
45
50

Assignment: Iterating string using for loop

str= "Python"

for i in str:
print("PRINT LINE ",i)

Assignment: Program to print the sum of the given list.

 

list = [10 , 30 , 23 , 43 , 65 , 12]
sum = 0
for i in list:
sum=sum+i
print("Total no of list :-",sum)

OUTPUT: Total no of list :- 183


For loop Using range() function

Syntax:   range(start,stop,step size)    

Example-1: Program to print numbers in sequence 

print("Print the data in Vartical")
for i in range(10):
print(i,end =' ')
OUTPUT:
0 1 2 3 4 5 6 7 8 9

 Ex-2: Program to print even number using step size in range().

 n= int(input("Enter the no :"))
for i in range (2,n,2):
print(i,end=' ')
Output:
Enter
the
no: 20
2 4 6 8 10 12 14 16 18

Ex 3: Program to print table of given number.


n = int(input("Enter the no :"))

for i in range(1,11):
sum=n*i
print(sum,end =' ')
Enter the no :16
16 32 48 64 80 96 112 128 144 160

Ex 4: Program to print table of given number.

n = int(input("Enter the no :"))

for i in range(1,11):
c=n*i
print(n,'*',i,'=', c)

OUTPUT
Enter the no :10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
 Assignment 1: 


*  * 
*  *  * 
*  *  *  * 
*  *  *  *  * 

no = int(input("Enter the no :-"))
for i in range(no):
for j in range(i+1):
print("*",end=' ')

print

Assignment 2:

* * * * *
* * * *
* * *
* *
*

no = int(input("Enter the no :-"))
for i in range(no):
for j in range(no -i):
print("*",end=' ')
print()

Assignment 3:

     * 
    *  * 
   *  *  * 
  *  *  *  * 
 *  *  *  *  *

no = 5
for i in range(no):
for space in range(no-i ):
print(end=' ')
for j in range(i+1):
print("*", end=' ')
print( )
Assignment 4: Print ODD STAR 
         * 
* * *
* * * * *
* * * * * * *
* * * * * * * * *
n = 10
s = 0
for i in range( n):
if i%2 !=0:
for space in range(n-i):
print(end=' ')
for j in range(i):
print("*",end=' ')
print()
Assignment 4:  STAR 
* * * * * 
* * * *
* * *
* *
*
n=5
for i in range(n):
for space in range(i):
print(end= ' ')
for j in range(n-i):

print("*",end=' ')
print()

No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts