Tables and lists for those moments when you need to quickly find a bit of Python information.
Table 2 shows the built-in functions that come with the Python interpreter upon installation.
Table 2. Python’s built-in functions1
abs() |
enumerate() |
issubclass() |
range() |
all() |
eval() |
iter() |
repr() |
any() |
exec() |
len() |
reversed() |
ascii() |
filter() |
list() |
round() |
bin() |
float() |
locals() |
set() |
bool() |
format() |
map() |
setattr() |
bytearray() |
frozenset() |
max() |
slice() |
bytes() |
getattr() |
memoryview() |
sorted() |
callable() |
globals() |
min() |
staticmethod() |
chr() |
hasattr() |
next() |
str() |
classmethod() |
hash() |
object() |
sum() |
compile() |
help() |
oct() |
super() |
complex() |
hex() |
open() |
tuple() |
delattr() |
id() |
ord() |
type() |
dict() |
input() |
pow() |
vars() |
dir() |
int() |
print() |
zip() |
divmod() |
isinstance() |
property() |
__import__() |
1Source: Built-in functions. (2015, November 23). Retrieved November 16, 2015 from https://docs.python.org/3/library/functions.html
Table 3 shows Python’s keywords (aka, reserved words). Each of these serves a special purpose, so remember that they can’t be used for variable names (Gaddis, 2015).
Table 3. Python keywords1
and |
del |
from |
None |
True |
as |
elif |
global |
nonlocal |
try |
assert |
else |
if |
not |
while |
break |
except |
import |
or |
with |
class |
False |
in |
pass |
yield |
continue |
finally |
is |
raise |
|
def |
for |
lambda |
return |
1Source: Gaddis, T. (2015). Starting out with python. Boston: Pearson.
Table 4 shows Python’s math, relational and logical operators. These can be used to do various things such as perform math operations, write statements, write Boolean expressions, or create control structures.
Table 4. Python operators1
Operator | Name/Description | Type |
---|---|---|
+ |
Additon | Math |
- |
Subtraction | Math |
* |
Multiplication | Math |
/ |
Division | Math |
// |
Integer division, returns a whole number quotient. Either drops the fractional part if the quotient is positive or rounds away from zero if the quotient is negative. | Math |
% |
Modulus, divides two numbers but only returns the remainder of the quotient. | Math |
** |
Exponent | Math |
> |
Greater than | Relational |
>= |
Greater than or equal to | Relational |
< |
Less than | Relational |
<= |
Less than or equal to | Relational |
== |
Equal to, returns true if the operands to its left and right are equal to each other. Otherwise, it returns false. | Relational |
!= |
Not equal, returns true if the operands to its left and right are not equal to each other. Otherwise, it returns false. | Relational |
and |
Returns true if the Boolean expressions to its left and right are both true. Otherwise, it returns false. | Logical |
or |
Returns true if at least one of the Boolean expressions to its left and right is true. If neither is true, it returns false. | Logical |
not |
Only evaluates one Boolean expression to its right for its opposite truth. Returns true if the expression is false. Returns false if the expression is true. | Logical |
1Source: Gaddis, T. (2015). Starting out with python. Boston: Pearson.
The order of operations in Python is similar to that which you probably learned in math class, except that some operations are grouped into tiers. The following list shows the order in which math operations are performed in Python (Gaddis, 2015):
()
**
*
/
//
%
+
-
Table 5 shows some methods and functions that can be used to modify lists. The table’s examples work with the following list:
Table 5. Methods and functions for modifying lists1
Method or Function | Result | Description |
---|---|---|
my_list.append(10) |
[11, 12, 3, 4, 5, 10] |
This method appends an item to the list’s end. |
my_list.insert(2, 10) |
[11, 12, 10, 3, 4, 5] |
This method inserts an item (the second argument) into a specific index (indicated by the first argument). |
my_list.remove(5) |
[11, 12, 3, 4] |
This method looks for an item and removes it from the list. |
my_list.index(3) |
2 |
This method returns the index where an item is found in the list. |
my_list.sort() |
None |
Nothing is returned, but the list is sorted in ascending order. |
my_list.reverse() |
None |
Nothing is returned, but the list is sorted in descending order. |
del my_list[1] |
[11, 3, 4, 5] |
This function deletes an item from a specific index in the list. |
min(my_list) |
3 |
Returns the largest value from the list. |
max(my_list) |
12 |
Returns the smallest value from the list. |
1Source: Gaddis, T. (2015). Starting out with python. Boston: Pearson.
You can format floating-point numbers, integers, numbers in scientific notation, and percentages by using the built-in format()
function. The format()
function accepts two arguments: a number and a notation (within quotes) which specifies how to format the number. Table 6 shows some examples of how this function works.
Table 6. Examples of the various uses of the format()
function1
Example format() |
Result | Description |
---|---|---|
format(1111.9876, '.2f') |
111.99 |
.2 specifies the decimal place to which the number should be rounded. This number can be changed depending on your needs. Meanwhile, f specifies that its a floating point number. |
format(111.9876, ',f') |
1,111.9876 |
The comma in the second argument indicates that the number should be comma separated. |
format(1.9876, '15f') |
1.9876 |
15 specifies the minimum number of spaces that the number should occupy. This number can be changed depending on your needs. |
format(1111.9876, '15,.2f) |
1,111.99 |
This shows a combination of all of the above formats. Any combination of the above can be used depending on your needs. |
format(1111, ',d') |
1,111 |
The comma in the second argument indicates that the number should be comma separated. The d indicates that the number is an integer. |
format(1111, '15,d') |
1,111 |
15 specifies the minimum number of spaces that the integer should occupy. This number can be changed depending on your needs. |
format(0.9876, '.2%') |
98.76% |
% multiplies the number by 100 and appends a percent sign to the end. .2 specifies the decimal place to which the floating-point number should be rounded; this can be omitted or modified to fit your needs |
1Source: Gaddis, T. (2015). Starting out with python. Boston: Pearson.
A slice is a portion of a sequence (Gaddis, 2015). Slices can be taken from lists, tuples or strings using the sequence index numbering system (in which the first item is numbered 0). The general syntax for a slice looks something like:
Table 7 reflects slices of the following list and string variables.
Table 7. Examples of sequence slices1
Example slice format | Result | Description |
---|---|---|
my_list[1:5] |
[11, 12, 13, 14] |
The slice starts at index 1 in the sequence. It goes up to, but does not include, index 5. |
str_var[1:5] |
'his ' |
In string sequences, spaces are part of the index numbering system. |
my_list[1:5:2] |
[11, 13] |
Here, 2 indicates that the slice should start at index 1 and retrieve every other value. |
str_var[1:10:2] |
'hsi y' |
(See previous description) |
my_list[1:] |
[11, 12, 13, 14, 15] |
A blank end value returns everything all the way to the sequence’s end. |
str_var[8:] |
'my string.' |
(See previous description) |
my_list[:5] |
[10, 11, 12, 13, 14] |
A blank start value returns everything from the beginning of the sequence up to the specified index value. |
str_var[:11] |
'This is my ' |
(See previous description) |
my_list[:] |
[10, 11, 12, 13, 14, 15] |
Blank start and end values return everything in the sequence. |
str_var[:] |
'This is my string.' |
(See previous description) |
my_list[-5:-1] |
[11, 12, 13, 14] |
Negative values represent positions relative to the sequence’s end. |
str_var[-10:] |
'my string.' |
(See previous description) |
my_list[2:8] |
[12, 13, 14, 15] |
The end value is greater than the length of the list. So, the list’s length is used as the end. |
str_var[8:30] |
'my string.' |
(See previous description) |
my_list[5:2] |
[] |
An empty list is returned if the start value is greater than the end value. |
str_var[10:2] |
'' |
(See previous description) |
1Source: Gaddis, T. (2015). Starting out with python. Boston: Pearson.
The following rules must be followed when naming variables in your code.
Built-in functions. (2015, November 23). Retrieved November 16, 2015 from https://docs.python.org/3/library/functions.html
Gaddis, T. (2015). Starting out with python. Boston: Pearson.
Langley, N. (2003). Python programs for beginners. Computer Weekly, 40.
[Untitled diagram of the three programming structures]. (n.d.). Retrieved from http://docs.oracle.com/cd/A87860_01/doc/appdev.817/a77069/03_struc.htm