2.9. Best Practices¶
This section includes some best practices to write Python code.
2.9.1. Underscore(_): Ignore Values That Will Not Be Used¶
When assigning the values returned from a function, you might want to ignore some values that are not used in future code. If so, assign those values to underscores
def return_two(): return 1, 2 _, var = return_two() var
2.9.2. Underscore “_”: Ignore The Index in Python For Loops¶
If you want to repeat a loop a specific number of times but don’t care about the index, you can use
for _ in range(5): print('Hello')
Hello Hello Hello Hello Hello
2.9.3. Python Pass Statement¶
If you want to create code that does a particular thing but don’t know how to write that code yet, put that code in a function then use
Once you have finished writing the code in a high level, start to go back to the functions and replace
pass with the code for that function. This will prevent your thoughts from being disrupted.
def say_hello(): pass def ask_to_sign_in(): pass def main(is_user: bool): if is_user: say_hello() else: ask_to_sign_in() main(is_user=True)