Python extra

Miscellaneous #

  • ; is not used, use spaces instead!

  • If you wanna 2 or more commands in 1 line, you can use ; to separate them.

  • Tab size usually is 4.

  • Lines can be broken with just an Enter, you also use \ between the lines if you want.

  • From Python 3.6, you can use 1_000_000 for the number 1000000.

  • from __future__ import <package> allows current version of python to use <package> which is only available in the "future" version of python.

  • if __name__ == "__main__": determines the main running file of codes. The codes inside this command only run if the file containing it is the main executed file! [read more]

  • If we don't need to mention some variable, use _:

    for _ in range(2):
    print("Thi")
    Thi
    Thi
    

Swap 2 variables #

a, b = b, a

Clear variables #

# Clear (without confirmation) a variable from workspace
del <var>
%reset_selective <var>
# Check if available and then delete
if '<var>' in globals(): del <var>
# use locals() inside a def
# Clear all variables
%reset # with confirmation
%reset -f # without confirmation

References #