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
| # USEFUL for extending functionality of functions you don't wanna modify | |
| # this function takes a function as its parameter | |
| def decorator(function): | |
| def wrap(): | |
| print("Adding Printing Functionality") | |
| function() | |
| print("Adding Print Functionality") |
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 even_numbers(x): | |
| for i in range(x): | |
| if i % 2 == 0: | |
| yield i | |
| for i in even_numbers(100): | |
| 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
| # ASSERIONS | |
| print(1) | |
| assert 2 + 2 == 4 | |
| print(2) | |
| assert 1 + 1 == 3 | |
| print(3) | |
| # Assertions are often placed at the start of a function to check for valid input | |
| # and at the end to check for valid output |
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
| # RAISING EXCEPTIONS | |
| print("Some Text") | |
| raise ValueError | |
| print("Something Else") | |
| # Detail Exception | |
| name = "1234" | |
| raise NameError("Invalid Name") | |
| # Raise in Except |
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
| try: | |
| word = "spam" | |
| print(word/0) | |
| except: | |
| print("An Error Occured") | |
| finally: | |
| print("This code will run no matter what errors") |
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
| try: | |
| num = "cos" | |
| num2 = 2 | |
| div = num2/num | |
| except ZeroDivisionError: | |
| print("An Error due to zero Division") | |
| except (ValueError, TypeError): | |
| print("An Error occured") | |
| else: | |
| print("The answer is: " + str(div)) |
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
| try: | |
| num = "text" | |
| num2 = 2 | |
| ans = num2/num | |
| except TypeError: | |
| print("An Error occured: You cannot perfom division on a string.") | |
| else: | |
| print("The answer is: " + str(ans)) |
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
| for x in range(2, 30, 3): | |
| print(x) |
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
| for i in range(10): | |
| 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
| numbers = list(range(5,20, 2)) | |
| print(numbers) |