Python Function Print Table of Number from Start to End

By | April 16, 2021

Python Function Print Table of Number Start to End – this Python program will display a table of the given number from starting value to ending value.

Python program able function of a number from start to ending value

How Python table Function program will work?

First of all, it will ask the user to input the number, start and end value.

The program will pass these three values to the function table(). The table function will print the required table of the given number from start ton ending value.

Explanation of Print Table Function

def print_table(num, start, end): 
    """ This function prints multiplication table of a given number""" 
    for i in range(start, end+1):
        print(num,' x ', i, ' = ',num*i)

 

  1. Use def keyword to define function header with name print_table and 3 parameters in parentheses. You will end the function header by a colon.
  2. Write down a doc string in first line of the function to describe the purpose of this function.
  3. Now use a for loop to display the table of the given number from start value to end value provided through arguments in the function call.
  4. We will use a range given by start to end inclusive.
  5. Use a print() function in loop body to print one line of table as follows
  6. 4 x 1 = 4

Source Code of Python program Table Function of given number Start To End

# An example Python program 
# to define and call a function table of
# a number 
# to show multiplication table of a number
# from a start value to ending value.
# Author : www.EasyCodebook.com (c) 
# Note the first line in function is called 
# document string,  

# define the function with formal parameter num 

def print_table(num, start, end): 
    """ This function prints multiplication table of a given number"""
    for i in range(start, end+1): 
        print(num,' x ', i, ' = ',num*i) 
# end of function table


# input a number
n = int(input("Please Enter a number to print its multiplication table:"))
start = int(input("Enter  start value to start printing table from:"))
end =  int(input("Enter  end value to stop printing table:"))
# call the function tanle by passing actual parameter 'n' 

print_table(n, start, end)

 

Output

Please Enter a number to print its multiplication table:7
Enter  start value to start printing table from:1
Enter  end value to stop printing table:15
7  x  1  =  7
7  x  2  =  14
7  x  3  =  21
7  x  4  =  28
7  x  5  =  35
7  x  6  =  42
7  x  7  =  49
7  x  8  =  56
7  x  9  =  63
7  x  10  =  70
7  x  11  =  77
7  x  12  =  84
7  x  13  =  91
7  x  14  =  98
7  x  15  =  105
 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *