Created
September 4, 2020 14:59
-
-
Save shinysu/b5665d01ac347c2302b8c4dccc403fcb to your computer and use it in GitHub Desktop.
Day5 examples
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
| """ | |
| + - - - - + - - - - + | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| + - - - - + - - - - + | |
| | | | | |
| | | | | |
| | | | | |
| | | | | |
| + - - - - + - - - - + | |
| """ | |
| def plus_line(): | |
| print("+ ", end='') | |
| for i in range(4): | |
| print('- ', end='') | |
| print("+ ",end='') | |
| for i in range(4): | |
| print('- ', end='') | |
| print("+") | |
| def other_line(): | |
| print("| ", end='') | |
| for i in range(4): | |
| print(" ", end='') | |
| print("| ", end='') | |
| for i in range(4): | |
| print(" ", end='') | |
| print("|") | |
| for x in range(1, 12): | |
| if x == 1 or x == 6 or x == 11: | |
| plus_line() | |
| else: | |
| other_line() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment