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
- Create a function table.
- Use the def keyword to define the function.
- Pass a parameter i.e. a number to the function.
- Take the number (parameter) as input through the user and pass it to the function.
- Create a variable and assign its value equal to 0.
- Use a for loop to iterate over 1-10.
- Multiply the given number with the loop number and store the result in the created variable.
- Print the result after each iteration.
- 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
Python function program to find the maximum of three numbers.
Python function program to find the sum of all the numbers in a list.
Python function program to multiply all the numbers in a list.
Python function program to reverse a string.
Python function program to check whether a number is in a given range.