Program to calculate the sum of numbers from 0 to 10

In this python function program, we will calculate the sum of numbers from 0 to 10.

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. Create a variable and assign its value equal to 0.
  4. Use a for loop with the range function to iterate over numbers from 1-10.
  5. After each iteration add the numbers to the variable.
  6. Print the sum of numbers between 1-10.
  7. Call the function to get the output.
				
					def total():
    t = 0
    for i in range(1,11):
        t += i
    print("Total: ",t)
    
total()
				
			

Output :

				
					Total:  55
				
			

Related Articles

find the LCM of two numbers.

find the HCF of two numbers.

Python function program to find the LCM of two numbers

In this python function program, we will find the LCM of two numbers.

LCM:
LCM stands for ‘Least Common Multiple’ or the Lowest Common Multiple. The least common multiple (LCM) of two numbers is the lowest possible number that can be divisible by both 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 lcm
  2. Use the def keyword to define the function.
  3. Pass two parameter i.e. numbers to the function.
  4. Using an if-else statement check which number is greater.
  5. Use While loop.
  6. Inside the While loop check whether both numbers divide the greater number or not.
  7. If yes then break the loop otherwise, add 1 to the greater number and let the loop continue.
  8. At the end print the output.
  9. Take two numbers as input through the user and pass them to the function while calling the function.
				
					def lcm(num1,num2):
    if num1 > num2:
        greater = num1
    else:
        greater = num2

    while(True):
        if((greater % num1 == 0) and (greater % num2 == 0)):
            lcm = greater
            break
        greater += 1

    print(f"L.C.M of {num1} and {num2}: {lcm}")

num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))   
lcm(num1,num2)
				
			

Output :

				
					Enter number 1: 12
Enter number 2: 20
L.C.M of 12 and 20: 60
				
			

Related Articles

access a function inside a function.

calculate the sum of numbers from 0 to 10.

Python function program to access a function inside a function

In this python function program, we wil access a function inside a function.

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
				
					def test(a):
        def add(b):
                nonlocal a
                a += 0
                return a+b
        return add
func= test(10)
print(func(10))
				
			

Output :

				
					20
				
			

Related Articles

execute a string containing Python code.

find the LCM of two numbers.

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.