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 function program to add two numbers

In this python function program, we will create a function to add two numbers.

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

Steps to solve the program
  1. Create a function add.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. numbers to the function.
  4. Add the numbers passed to the function using the” operator.
  5. Print the output.
  6. Pass the numbers to the function while calling the function.
				
					def add(a,b):
    total = a+b
    print("Total: ",total)
    
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

add(num1,num2)
				
			

Output :

				
					Enter number 1: 10
Enter number 2: 20
Total:  30
				
			

Related Articles

print the input string 10 times.

Find the last element of a tuple using negative indexing

In this python tuple program, we will find the last element of a tuple using negative indexing.

Steps to solve the program
  1. Take a tuple as input.
  2. Find the last element of a tuple using tuple_name[-1].
  3. Print the output.
				
					tup = ('p','y','t','h','o','n')
print("Original tuple: ",tup)
print("Last element of the tuple using negative indexing: ",tup[-1])
				
			

Output :

				
					Original tuple:  ('p', 'y', 't', 'h', 'o', 'n')
Last element of the tuple using negative indexing:  n
				
			

check the type of the input and return True if the type is a tuple and False if it is not a tuple.

Check the type of the input and return True if the type is a tuple

In this python tuple program, we will check the type of the input and return True if the type is a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Use an if-else statement and print True if the type is a tuple and False if it is not a tuple.
  3. Print the output.
				
					tup = (7,4,9,2,0)
if type(tup) == tuple:
    print("True")
else:
    print("False")
				
			

Output :

				
					True
				
			

program to swap tuples.

find the last element of a tuple using negative indexing.

Python tuple program to swap the tuples

In this python tuple program, we will swap the tuples.

Steps to solve the program
  1. Take two tuples as input.
  2. Swap the tuples by using the  ” operator i.e. a,b=b,a.
  3. Print the output.
				
					A=(7,4,9)
B=(3,)
print(f"tuple1: {A}, tuple2: {B}")
A,B = B,A
print(f"tuple1: {A}, tuple2: {B}")
				
			

Output :

				
					tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (3,), tuple2: (7, 4, 9)
				
			

calculate the average of the elements in the tuple.

check the type of the input and return True if the type is a tuple and False if it is not a tuple.

Program to calculate the average of the elements in the tuple

In this python tuple program, we will calculate the average of the elements in the tuple.

Steps to solve the program
  1. Take a tuple as input and create a variable add and set its value equal to 0.
  2. Use a for loop to iterate over numbers in the tuple.
  3. Add the numbers to the add variable.
  4. Find the average of the elements in the tuple by dividing the value of add variable by the length of the tuple i.e number of elements in the tuple.
  5. Print the output.
				
					tup = (5,3,9,6)
print("Original tuple: ",tup)
add = 0
for ele in tup:
    add += ele
print("Average of elements in the tuple: ",add/len(tup))
				
			

Output :

				
					Original tuple:  (5, 3, 9, 6)
Average of elements in the tuple:  5.75
				
			

count the total number of unique tuples.

program to swap tuples.

Python tuple program to convert a binary tuple to an integer

In this python tuple program, we will convert a binary tuple to an integer.

Steps to solve the program
				
					tup = (1,0,0)
print("Original tuple is : ",tup) 
result = int("".join(str(val) for val in tup), 2)
print("Decimal number: ",result)
				
			

Output :

				
					Original tuple is :  (1, 0, 0)
Decimal number:  4
				
			

find common elements between two lists of tuples.

count the total number of unique tuples.