Last active
November 13, 2020 13:37
-
-
Save shinysu/8c7cbab47bbd5fa0815975aafc356571 to your computer and use it in GitHub Desktop.
programs using loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
print the sum of first n numbers | |
''' | |
n = int(input("Enter n: ")) | |
sum = 0 | |
for number in range(1, n+1): | |
sum = sum + number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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