Exiting a python program

Exit an endless loop Python program This method uses an interrupt to capture keyboard CTRL+C entered on the command line, but then cleanly exits from the program’s main loop.

Read More

Paths

Handling path slash character for Windows and Linux compatibility Windows uses “\\”Linux uses “/” Although using the Linux forwardslash will often work on Windows too, its a bit hacky and isn’t guaranteed to work. For correct Python code you should use the pathlib to solve it Detecting Windows or Linux for OS file system dependant […]

Read More

Python timers

System timer The time module in python provides various methods and functions related to time. Elapsed Seconds The time.time() method returns the current CPU time in seconds (the time since the start of the epoch which is system dependant, but its some date in the past). Elapsed milliseconds Because the time.time() method returns a floating […]

Read More

static variables inside a function

In C a static variable inside a function is only visible inside that function’s scope, but its lifetime is the entire life of the program, and it’s only initialized once. There is NO equivalence in Python. So if you want to be a proper python programmer you should just accept that! Use a class, use […]

Read More

Using error catching

Catching specific errors See the different exception types here If no errors occurred You can use the else keyword to define a block of code to be executed if no errors were raised: Raising an error

Read More