Python/Syntax: Difference between revisions
Brodriguez (talk | contribs) (Create page) |
Brodriguez (talk | contribs) m (Correct typo) |
||
Line 131: | Line 131: | ||
Or we can use the following syntax to pass values for each one: | Or we can use the following syntax to pass values for each one: | ||
kwarg_func(my_var=True, my_int=555, some_string='This is | kwarg_func(my_var=True, my_int=555, some_string='This is a different string.') | ||
Note that we can also do any combination thereof, where we leave some values as the defaults, but override others. | Note that we can also do any combination thereof, where we leave some values as the defaults, but override others. |
Revision as of 11:26, 27 December 2020
Unlike most other languages, Python attempts to be simplistic in syntax design.
Thus, there are no semicolons to indicate a line end, and there are no brackets.
However, indentation is very important, and Python raises syntax errors without proper indentation.
Syntax Styling
Python recommends following the official PEP-8 guidelines for code styling.
Note that PEP-8 is ultimately just a suggestion, and can be adapted for your needs. The most important part is to be consistent internally, within each individual project.
Of note though:
- PEP-8 recommends using spaces over tabs, for indentation.
- Note though, that either is technically okay, but combining the two in one file will result in syntax errors.
- PEP-8 recommends 79 character maximum line length. But this is frankly really small now of days, particularly as screen sizes grow larger.
- Instead, we recommend following PyCharm's default of 120 characters per line, maximum.
- PEP-8 recommends removing trailing whitespaces and adding a blank line at the end of each file.
- This is easily done automatically on file save, via EditorConfig Files (See [https://git.brandon-rodriguez.com/other/editorconfig_references this git project) for editorconfig examples).
Comments
Inline Comments
# This is an inline comment.
Block Comments
""" This is a block comment. Comment line 2. Another block comment line. """
Block/Region Declaration
Regions are used for organizational purposes, and allow for code folding at the click of a button. To be useful, both the start and end must be declared.
#region MyRegionName ... #endregion MyRegionName
Variables
Variables are weakly typed in Python. That means you can just declare a variable name, and Python will automagically figure out what typing it is.
Variable Definition
a_bool = True b_bool = False my_var_1 = "This is " my_var_2 = "a string."
Variable Usage
print('Printing variable values'.) print(a_bool) print(b_bool) print('{0}{1}'.format(my_var_1, my_var_2))
Booleans
Python uses the following bools:
true_bool = True false_bool = False
Null Values
Python uses None
in place of the standard null
:
null_var = None
If Statements
Note that Python uses and
to replace the standard &&
, and or
to replace the standard {{ ic |||}.
Basic If
if x == y: # Logic if true.
Full If
if x == y: # Logic if true. elif x and (y or z): # Logic for "else if" true. else: # Logic for false.
Switch/Case Statements
Surprisingly, Python does not support case statements. Instead, Python officially recommends just using if statements to accomplish the equivalent.
Functions
Basic Template
def <function_name>(<args>, <kwargs>): """" Function description here. :param <variable_name>: <variable_description> :return: <return_val_description> """ # Function logic here. ... # If function has a return value, then return it with the following. return <variable_here>
Examples
Basic example:
def my_func(): # Function logic here.
Example with "arg" arguments:
def arg_func(my_int, my_string, some_other_arg): # Function logic here.
Example with "kwarg" arguments:
def kwarg_func(my_var=None, my_int=12345, some_string='This is the default value.'): # Function logic here.
Args and Kwargs
"Args" and "Kwargs" are fancy ways to say "variables with a default" and "variables without a default".
Note that in function declaration, arg values must always come before kwarg values.
Args
To show, in the "arg" example above, we have my_int
. This is an "arg" because it does not have an equal sign next to it.
Effectively, this means the variable must be provided into the function, or a syntax error will raise.
To call the function in the above "arg" example, we use the following syntax:
arg_func(12345, 'This is a string.', True)
Kwargs
Meanwhile, in the "kwarg" example above, we have "my_int" again, but it looks like this my_int=12345
. The equal sign means it's a "kwarg".
Effectively, a kwarg is an optional argument, in that you can call the function without specifying it. In such a case, the kwarg will take on the value after the equal sign, as a default. However, if you do pass a value for the kwarg, then it will use that value instead of the default.
To call the function in the above "kwarg" example, we use the following syntax to use default values:
kwarg_func()
Or we can use the following syntax to pass values for each one:
kwarg_func(my_var=True, my_int=555, some_string='This is a different string.')
Note that we can also do any combination thereof, where we leave some values as the defaults, but override others.