Wednesday, 19 January 2022

Python Exercise on loops, Function and some common python programs

Below are the set of  multiple questions which will help to give the basic knowledge of the loops in python and how to display the different patterns using python. This section also contain some basic common python programs along with some question on Functions. 

Steps to run below programs:

  1. Install Anaconda in your PC/ Laptop.
  2. Open Spyder.
  3. Copy and paste below programs.
  4. Press Run button. You can see the result in Console section at right side.
  5. Result of all the below program is given in the last 'Result' Section.

#1. Program to calculate the sum of all numbers from 1 to given number by user.

num = int(input("Enter Number to calculate sum :"))

sum=0

for i in range (1, num+1):

    sum = sum +i

print("Sum of the all numbers :", sum)


#2. Program to count total digits in a given number

digit_num = 856938

count=0

while (digit_num != 0):

    digit_num = digit_num // 10

    count = count +1

print ("count of all digits in given number :", count)


#3. Program to show numbers from -5 to -1

print("Numbers from -5 to -1 are :")

for i in range(-5, 0, 1):

    print(i, end=' ')


print (" ")    

#4. Program of Fibonacci series upto 10 when first two numbers are 0 and 1

n1=0

n2=1

print("Fibonacci series upto 10 numbers are :")

for i in range(10):

    print (n1, end =' ')

    n3 = n1 + n2

    n1 = n2

    n2 = n3

print(" ")


#5. Program to display all prime numbers upto 2 to 20 numbers

#Note: all prime numubers are greater than 1

print ("All prime numbers upto 2 to 20 numbers are :")

for num in range(2, 21):

    if (num>1):

        for i in range(2,num):

            if (num % i) == 0:

                break

        else:

            print(num, end =' ')

print(" ")


#6. Program to reverse the given number and to check if number is palindrome

#Note: A palindrome number is a number that is same after reverse like 1221

Given_Num = 589371

Reverse_num = 0

while (Given_Num> 0):

    rem = Given_Num % 10

    Reverse_num = (Reverse_num * 10) + rem

    Given_Num = Given_Num // 10

print("Reversed Number of given num are :", Reverse_num)

if Given_Num == Reverse_num:

    print ("Number is palindrome")

else:

    print ("Number is not palindrome")


#7. Program to find any exponent value

def Exponent(base, exp):

    pow = exp

    res =1

    while (pow > 0):

        res = res*base

        pow =pow-1

    print("Exponent, base raise to the power is :", res)

Exponent(20,3)


#8. Program to find factorial of given number

number = 10

fact =1

if number<0:

    print("No Factorial for negative number")

elif number == 0:

    print("Factorial of zero is 1")

else:

    for i in range(number+1):

        fact = fact * i

    print ("The factorial of given number is :", fact)

    

#9. Program to display Multiplication table from 1 to 10

for i in range(1, 11):

    for j in range(1,11):

        print(i*j, end=' ')

    print(" ")


print(' ')

#10. Program to check if given year is leap year

def CheckLeapYear(Year):    

  if((Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0)):   

    print("Given year :", Year, " is leap Year");  

  else:  

    print ("Year :", Year, "  is not a leap Year")   

CheckLeapYear(2012)  

CheckLeapYear(2002)

CheckLeapYear(2020)


#11. Loop1 : Number pattern

print("Solution of Loop1 : Number pattern : ")

for i in range(1,6):

    for j in range(i):

        print(i, end=' ')

    print(" ")


#12. Loop2 : Right angled triangle star pattern with asterisk(*)

print("Solution of Loop2 : Right angled triangle star pattern with asterisk(*) : ")

for i in range(5,0,-1):

    for j in range(i):

        print("*", end=' ')

    print(" ") 


#13. Loop3 : different Number pattern

print("Solution of Loop3 : different Number pattern : ")

for i in range(1,6):

    for j in range(1,i+1):

        print(j, end=' ')

    print(" ")


#14. Loop4 : Half Pyramid pattern with number

