Program that take a number as a parameter and checks whether the number is prime or not

In this python function program, we will take a number as a parameter and checks whether the number is prime or not.

Prime Number: A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function prime
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a count variable and assign its value equal to 0.
  5. Use for loop with the range function to check whether a number is a prime number or not.
  6. If a number divides the input number then add 1 to the count variable.
  7. If a number is divisible by 1 and by itself then it is a prime number.
  8. Print the output.
  9. Take a number as input through the user and pass it to the function while calling the function.

				
					def prime(num):
    count = 0

    for i in range(2,num):
        if num%i == 0:
            count += 1

    if count > 0:
        print("It is not a prime number")
    else:
        print("It is a prime number")
        
num1 =  int(input("Enter a number: "))
prime(num1)
				
			

Output :

				
					Enter a number: 7
It is a prime number
				
			

Related Articles

program that takes a list and returns a new list with unique elements of the first list.

find the even numbers from a given list.

Program to take a list and returns a new list with unique elements of the first list

In this python function program, we will take a list and returns a new list with unique elements of the first list.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function unique
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. list to the function.
  4. Convert the list to set using set() to remove the duplicate values.
  5. Now convert it back to the list using list().
    Print the output.
  6. Create a list and pass that list to the function while calling the function.
				
					def unique(list1):
    print(list(set(list1)))
    
l = [2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
unique(l)
				
			

Output :

				
					[1, 2, 3, 4, 6]
				
			

Related Articles

check whether a number is in a given range.

take a number as a parameter and checks whether the number is prime or not.

Program to check whether a number is in a given range

In this python function program, we will check whether a number is in a given range.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function check
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Check whether the number lies between 1-20 using an if-else statement.
  5. If the number lies between 1-20 then print True else print False.
  6. Take a number through the user as input and pass it to the function while calling the function.
				
					def check(num):
    if num in range(2,20):
        print("True")
    else:
        print("False")    
num1 = int(input("Enter a number: "))
check(num1)
				
			

Output :

				
					Enter a number: 7
True
				
			

Related Articles

reverse a string.

program that takes a list and returns a new list with unique elements of the first list.

Python function program to reverse a string

In this python function program, we will reverse a string.

String: A string is any series of characters that are interpreted literally by a script. For example, “Python”.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function rev
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. a string to the function.
  4. Reverse the string using indexing.
  5. Print the output.
  6. Take a string as input through the user and pass it to the function while calling the function.
				
					def rev(str1):
    a = str1[::-1]
    print("Reverse of the given string: ",a)
    
string = input("Enter a string: ")
rev(string)
				
			

Output :

				
					Enter a string: Python1234
Reverse of the given string:  4321nohtyP
				
			

Related Articles

multiply all the numbers in a list.

check whether a number is in a given range.

Program to multiply all the numbers in a list

In this python function program, we will multiply all the numbers in a list.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function mul.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. list to the function.
  4. Create a variable and assign its value equal to 1.
  5. Use a for loop to iterate over the values in the list.
  6. After each iteration multiply the variable by the value.
  7. Print the output.
  8. Create a list and pass that list to the function while calling the function.
				
					def mul(list1):
    t = 1
    for val in list1:
        t *= val
    print("Product of elements in the given list: ",t)
    
l = [-8, 6, 1, 9, 2]
mul(l)
				
			

Output :

				
					Product of elements in the given list:  -864
				
			

Related Articles

find the sum of all the numbers in a list.

reverse a string

Program to find the maximum of three numbers.

In this python function program, we will find the maximum of three numbers.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function largest
  2. Use the def keyword to define the function.
  3. Pass three parameters i.e. numbers to the function.
  4. Take 3 numbers as input through the user and pass them to the function while calling the function.
  5. Find the largest number among three numbers using logic and if-else statements.
				
					def largest(a,b,c):
    if a>b:
        if a>c:
            print(f"{a} is the greatest number")
    elif b>a:
        if b>c:
            print(f"{b} is the greatest number")
    else:
        print(f"{c} is the greatest number")
        
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
num3 = int(input("Enter number 3: "))

largest(num1,num2,num3)
				
			

Output :

				
					Enter number 1: 50
Enter number 2: 60
Enter number 3: 44
60 is the greatest number
				
			

Related Articles

print a table of a given number.

find the sum of all the numbers in a list.

Python function program to print a table of a number

In this python function program, we will create a function to print a table of a number.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function table.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. a number to the function.
  4. Take the number (parameter) as input through the user and pass it to the function.
  5. Create a variable and assign its value equal to 0.
  6. Use a for loop to iterate over 1-10.
  7. Multiply the given number with the loop number and store the result in the created variable.
  8. Print the result after each iteration.
  9. Pass the number to the function while calling the function.
				
					def table(num):
    a = 0
    for i in range(1,11):
        a = i*num
        print(i,"*",num,"=",a)
        
n = int(input("Enter a number: "))

table(n)
				
			

Output :

				
					Enter a number: 5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
				
			

Related Articles

print the input string 10 times.

find the maximum of three numbers.

Python function program to print the string 10 times

In this python function program, we will create a function to print the string 10 times.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function String.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. string to the function.
  4. Take a string as input through the user and use this string as a parameter that has passed to the function.
  5. Print the string 10 times by multiplying it by 10.
  6. Pass the string to the function while calling the function.
				
					def String(str1):
    print(str1*10)
    
string = input("Enter a string: ")

String(string)
				
			

Output :

				
					Enter a string: Python
PythonPythonPythonPythonPythonPythonPythonPythonPythonPython
				
			

Related Articles

add two numbers.

print a table of a given number.

Python Functional, Programs And Excercises

A Python function is a sequence of statements that executes a specific task. A function can take arguments, which are the information passed to the function when it is called. Functions can also return values, which are the result of executing the function.

1). Python function program to add two numbers.

