Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday

Python Multiplication Table of Specified Number of Columns and Rows Using Python User input() Function

In this example, we create a python multiplication table program that allows user to input the start number and end number of columns and rows.

The python user input() function is used to accept input from user.


Output:


Tuesday

Python Multiplication Table: Multiplication of Numbers with Python For Loop and Range Function

This program displays the multiplication table of numbers (from 1 to 12) using Python for loop and range function. 

The Code:


columns = range(2,13) # from number 2 to 12 (index 13)
rows = range(2,13) # from number 2 to 12 (index 13)
for x in columns: # loop through the column range
print('\nTABLE ' + str(x))
for y in rows: # loop through the row range
product = x * y 
print(str(x) + " x " + str(y) + " = " + str(product))

In this example, we have used the for loop along with the range() function to iterate tables 2 to 12 (and it can also be extended). The arguments inside the range() function (2, 12) can be modified to test for other values.

Output:



Friday

Python Program — Display Even and Odd Numbers


Using Python to loop through a specified range of numbers to display odd and even numbers.

Even numbers are numbers divisible by 2, while odd numbers are numbers whose remainder equals 1 if divided by two.

This program displays even and odd numbers between 1 and a specified limit. The limit can be changed by changing the value of the last number variable. The initial even or initial odd number value can also be changed to specify the start number.

last_number = 30 #the end number - value can be changed
# Display Even Number
print("Even Numbers:")
i = 2 #initial even number
while i <= last_number:
  print(i),
  i += 2 #increment i by 2
#Display Odd Numbers
print("\n\nOdd Numbers:")
i = 1 #initial odd number
while i <= last_number:
print(i),
i += 2 #increment i by 2

Download program here.