print("Solution of Loop4 : Half Pyramid pattern with number : ")

for i in range(5,0,-1):

    for j in range(i,0,-1):

        print(j, end=' ')

    print(" ")


#15. Loop5: Pyramid star Pattern with asterisk(*)

print("Solution of Loop5: Pyramid star Pattern with asterisk(*) : ")

rows1 =6

rows2 =4

for i in range(1, rows1):

    for j in range(rows1-i-1):

        print(' ', end='')

    for k in range(0, 2 * i - 1):

        print('*', end = '')

    print(" ")

    

#16. Loop6: Inverted Pyramid star pattern with asterisk(*)

print("Solution of Loop6: Inverted Pyramid star Pattern with asterisk(*) : ")

rwo = 5

for i in range(rwo,0,-1):

    for j in range(rwo-i+1):

        print(' ', end='')

    for k in range(0,2*i -1):

        print('*', end = '')

    print(" ")


#17. Loop7 : Half diamond pattern with asterisk(*)

print("Solution of Loop7: Half diamond pattern with asterisk(*) : ")

for i in range(1,5):

    for j in range(i):

        print("*", end='')

    print(" ")    

for i in range(5,0,-1):

    for j in range(i):

        print("*", end='')

    print(" ")


#18. Loop8 : Mirror Half diamond pattern with asterisk(*)

print("Solution of Loop8: Mirror Half diamond pattern with asterisk(*) : ")

rows =6

for i in range(1,rows):

    for j in range(rows-1-i):

        print(' ', end='')

    for k in range(i):

        print("*", end='')

    print(" ")

for i in range(rows,0,-1):

    for j in range(rows-i+1):

        print(' ', end='')

    for l in range(i-2):

        print("*",end='')

    print(" ")


#19. Loop9 : Diamond Star pattern with asterisk(*)

print("Solution of Loop9: Diamond Star pattern with asterisk(*) : ")

rows1 =6

rows2 =4

for i in range(1, rows1):

    for j in range(rows1-i-1):

        print(' ', end='')

    for k in range(0, 2 * i - 1):

        print('*', end = '')

    print(" ")

for i in range(rows2,0,-1):

    for j in range(rows2-i+1):

        print(' ', end='')

    for k in range(0,2*i -1):

        print('*', end = '')

    print(" ")

  

#20. Loop10 : Rectangle Star pattern 

print("Solution of Loop10: Rectangle Star pattern : ")  

for i in range(5):

    for j in range(12):

        print("*", end=' ')

    print(" ")

    

#21. Loop11 : Square Star pattern 

print("Solution of Loop11: Square Star pattern : ")  

for i in range(5):

    for j in range(5):

        print("*", end=' ')

    print(" ")


#22. Loop12 : Rhombus Star pattern

print("Solution of Loop12: Rhombus Star pattern : ")  

for i in range(rows, 0, -1):

    for j in range(1, i):

        print(' ', end = '')

    for k in range(0, rows):

        print('*', end = '')

    print(" ")


#23. Loop13: Triangle Number pattern

print("Solution of Loop13:  Triangle Number pattern : ") 

for i in range(1, 6):

    for j in range(6-i-1):

        print(' ', end='')

    for k in range(0, 2 * i - 1):

        print(i, end = '')

    print(" ")


#24. Create function 'Employee' with two argument, Emp_name and salary

#Declaration of function

def Employee(Emp_name, salary):

    print("Function Declaration :", Emp_name, salary)

#Calling the function

Employee("Akansha",30000)


#25. Create function Employee calling multiple arguments at a time using one function

def Employee(*args):

    for i in args:

        print("Calling multiple arguments :", i)

Employee("Akansha","Isha")

Employee("Isha", "Akansha", "Sakshi")


#26. Create a function 'Employee' with salary as default argument and it can be changed also after calling the function

def Employee(Emp_name, salary=25000):

    print(Emp_name, salary)

Employee("Akansha")

Employee("Isha", 20000)


#27. Change the name of function 'Employee' to 'Employee_Details