2). Python function program to print the input string 10 times.

3). Python function program to print a table of a given number.

4). Python function program to find the maximum of three numbers.

Input: 17, 21, -9
Output: 21

5). Python function program to find the sum of all the numbers in a list.
Input : [6,9,4,5,3]
Output: 27

6). Python function program to multiply all the numbers in a list.
Input : [-8, 6, 1, 9, 2]
Output: -864

7). Python function program to reverse a string.
Input: Python1234
Output: 4321nohtyp

8). Python function program to check whether a number is in a given range.
Input : num = 7, range = 2 to 20
Output: 7 is in the range

9). Python function program that takes a list and returns a new list with unique elements of the first list.
Input : [2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
Output : [2, 3, 1, 4, 6 ]

10). Python function program that take a number as a parameter and checks whether the number is prime or not.
Input : 7
Output : True

11). Python function program to find the even numbers from a given list.
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output : [2, 4, 6, 8]

12). Python function program to create and print a list where the values are squares of numbers between 1 to 10.
Input: 1 to 10
Output: 1, 4, 9, 16, 25, 36, 49, 64, 81

13). Python function program to execute a string containing Python code.

14). Python function program to access a function inside a function.

15). Python function program to find the LCM of two numbers.
Input: 12, 20
Output: 60

16). Python function program to calculate the sum of numbers from 0 to 10.
Output: 55

17). Python function program to find the HCF of two numbers.
Input: 24 , 54
Output: 6

18). Python function program to create a function with *args as parameters.
Input: 5, 6, 8, 7
Output: 125 216 512 343

19). Python function program to get the factorial of a given number.
Input: 5
Output: 120

20). Python function program to get the Fibonacci series up to the given number.
Input: 10
Output: 1 1 2 3 5 8 13 21 34

21). Python function program to check whether a combination of two numbers has a sum of 10 from the given list.
Input : [2, 5, 6, 4, 7, 3, 8, 9, 1]
Output : True

1, 22). Python function program to get unique values from the given list.
Input : [4, 6, 1, 7, 6, 1, 5]
Output : [4, 6, 1, 7, 5]

23). Python function program to get the duplicate characters from the string.
Input: Programming
Output: {‘g’,’m’,’r’}

24). Python function program to get the square of all values in the given dictionary.
Input = {‘a’: 4, ‘b’ :3, ‘c’ : 12, ‘d’: 6}
Output = {‘a’: 16, ‘b’ : 9, ‘c’: 144, ‘d’, 36}

25). Python function program to create dictionary output from the given string.
Note: Combination of the first and last character from each word should be
key and the same word will the value in the dictionary.
Input = “Python is easy to Learn”
Output = {‘Pn’: ‘Python’, ‘is’: ‘is’, ‘ey’: ‘easy’, ‘to’: ‘to’, ‘Ln’: ‘Learn’}

26). Python function program to print a list of prime numbers from 1 to 100.

27). Python function program to get a list of odd numbers from 1 to 100.

28). Python function program to print and accept login credentials.

29). Python function program to get the addition with the return statement.

30). Python function program to create a Fruitshop Management system.

31). Python function program to check whether the given year is a leap year.

32). Python function program to reverse an integer.

33). Python function program to create a library management system.

34). Python function program to add two Binary numbers.

35). Python function program to search words in a string.

36). Python function program to get the length of the last word in a string.

37). Python function program to get a valid mobile number.

38). Python function program to convert an integer to its word format.

39). Python function program to get all permutations from a string.

40). Python function program to create a function with **kwargs as parameters.

41). Python function program to create a function local and global variable.