Skip to main content

Python : Loop (CWH)



 #Program to print the content of a list using while loop


name=["Animesh","Vikas","Rohit","Karan"]
i=0
while i<len(name):
    print(name[i])
    i=i+1


#Program to print the content of a list using for loop

name=["Animesh","Vikas","Rohit","Karan"]
for naam in name:
    print(naam)


#Program to print 10 digit/number using for loop and range funtion having syntax
range(start,stop,step_size)


for i in range (10):
    print(i)

Output: (tab=line change)

0    1    2    3    4    5    6    7    8    9    10


#Program to print 10 number from 1-10 using for loop and range function

for i in range (1,11):
    print(i)

Output: (tab=line change)

    1    2    3    4    5    6    7    8    9    10


#Program to print multiplication table of a given number using for loop

a=int(input("Enter Number"))
for i in range(1,11):
    print(str(a)+"x"+ str(i)+" = "+ str(i*a))
             #OR
    print(a,"x",i," = ",i*a)
#Can also be done using F -string function(Separate Post)



#Program to greet all the person names stored in a list L1 and which starts with "S"
    L1=["Harry","Saham","Sachin","Rahul"]

L1=["Harry","Saham","Sachin","Rahul"]
for i in L1:
    if i[0]=="S":
        print(f"Hello {i}")

        #OR
for name in L1:
    if name.startswith("S"):
        print(f"Hello {name}")


#Program to print multiplication table of a given number using while loop

a=int(input("Enter Number"))
i=1
while i<11:
    print(f"{a}x{i} = {a*i}")
    i=i+1


#Program to find the sum of first n natural number using for loop

n=int(input("Enter Number"))
s=0
for i in range(1,n+1):
    s=s+i
print(f"{s}")


#Program to calculate the factorial of a given number using for loop

n=int(input("Enter Number"))
s=1
for i in range(1,n+1):
    s=s*i
print(f"{s}")


#Program to to print the following star pattern [for n=3]
                        *
                  *    *    *
            *    *    *    *    *

n=int(input("Enter n : "))
for i in range(n):
    print(" " * (n-i-1), end="")
    print("*" * (2*i+1), end="")
    print(" " * (n-i-1))    #do not use end="" here to change line

    #in default print give line,to solve this issue we use end=""



#Program to to print the following star pattern [for n=3]
            *
            *    *
            *    *    *
  
n=int(input("Enter n : "))
for i in range(n):
    print("*" * (i+1))


#Program to to print the following star pattern [for n=3]
    *    *    *
    *          *
    *    *    *

n=int(input("Enter n : "))
for i in range(1,n+1):
    for j in range(1,n+1):
        if(i==1 or i==n or j==1 or j==n):
            print("* ",end="")
        else:
            print("  ",end="")
    print()


#Program to to print multiplication table of n using for loop in reverse order

n=int(input("Enter n : "))
for i in range(10,0,-1):
    print(f"{n}x{i} = {n*i}")










Popular posts from this blog

C++ : Calculator

  # include < iostream > using namespace std ; int main () {     int a , b ;     char o ;     float c ;     cout << " Enter Value of A \n " ;     cin >> a ;     cout << " Enter Value of B \n " ;     cin >> b ;     cout << " Enter Operation \n " << " + for Addition \n - for substration \n * for multiplication \n / for division \n " ;     cin >> o ;     switch ( o )     {         case ' + ' :       {            c = a + b ;             break ;       }       case ' - ' :       {           c = a - b ;           break ;       }       case ' / ' :       {         ...

C++ : Pointer Program

# include < iostream > using namespace std ; int main () {     int a = 4 ;     int * b =& a ;     // & is address of operator     cout << " Thr address of a is " << & a << endl ;     cout << " Thr address of a is " << b << endl ;     // * is value at dereference operator     cout << " Thr value of a is " << * b << endl ;           // Pointer to pointer     int ** c =& b ; cout << " The adress of b is " << & b << endl ;     cout << " The adress of b is " << c << endl ;     cout << " The value at address c is " << * c << endl ;     cout << " The value at address value_at (value_at( c )) is " << ** c << endl ;     } OUTPUT =>           Thr address of...

Python : Small Projects (CWH)

  # Project 1 : Snake, Water, Game OR Rock,Paper,Scissor import random def game ( comp , you ) :     if comp == you :         return 2     elif comp == " s " :         if you == " w " :             return 0         elif you == " g " :             return 1     elif comp == " w " :         if you == " g " :             return 0         elif you == " s " :             return 1     elif comp == " g " :         if you == " w " :             return 0         elif you == " s " :             return 1 print ( " Computer turn : Snake(s),Water(w) or Gun(g) " ) randno = random . randint ( 1 , 3 ) if randno == 1 :  ...