Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
python [2017/04/24 16:21] – mgupton | python [2023/03/20 17:33] (current) – [Python] mgupton | ||
---|---|---|---|
Line 1: | Line 1: | ||
======Python====== | ======Python====== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | * [[Python Modules and Packages]] | ||
+ | * [[Python Tools]] | ||
+ | * [[python: | ||
+ | |||
+ | |||
======Core Elements & Constructs====== | ======Core Elements & Constructs====== | ||
=====Lists===== | =====Lists===== | ||
Line 41: | Line 49: | ||
b | b | ||
=> {} | => {} | ||
+ | </ | ||
+ | |||
+ | ======Basic Structures and Constructs====== | ||
+ | * If the %%__name__%% variable is set to %%__main__%% the module is being loaded as a main script and program entry point. Otherwise it's being loaded as a library module. | ||
+ | <code python> | ||
+ | if __name__ == " | ||
+ | | ||
+ | else: | ||
+ | pass | ||
+ | </ | ||
+ | |||
+ | ======Conditionals====== | ||
+ | <code python> | ||
+ | num = 100 | ||
+ | |||
+ | a = [" | ||
+ | |||
+ | if " | ||
+ | pass | ||
+ | | ||
+ | if num > 10: | ||
+ | pass | ||
+ | elif num < 10: | ||
+ | pass | ||
+ | else: | ||
+ | pass | ||
+ | </ | ||
+ | |||
+ | ======Loops/ | ||
+ | <code python> | ||
+ | colors = [" | ||
+ | |||
+ | for c in colors: | ||
+ | print(" | ||
</ | </ | ||
Line 58: | Line 100: | ||
</ | </ | ||
- | ======Basic Structures | + | =====Iterate over indicies |
- | * If the %%__name__%% variable is set to %%__main__%% the module is being loaded as a program entry point. Otherwise it's being loaded as a library module. | + | |
<code python> | <code python> | ||
- | if __name__ == "__main__": | + | a = ["a", " |
- | print(" | + | |
+ | for k, v in enumerate(a): | ||
+ | |||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | ======Common Elements and Ways====== | ||
+ | ======Determine if variable is defined====== | ||
+ | <WRAP round info> | ||
+ | It might be a code smell/ | ||
+ | </ | ||
+ | =====Method 1====== | ||
+ | < | ||
+ | x_exists = ' | ||
+ | </ | ||
+ | |||
+ | =====Method 2====== | ||
+ | <code python> | ||
+ | try: | ||
+ | x | ||
+ | except NameError: | ||
+ | x_exists = False | ||
else: | else: | ||
- | pass | + | x_exists = True |
</ | </ | ||
- | ======Conditionals====== | + | =====Method 3===== |
<code python> | <code python> | ||
- | num = 100 | + | hasattr(a, ' |
+ | </ | ||
- | a = [" | + | =====Method 4===== |
+ | <code python> | ||
+ | if args.file is None or len(args.file) == 0: | ||
- | if " | + | |
- | pass | + | |
- | + | ||
- | if num > 10: | + | |
- | pass | + | |
- | elif num < 10: | + | |
- | pass | + | |
- | else: | + | |
- | | + | |
</ | </ | ||
+ | |||
+ | =======Resources/ | ||
+ | * [[http:// | ||
+ | * [[https:// | ||