The 4 common Python programming styles
You might be a nonprogrammer or a developer who isn’t interested in learning some new arcane method of writing code. If so, Python is the a great language for you. Most programming languages use just one coding style, which reduces flexibility for the programmer. Python is different, though. You can use a number of coding styles with it to achieve differing effects. Among the various Python coding styles, here are the four commonly used:
- Functional: Every statement is a kind of math equation. This style lends itself well to use in parallel processing activities. Academics and data scientists tend to use this coding style regularly. However, nothing stops you from using it even if you don’t fall into one of these groups.
- Imperative: Computations occur as changes to program state. This style is most used for manipulating data structures. Scientists of all sorts rely on this programming style because it demonstrates processes so clearly.
- Object-oriented: This is the style commonly used with other languages to simplify the coding environment by using objects to model the real world. Python doesn’t fully implement this coding style because it doesn’t support features like data hiding, but you can still use this approach to a significant degree. This is the style that most developers use, but other groups can use it when creating more complicated applications.
- Procedural: Most people begin learning a language by using procedural code, where tasks proceed a step at a time. This style is most used for iteration, sequencing, selection, and modularization. It’s the simplest form of coding you can use. Nonprogrammers love this style because it’s the least complicated way to achieve smaller, experimental tasks.
Common Python operators
Operators help determine the interactions between elements in a Python statement. For example, when you write 2 + 3
, it means to add (using the +
operator) the value 2
to the value 3
for a sum of 5
. It’s important to know which operators Python supports, and remembering them all is not always easy. The following table provides a quick summary.
Operator | Type | Description | Example |
− | Arithmetic | Subtracts the right operand from left hand operand. | 5 − 2 = 3 |
− | Unary | Negates the original value so that positive becomes negative and vice versa. | −(−4) results in 4 while −4 results in −4 |
−= | Assignment | Subtracts the value found in the right operand from the value found in the left operand and places the result in the left operand. | MyVar -= 2 results in MyVar containing 3 |
!= | Relational | Determines whether two values are not equal. Some older versions of Python would allow you to use the <> operator in place of the != operator. Using the <> operator results in an error in current versions of Python. | 1 != 2 is True |
% | Arithmetic | Divides the left operand by the right operand and returns the remainder. | 5 % 2 = 1 |
%= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the remainder in the left operand. | MyVar %= 2 results in MyVar containing 1 |
& (And) | Bitwise | Determines whether both individual bits within two operators are true and sets the resulting bit to true when they are. | 0b1100 & 0b0110 = 0b0100 |
* | Arithmetic | Multiplies the right operand by the left operand. | 5 * 2 = 10 |
** | Arithmetic | Calculates the exponential value of the right operand by the left operand. | 5 ** 2 = 25 |
**= | Assignment | Determines the exponential value found in the left operand when raised to the power of the value found in the right operand and places the result in the left operand. | MyVar ** 2 results in MyVar containing 25 |
*= | Assignment | Multiplies the value found in the right operand by the value found in the left operand and places the result in the left operand. | MyVar *= 2 results in MyVar containing 10 |
/ | Arithmetic | Divides the left operand by the right operand. | 5 / 2 = 2.5 |
// | Arithmetic | Performs integer division, where the left operand is divided by the right operand and only the whole number is returned (also called floor division). | 5 // 2 = 2 |
//= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the integer (whole number) result in the left operand. | MyVar //= 2 results in MyVar containing 2 |
/= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the result in the left operand. | MyVar /= 2 results in MyVar containing 2.5 |
^ (Exclusive or) | Bitwise | Determines whether just one of the individual bits within two operators are true and sets the resulting bit to true when they are. When both bits are true or both bits are false, the result is false. | 0b1100 ^ 0b0110 = 0b1010 |
| (Or) | Bitwise | Determines whether either of the individual bits within two operators are true and sets the resulting bit to true when they are. | 0b1100 | 0b0110 = 0b1110 |
~ | Unary | Inverts the bits in a number so that all the 0 bits become 1 bits and vice versa. | ~4 results in a value of −5 |
~ (One’s complement) | Bitwise | Calculates the one’s complement value a number.
|
~0b1100 = −0b1101
~0b0110 = −0b0111 |
+ | Arithmetic | Adds two values together. | 5 + 2 = 7 |
+ | Unary | Provided purely for the sake of completeness. This operator returns the same value that you provide as input. | +4 results in a value of 4 |
+= | Assignment | Adds the value found in the right operand to the value found in the left operand and places the result in the left operand. | MyVar += 2 results in MyVar containing 7 |
< | Relational | Verifies that the left operand value is less than the right operand value. | 1 < 2 is True |
<< (Left shift) | Bitwise | Shifts the bits in the left operand left by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 << 2 = 0b11001100 |
<= | Relational | Verifies that the left operand value is less than or equal to the right operand value. | 1 <= 2 is True |
= | Assignment | Assigns the value found in the right operand to the left operand. | MyVar = 2 results in MyVar containing 2 |
== | Relational | Determines whether two values are equal. Notice that the relational operator uses two equals signs. A mistake many developers make is using just one equals sign, which results in one value being assigned to another. | 1 == 2 is False |
> | Relational | Verifies that the left operand value is greater than the right operand value. | 1 > 2 is False |
>= | Relational | Verifies that the left operand value is greater than or equal to the right operand value. | 1 >= 2 is False |
>> (Right shift) | Bitwise | Shifts the bits in the left operand right by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 >> 2 = 0b00001100 |
and | Logical | Determines whether both operands are true. | True and True is True
True and False is False False and True is False False and False is False |
in | Membership | Determines whether the value in the left operand appears in the sequence found in the right operand. | “Hello” in “Hello Goodbye” is True |
is | Identity | Evaluates to true when the type of the value or expression in the right operand points to the same type in the left operand. | type(2) is int is True |
is not | Identity | Evaluates to true when the type of the value or expression in the right operand points to a different type than the value or expression in the left operand. | type(2) is not int is False |
not | Logical | Negates the truth value of a single operand. A true value becomes false and a false value becomes true. | not True is False
not False is True |
not in | Membership | Determines whether the value in the left operand is missing from the sequence found in the right operand. | “Hello” not in “Hello Goodbye” is False |
or | Logical | Determines when one of two operands are true. | True or True is True
True or False is True False or True is True False or False is False |
Python operator precedence
Operator | Description |
() | Parentheses are used to group expressions and to override the default precedence so that you can force an operation of lower precedence (such as addition) to take precedence over an operation of higher precedence (such as multiplication). |
** | Exponentiation raises the value of the left operand to the power of the right operand. |
~ + – | Unary operators interact with a single variable or expression. |
* / % // | Multiply, divide, modulo and floor division. |
+ – | Addition and subtraction. |
>> << | Right and left bitwise shift. |
& | Bitwise AND. |
^ | | Bitwise exclusive OR and standard OR. |
<= < > >= | Comparison operators. |
== != | Equality operators. |
= %= /= //= -= += *= **= | Assignment operators. |
is is not | Identity operators. |
in not in | Membership operators. |
not or and | Logical operators. |
Common Jupyter Notebook magic functions
Jupyter Notebook relies on the literate programming approach originally advanced by Donald Knuth. This means that the Integrated Development Environment (IDE) produces output that looks more like a report than the complex coding environments that most people rely on. The magic functions add to this capability by creating an environment in which you can choose something other than the expected result. The following table highlights the most important magic functions that Jupyter Notebook provides.
Magic Function | Type Alone Provides Status? | Description |
%alias | Yes | Assigns or displays an alias for a system command. |
%autocall | Yes | Enables you to call functions without including the parentheses. The settings are Off, Smart (default), and Full. The Smart setting applies the parentheses only if you include an argument with the call. |
%automagic | Yes | Enables you to call the line magic functions without including the % sign. The settings are False (default) and True. |
%autosave | Yes | Displays or modifies the intervals between automatic Notebook saves. The default setting is every 120 seconds. |
%cd | Yes | Changes directory to a new storage location. You can also use this command to move through the directory history or to change directories to a bookmark. |
%cls | No | Clears the screen. |
%colors | No | Specifies the colors used to display text associated with prompts, information system, and exception handlers. You can choose between NoColor (black and white), Linux (default), and LightBG. |
%config | Yes | Enables you to configure IPython. |
%dhist | Yes | Displays a list of directories visited during the current session. |
%file | No | Outputs the name of the file that contains the source code for the object. |
%hist | Yes | Displays a list of magic function commands issued during the current session. |
%install_ext | No | Installs the specified extension. |
%load | No | Loads application code from another source, such as an online example. |
%load_ext | No | Loads a Python extension using its module name. |
%lsmagic | Yes | Displays a list of the currently available magic functions. |
%magic | Yes | Displays a help screen showing information about the magic functions. |
%matplotlib | Yes | Sets the backend processor used for plots. Using the inline value displays the plot within the cell for an IPython Notebook file. The possible values are gtk’; ‘gtk3’; ‘inline’; ‘nbagg’; ‘osx’; ‘qt’; ‘qt4’; ‘qt5’; ‘tk’; and ‘wx’. |
%paste | No | Pastes the content of the Clipboard into the IPython environment. |
%pdef | No | Shows how to call the object (assuming that the object is callable). |
%pdoc | No | Displays the docstring for an object. |
%pinfo | No | Displays detailed information about the object (often more than is provided by help alone). |
%pinfo2 | No | Displays extra detailed information about the object (when available). |
%reload_ext | No | Reloads a previously installed extension. |
%source | No | Displays the source code for the object (assuming that the source is available). |
%timeit | No | Calculates the best performance time for an instruction. |
%%timeit | No | Calculates the best time performance for all the instructions in a cell, apart from the one placed on the same cell line as the cell magic (which could therefore be an initialization instruction). |
%unalias | No | Removes a previously created alias from the list. |
%unload_ext | No | Unloads the specified extension. |
%%writefile | No | Writes the contents of a cell to the specified file. |