Exploring Multiplication in Python: A Simple Program
In this post, we will delve into a basic Python program that demonstrates the concept of multiplication using both explicit print statements and a while loop.
Part 1: Using Print Statements
#print num series 2 4 8 16 32
#using print statement
print(2)
print(4)
print(8)
print(16)
print(32)
In this section, we use individual print statements to display the sequence of numbers. Each number is a power of 2, starting from 21 and doubling with each subsequent number.
Part 2: Using a Variable
#print num series 2 4 8 16 32
#using a variable
no = 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)
Here, we start with the number 2 assigned to the variable 'no
β. We then repeatedly multiply 'no
β by 2 and print the result. This approach showcases the concept of updating a variableβs value based on its previous state.
Part 3: Using a While Loop
#print num series 2 4 8 16 32
#using while loop
no = 2
while no <= 32:
print(no)
no = no * 2
In this final part, we use a βwhile'
loop to achieve the same result as above. The loop continues to run as long as βno'
is less than or equal to 32. Inside the loop, we print the current value of 'no
β and then double it. This approach is more efficient and scalable, as it reduces the need for repetitive code.
Conclusion
This simple Python program effectively demonstrates how to use basic arithmetic operations and loops to achieve a desired sequence of numbers. Itβs a great starting point for beginners to understand variable manipulation and control flow in programming.
program:
#print num series 2 4 8 16 32
#using print statement
print(2)
print(4)
print(8)
print(16)
print(32)
print()#just for space in output
#print num series 2 4 8 16 32
#using a variable
no=2
print(no)
no=no*2
print(no)
no=no*2
print(no)
no=no*2
print(no)
no=no*2
print(no)
print()#just for space in output
#print num series 2 4 8 16 32
#using while loop
no=2
while no<=32:
print(no)
no=no*2