Data structure
empty list
|
|
Check whether a list is empty
|
|
Print all items in the list
|
|
All except the first one
|
|
String
concatenation
|
|
Trim
Use stri()
|
|
Padding
Forces the field to be left-aligned within the available space
|
|
For more other format please see https://docs.python.org/3/library/string.html#format-specification-mini-language
Loop
You can increment not only by one when using range for i in range(3, 20, 2) the last argumnet is for the incremtal
Make the file callable
if name == ‘main’: main()
Method
Ignore return value
use underscore ignore specific value return
|
|
User inputs
default value
|
|
get password
The response will not be displayed on the console to frustrate any shoulder-surfers
|
|
I/O
Current directory
|
|
Exception
Handle the exception
|
|
exit
exit(0)
means a clean exit without any errors / problems
exit(1)
means there was some issue / error / problem and that is why the program is exiting.
This is not Python specific and is pretty common. A non-zero exit code is treated as an abnormal exit, and at times, the error code indicates what the problem was. A zero error code means a successful exit.
This is useful for other programs, shell, caller etc. to know what happened with your program and proceed accordingly.
Use else
There is one big reason to use else
- style and readability. It’s generally a good idea to keep code that can cause exceptions near the code that deals with them.
|
|
OS dependent
Check whether the OS is Unix or widows
|
|
nt means that you are running windows, and posix mac
Buffering to output
Python’s standard out is buffered (meaning that it collects some of the data “written” to standard out before it writes it to the terminal). Calling sys.stdout.flush()
forces it to “flush” the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so.