0

Python heading

How to start python code like a pro?

15th Oct 2024, 10:24 PM
Codexx Keyys
Codexx Keyys - avatar
2 Réponses
+ 3
Codexx Keyys Most devices begin with a bash shell #!/bin/bash Then it is a matter of installing the right GUI and the necessary libraries or install certain frameworks. Tkinter PyQt wxPython Kivy PyGTK PySide THEN given that you have created something you can proceed to: import sys sys.path.append('/path/to/your/modules') import mymodule If you are not that far then just for practice follow basically what Jerry Hobby mentioned
16th Oct 2024, 3:15 AM
BroFar
BroFar - avatar
+ 2
A Python script has a top-to-bottom execution flow. So a few tips are: 1. Create functions and put those at the top. 2. Create your "main" execution code at the very bottom. 3. Create all your objects in separate files and import them. 4. Keep your functions small. It's better to isolate each function to do ONE thing and call other functions to do other things. 5. Use meaningful and clear names for functions and variables. 6. Add comments. 7. Be sure to test for error conditions Your code is much easier to read of it's a number of short functions that each do one thing. Name them accordingly. ############################################## # short demo of a python script def add_numbers(a, b): return a + b def subtract_numbers(a, b): return a - b def divide_numbers(a, b): try: return a / b except: print(f"Error dividing: {a} / {b}") ############################################## # main print(add_numbers(3, 5)) print(subtract_numbers(5, 10)) print(divide_numbers(5, 0)) By doing it this way, all your functions are clear and easy to understand. If you find that you've written functions that are a little complicated, that's ok. Just break it apart and rewrite it so it's very easy to understand. That process is called REFACTORING. In the end, imagine you will come back to this code a year from now. You want to be able to understand it easily.
15th Oct 2024, 10:47 PM
Jerry Hobby
Jerry Hobby - avatar