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.

Python tuple program to count the total number of unique tuples in a list

In this python tuple program, we will count the total number of unique tuples in a list.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Create a set of the given list to remove duplicate tuples using set().
  3. Now count the total number of unique tuples using len().
  4. Print the output.
				
					l =[(8,9),(4,7),(3,6),(8,9)]
print("Original list of tuples: ",l)
print("Total number of unique tuples: ",len(set(l)))
				
			

Output :

				
					Original list of tuples:  [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples:  3
				
			

convert a binary tuple to an integer.

calculate the average of the elements in the tuple.

Find common elements between two lists of tuples

In this python tuple program, we will find common elements between two lists of tuples.

Steps to solve the program
  1. Take two lists of tuples as input.
  2. Create a set of two lists to remove duplicate tuples by using set().
  3. Find the common tuples between two lists of tuples using the ” operator.
  4. Print the output.
				
					tup1 = [(1,5),(4,8),(3,9)]
tup2 = [(3,9),(5,6),(1,5),(0,4)]
print(f"Tuples: {tup1} and {tup2}")
result = (set(tup1) & set(tup2))
print("Common elements between tuples: ",result)
				
			

Output :

				
					Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples:  {(3, 9), (1, 5)}
				
			

order tuples by external list.

convert a binary tuple to an integer.