In this chapter we examine Python objects and operators, and learn how to write comments in our code. Including comments in your code is very important for two reasons. First, they serve as a reminder of our thought processes on the work we did weeks or months after we’ve created a script. Second, they help other programmers understand why we did what we did.
Objects in Python
In Python, any item is considered an object, a container to place data. Python objects include tuples, lists, sets, dictionaries, and containers. Python processing is based on objects.
- 1.
A name
- 2.
A type
- 3.
An ID
Object names consist of alphanumeric characters and underscores—in other words, all characters from A through Z, a through z, 0 through 9, and _. “Type” is the type of object, such as string, numeric, or Boolean. “ID” is a number that identifies the object uniquely.
Objects remain inside computer memory and can be retrieved. When no longer needed, a garbage-collecting feature frees up the space.
Some IDs and types are assigned automatically. With regard to names, however, note there are some names that cannot be used for objects.
Reserved Terms for the System
and, as, assert, break, class, continue, def, del, elif, else, except, exec, False, finally, for, from, global, if, import, in, is, lambda, None, not, or, pass, print, raise, return, True, try, while, with, yield
They must begin with a letter or underscore.
They must contain only letters, numbers, and underscores.
They are case sensitive. For example, a test object is not the same as a TEST object or a Test object.
Entering Comments in the Code
Types of Data
Python Data Types
Data Type | Example |
---|---|
Integer (int) | 1, 20, –19 |
Float | 1.7, 12.54 |
Complex | 657.23e+34 |
String (str) | "Hello World", 'string1', """ string2 """ |
List | list = ['a', 'b', 'c' ] |
Tuple | tuple = ('Laura', 29, 'Andrea', 4) |
Dictionary | dictionary = {'name' : 'Simon', 'key': 'D007'} |
File Format
After you create a script in Python, you need to save it with a .py extension. Typically, when it comes to complex scripts, you create a script on an editor that you then run. A .py script can be written using any one of the different editors discussed earlier—even a normal text editor—and then can be renamed with .py extension.
Operators
Mathematical
Comparison
Membership
Bitwise
Assignment
Logical
Identity
Beside these operators, there is also a hierarchy that marks the order in which they are used. Operators are very important for arithmetic, or even to extract some data with specific characteristics.
Mathematical Operators
Mathematical Operators
Operator | Description | Example |
---|---|---|
+ | Addition | 3 + 2 = 5 |
− | Subtraction | 10 − 4 = 6 |
* | Multiplication | 4 * 3 = 12 |
/ | Division | 20/2 = 10 |
% | Modulo | 21/2 = 1 |
** | Exponentiation | 3 ** 2 = 9 |
// | Floor | 10.5 // 2 = 5.0 |
Comparison and Membership Operators
Comparison and Membership Operators
Operator | Description |
---|---|
> | Greater than |
< | Less than |
== | Equal to |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Different |
is | Identity |
is not | Non identity |
in | Exists in |
not in | Does not exist in |
Python compares text strings lexicographically using, for example, the ASCII value of the characters. We cannot compare strings and numbers because we would throw an error.
Bitwise Operators
Bitwise Operators
Operator | Description |
---|---|
& or and | And |
| or or | Or |
^ | Xor |
~ | Bitwise not |
<< | Left shift |
>> | Right shift |
Assignment Operators
Assignment Operators
Operator | Description | Example |
---|---|---|
= | Basic assignment operator | x = 5 + 6 |
+= | Adds an element and assigns the result to the name | x += y (corresponds to x = x + y) |
-= | Subtracts an element and assigns the result to the name | x -= y (corresponds to x = x – y) |
/= | Divides an element and assigns the result to the name | x /= y (corresponds to x = x/y) |
*= | Multiplies an element and assigns the result to the name | x *= y (corresponds to x = x * y) |
%= | Modulo and reassignment | x %= y (corresponds to x = x % y) |
**= | Exponentiation and reassignment | x **= y (corresponds to x = x ** y) |
//= | Floor division and reallocation | x//=y (corresponds to x = x//y) |
Each time, Python performs the operation and records the result in the x object.
Operator Order
Priority Rules for Operators
Operator | Priority (highest to lowest) |
---|---|
** | Exponentiation has the highest priority |
- | Denial |
* / // % | Multiplication, division, modulo, floor division |
+ - | Addition and subtraction |
>> << | Bitwise right and left |
& | Bitwise AND |
^ | | Bitwise OR |
<=, >, <, >= | Less than, more than, smaller, bigger than |
== != | Equal, different |
= += -= *= /= %= **= //= | Assignment operators |
is / is not | Comparison |
in / not in | Comparison |
not / or / and | Comparison |
Indentation
Python uses indentation to limit blocks of instructions, which makes the code more readable. Code blocks are thus defined by indentation. Typically an indentation corresponds to four spaces.
Quotation Marks
We examine string management in Chapter 5.
Summary
In this chapter, we studied commenting and operators. When we write code for data analysis, it is important that we include comments not only as a reminder to ourselves but also as a guide for other programmers. Operators, and their ranking, help us create and define our code.