Sample programs: Functions basics

Simple programs - solution for practice:

 Copy and try on your own.
1) First 10 prime numbers program in python
    n = 10
    print("First ten prime no program in Python")
    for i in range(1,n+1):
        for j in range(2,n+1):
            if i % j == 0:
        if count == 1:

First 10 prime number program in Python
Output of prime number program
2) Write a program to set any two numbers to calculate the sum, difference, product, and division. Print the result.

Num1=int(input("Enter 1st number:"))
Num2=int(input("Enter 2nd number:"))

#Addition logic
Num3 = Num1+Num2
print("Sum value:",Num3)

#Subtraction logic
Num4 = Num1 - Num2
print("Difference value:",Num4)

#Product/multiplication logic
Num5 = Num1 * Num2
print("Product value:",Num5)

#Single division logic
Num6 = Num1 / Num2
print("Single divison value:",Num6)

#Double division logic
Num7 = Num1 // Num2
print("Double division value:",Num7)

#Modulus logic
Num8 = Num1 % Num2

print("Modulus division value:",Num8)

Arithmetic operations
Output of Arithmetic operations

3) Write a Python program to read a list of integers and print only the multiples of 8. 


#Logic to read a list of integers and print only the multiples of 8. 
print("Multiples of 8 are:")
for i in list_of_integers:
    if (i % 8) == 0:
        print(i, end=',')
Multiples of 8
Output - Multiples of 8
4) Write a function in python to find the greater of 2 numbers.

    def greater(a,b):
        if a > b:
            print(a,"is greater than ",b)
            print(b,"is greater than ",a)
    print("To find greater of 2 number")    
    num1=int(input("Enter 1st number:"))
    num2=int(input("Enter 2nd number:"))


Greater of 2 numbers
Output - Greater of  2 numbers

Simple programs to practice:

1) Write a Python function Factorial(n) which returns the factorial of the given number(n).

2) Write a Python function, Square(num) which returns the square of the given number(num).

3) Write a Python function, Sum(n) which returns the sum of first n numbers.

4) Write a Python function, Palindrome(num) that accepts an integer num as input and returns true/false based on condition.

    Ex: num = 12321 ---> output = 12321 ----> then given number is palindrome.

5) Write a Python function, check_strong_number(num) that accepts a positive as input and return true/false based on condition.

    A number is said to be Strong number if the sum of the factorial of each digit is equal to the given number.

   Ex: 145 is strong number as (1! =1) + (4! = 24) + (5! = 120) = 145.

Previous post:
  • Collections
          Click here -----> Lesson 4: Collections
  • Functions
          Click here -----> Lesson 3: Functions in python
  • Control structures
          Click here -----> Lesson 2: Control structures
  • Basics of python
          Click here -----> Lesson 1: Basics of python
  • About: Introduction to python course
          Click here -----> About: Introduction to python course

No comments:

Post a comment