Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active November 13, 2020 13:37
Show Gist options
  • Save shinysu/8c7cbab47bbd5fa0815975aafc356571 to your computer and use it in GitHub Desktop.
Save shinysu/8c7cbab47bbd5fa0815975aafc356571 to your computer and use it in GitHub Desktop.
programs using loops
'''
using for loop to print numbers 1 to 10
range(a, b+1) starts from a and ends at b
'''
for i in range(1, 11):
print(i)
'''
using for loop to print a range of numbers
'''
start = int(input("Beginning range: "))
end = int(input("Ending range: "))
for number in range(start, end+1):
print(number)
'''
print the numbers in a given range while incrementing by 4
The range() takes a third argument, step that increments the number by that value after every iteration
'''
start = int(input("Beginning range: "))
end = int(input("Ending range: "))
for number in range(start, end+1, 4):
print(number)
'''
print all the even numbers in the given range of numbers
'''
start = int(input("Beginning range: "))
end = int(input("Ending range: "))
for number in range(start, end+1):
if number % 2 == 0:
print(number)
'''
print the sum of first n numbers
'''
n = int(input("Enter n: "))
sum = 0
for number in range(1, n+1):
sum = sum + number
'''
print the numbers from 10 to 1.
step counting of -1 is used to reduce the value by 1 for every iteration
'''
for i in range(10, 0, -1):
print(i)
'''
print the multiplication table of any given number - using while
'''
number = int(input("Enter any number to get it's table"))
i = 1
while i <= 10:
print(i,"*",number,"=", i*number)
i = i + 1
'''
print the multiplication table of any given number - using for loop
'''
number = int(input("Enter any number to get it's table"))
for i in range(1, 11):
print(i,"*",number,"=", i * number)
i = i + 1
1. Write a Python program that can draw the pattern shown below
*
* *
* * *
* * * *
* * * * *
You can also try out other patterns similar to this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment