<style> .reveal { font-size: 20px; } .reveal div.para { text-align: left; } .reveal ul { display: block; } .reveal ol { display: block; } img[alt=drawing] { width: 200px; } </style> # COMP1010 ## 4 Python in the Real World --- ## Moving away from Colab * To make our own websites, we need a bit more flexibility than Colab can give us. * Set up your computer by following the instructions [here](https://hackmd.io/@sim/SJFcNywtq). --- ## Terminology **Debugging**: finding errors in your code and fixing them. --- ## Checking Variable Contents * Helpful in debugging * Doesn't work in Colab, because Colab uses Python 3.6.9, and this only works from Python 3.8 * Thanks to: [jwburn19](https://www.reddit.com/r/learnpython/comments/nur6o9/til_ive_been_making_debugging_statements_harder/) --- ## Checking Variable Contents <div class="para">Rather than:</div> ```{python} example_variable = 42 print(f"example_variable = {example_variable}") ``` <div class="para">We can do:</div> ```{python} example_variable = 42 print(f"{example_variable=}") ``` --- ## Code Style * Indentation: * Not tabs - spaces. * 4 spaces per indentation level is ideal. * 2, 3 or 4 spaces are acceptable, but they must be consistent. * Line length: * Line length should not exceed 79 characters. * [Reasoning](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) * If you need help (eg your line of code is longer than this, and you need to put it over 2 lines, but then the code breaks), post your specific example on the forums and we can show you how to do this. --- # Code Style * Functions: * Don't leave chunks of code floating around your file. * When in doubt, put your code in a (well-named) function, and call it at the right time. --- # File Structure ```{python} # import libraries (if you have them) # define constants (if you have them) # other functions you write (if you have them) def main(): # Your code here if __name__ == '__main__': main() ``` --- ## Example You can convert any of our programs so far: * [Guess My Number](https://github.com/sim-mautner/cs1010/blob/main/lectures/03-python-and-programming-fundamentals/02-control-flow.ipynb) * [Count Punctuation](https://github.com/sim-mautner/cs1010/blob/main/lectures/03-python-and-programming-fundamentals/02-control-flow.ipynb) * [Tic Tac Toe](https://github.com/sim-mautner/cs1010/blob/main/lectures/03-python-and-programming-fundamentals/04-collections.ipynb) --- ## Checking the main What is the purpose of this? ```{python} if __name__ == '__main__': main() ``` --- ## Constants * Variables, except... * Don't change throughout your entire code. * Style: should be defined in capital letters. * Examples: * Guess My Number: * `START_HIGH = 100` * `START_LOW = 0` * Tic Tac Toe: * `BOARD_SIZE = 3` * `PLAYER_1 = 'X'` * `PLAYER_2 = 'O'` * `EMPTY = '-'` ---
{"slideOptions":"{\"transition\":\"slide\"}","metaMigratedAt":"2023-06-17T02:58:49.735Z","metaMigratedFrom":"YAML","title":"4 Python in the Real World","breaks":true,"contributors":"[{\"id\":\"969c3c3d-0ef4-4f08-b22a-2f2b8951224b\",\"add\":3070,\"del\":0}]"}
    303 views