Program to execute a string containing Python code

In this python function program, we will execute a string containing Python code.

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
				
					mycode = 'print("Python")'
code = """
def mutiply(x,y):
    return x*y

print('sqatools')
"""
exec(mycode)
exec(code)
				
			

Output :

				
					Python
sqatools
				
			

Related Articles

create and print a list where the values are squares of numbers between 1 to 10.

access a function inside a function.

Program to create and print a list where the values are squares of numbers

In this python function program, we will create and print a list where the values are squares of numbers. To find the square of a number we need to multiply that number by itself.

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 square.
  2. Use the def keyword to define the function.
  3. Use a for loop with the range function to iterate over numbers from 1-10.
  4. After each iteration print the square of the number.
  5. To get the square of the number use** “(ex.-2**2).
  6. Print the number and its square.
  7. Call the function to get the output.
				
					def square():
    for i in range(1,11):
        print(i,":",i**2)
        
square()
				
			

Output :

				
					1 : 1
2 : 4
3 : 9
4 : 16
5 : 25
6 : 36
7 : 49
8 : 64
9 : 81
10 : 100
				
			

Related Articles

find the even numbers from a given list.

execute a string containing Python code.

Program to find the even numbers from a given list

In this python function program, we will find the even numbers from a given list.

Even number:
Even numbers are those numbers that can be divided into two equal parts.

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 even.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. list to the function.
  4. Create an empty list.
  5. Use a for loop to iterate over the numbers in the list.
  6. Use an if-else statement to check whether the number is a even number.
  7. If yes then add it to the empty list.
  8. Print the output.
  9. Create a list and pass that list to the function while calling the function.
				
					def even(list1):
    even_list = []
    for val in list1:
        if val%2 == 0:
            even_list.append(val)
    print(even_list)
            
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
even(l)
				
			

Output :

				
					[2, 4, 6, 8]
				
			

Related Articles

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

create and print a list where the values are squares of numbers between 1 to 10.

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

Python function program to find the sum of all the numbers in a list

In this python function program, we will find the sum of 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 total
  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 0.
  5. Use a for loop to iterate over the values in the list.
  6. After each iteration add the values to the variable.
  7. Print the output.
  8. Create a list and pass that list to the function while calling the function.
				
					def total(list1):
    t = 0
    for val in list1:
        t += val
    print("Sum of given list: ",t)
    
l = [6,9,4,5,3]
total(l)
				
			

Output :

				
					Sum of given list:  27
				
			

Related Articles

find the maximum of three numbers.

multiply all the numbers in a list.

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.