def Employee(Emp_name, salary):

    print(Emp_name, salary)

#Assigning different function name

Employee_Details = Employee

Employee("Akansha",30000)


#28. Find the Factorial  of given number using recursion function

def Factorial_Recursion(num):

    if num == 1:

        return num

    else:

        return num*Factorial_Recursion(num-1)

Given_num = int(input("Enter factorial number :"))

if Given_num < 0:

    print("No factorial for negative integers")

elif Given_num == 0:

    print("Factorial of zero is 1")

else:

    print(("Factorial of given number is :", Factorial_Recursion(Given_num)))

    

Result of above Exercise

Enter Number to calculate sum :5

Sum of the all numbers : 15

count of all digits in given number : 6

Numbers from -5 to -1 are :

-5 -4 -3 -2 -1  

Fibonacci series upto 10 numbers are :

0 1 1 2 3 5 8 13 21 34  

All prime numbers upto 2 to 20 numbers are :

2 3 5 7 11 13 17 19  

Reversed Number of given num are : 173985

Number is not palindrome

Exponent, base raise to the power is : 8000

The factorial of given number is : 0

1 2 3 4 5 6 7 8 9 10  

2 4 6 8 10 12 14 16 18 20  

3 6 9 12 15 18 21 24 27 30  

4 8 12 16 20 24 28 32 36 40  

5 10 15 20 25 30 35 40 45 50  

6 12 18 24 30 36 42 48 54 60  

7 14 21 28 35 42 49 56 63 70  

8 16 24 32 40 48 56 64 72 80  

9 18 27 36 45 54 63 72 81 90  

10 20 30 40 50 60 70 80 90 100  

 

Given year : 2012  is leap Year

Year : 2002   is not a leap Year

Given year : 2020  is leap Year

Solution of Loop1 : Number pattern : 

1  

2 2  

3 3 3  

4 4 4 4  

5 5 5 5 5  

Solution of Loop2 : Right angled triangle star pattern with asterisk(*) : 

* * * * *  

* * * *  

* * *  

* *  

*  

Solution of Loop3 : different Number pattern : 

1  

1 2  

1 2 3  

1 2 3 4  

1 2 3 4 5  

Solution of Loop4 : Half Pyramid pattern with number : 

5 4 3 2 1  

4 3 2 1  

3 2 1  

2 1  

1  

Solution of Loop5: Pyramid star Pattern with asterisk(*) : 

    * 

   *** 

  ***** 

 ******* 

********* 

Solution of Loop6: Inverted Pyramid star Pattern with asterisk(*) : 

 ********* 

  ******* 

   ***** 

    *** 

     * 

Solution of Loop7: Half diamond pattern with asterisk(*) : 

** 

*** 

**** 

***** 

**** 

*** 

** 

Solution of Loop8: Mirror Half diamond pattern with asterisk(*) : 

    * 

   ** 

  *** 

 **** 

***** 

 **** 

  *** 

   ** 

    *       

Solution of Loop9: Diamond Star pattern with asterisk(*) : 

    * 

   *** 

  ***** 

 ******* 

********* 

 ******* 

  ***** 

   *** 

    * 

Solution of Loop10: Rectangle Star pattern : 

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

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

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

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

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

Solution of Loop11: Square Star pattern : 

* * * * *  

* * * * *  

* * * * *  

* * * * *  

* * * * *  

Solution of Loop12: Rhombus Star pattern : 

     ****** 

    ****** 

   ****** 

  ****** 

 ****** 

****** 

Solution of Loop13:  Triangle Number pattern : 

    1 

   222 

  33333 

 4444444 

555555555 

Function Declaration : Akansha 30000

Calling multiple arguments : Akansha

Calling multiple arguments : Isha

Calling multiple arguments : Isha

Calling multiple arguments : Akansha

Calling multiple arguments : Sakshi


Akansha 25000

Isha 20000

Akansha 30000


Enter factorial number :5

('Factorial of given number is :', 120)

No comments:

Post a Comment