-
Collections are groups of items. Python supports several types of collections. Three of the most common are dictionaries, lists and arrays.
Lists are a collection of items. Lists can be expanded or contracted as needed, and can contain any data type. Lists are most commonly used to store a single column collection of information, however it is possible to nest lists
Arrays are similar to lists, however are designed to store a uniform basic data type, such as integers or floating point numbers.
Dictionaries are key/value pairs of a collection of items. Unlike a list where items can only be accessed by their index or value, dictionaries use keys to identify each item.
-
For loops takes each item in an array or collection in order, and assigns it to the variable you define.
names = ['John Doe', 'Jane Doe'] for name in names: print(name)
While loops perform an operation as long as a condition is true.
names = ['Christopher', 'Susan'] index = 0 while index < len(names): name = names[index] print(name) index = index + 1
-
Functions allow you to take code that is repeated and move it to a module that can be called when needed. Functions are defined with the
defkeyword and must be declared before the function is called in your code. Functions can accept parameters and return values.def functionname(parameter): # code to execute return value
-
Functions allow you to take code that is repeated and move it to a module that can be called when needed. Functions are defined with the
defkeyword and must be declared before the function is called in your code. Functions can accept one or more parameters and return values.def function_name(parameter): # code to execute return value
Parameters can be assigned a default value making them optional when the function is called.
def function_name(parameter=default): # code to execute return value
When you call a function you may specify the values for the parameters using positional or named notation
def function_name(parameter1, parameter2): # code to execute return value # Positional notation pass in arguments in same order as parameters are declared result = function_name(value1,value2) # Named notation result = function_name(parameter1=value1, parameter2=value2)
-
Modules allow you to store reusable blocks of code, such as functions, in separate files. They're referenced by using the
importstatement.# import module as namespace import helpers helpers.display('Not a warning') # import all into current namespace from helpers import * display('Not a warning') # import specific items into current namespace from helpers import display display('Not a warning')
Distribution packages are external archive files which contain resources such as classes and functions. Most every application you create will make use of one or more packages. Imports from packages follow the same syntax as modules you've created. The Python Package index contains a full list of packages you can install using pip.
Virtual environments allow you to install packages into an isolated folder. This allows you to better manage versions.
-
Many APIs return data in JSON, JavaScript Object Notation. JSON is a standard format that can is readable by humans and parsed or generated by code.
JSON is built on two structures:
- collections of key/value pairs
- lists of values
JSON Linters will format JSON so it easier to read by a human. The following website have JSON linters:
Python includes a json module which helps you encode and decode JSON
-
Decorators are similar to attributes in that they add meaning or functionality to blocks of code in Python. They're frequently used in frameworks such as Flask or Django. The most common interaction you'll have with decorators as a Python developer is through using them rather than creating them.
# Example decorator @log(True) def sample_function(): print('this is a sample function')