4 Python Fundamentals
Our learning objectives in this section:
- Learn and use basic functions, methods and packages
-
Explore the most common data containers:
- Lists
- Dictionaries
- NumPy Arrays
Let’s begin with some really basic foundations.
4.1 Key types
There are four key data types that we’ll see in Python:
Type | Name | Example |
---|---|---|
bool |
Binary |
True , False
|
int |
Integer numbers | 7, 9, 2, -4 |
float |
Real numbers | 3.14, 2.78, 6.45 |
str |
String | All Alphanumeric and special characters |
As we’ve already seen, just use the assignment operator, =
, for storing a value in a variable. Let’s take a look at the different types. You can also find out the type of an object using the type()
function.
# Types
= 1
a type(a)
#> <class 'int'>
= 0.25
b type(b)
#> <class 'float'>
= "Berlin"
c type(c)
#> <class 'str'>
= True
d type(d)
#> <class 'bool'>
4.2 Arithmetic operators
Python can perform arithmetic operations using the following operators.
Operator | Description | Use |
---|---|---|
+ |
Addition: adds two operands | x + y |
- |
Subtraction: subtracts two operands | x - y |
* |
Multiplication: multiplies two operands | x * y |
/ |
Division (float): divides the first operand by the second | x / y |
** |
Exponentiation | x ** y |
// |
Division (floor): divides the first operand by the second | x // y |
% |
Modulus: returns the remainder when first operand is divided by the second | x % y |
There are many operators in Python. An operator is simply a little function that takes something on the left, something on the right and performs some kind of function with them.
Exercise 4.1 (Differences in handling types) What do expect when executing the following functions?
1 + 1
'1' + '1'
'1' * 5
'1' '1'
Notice the curious case of adding str
objects.
= "hello"
w1 = "there"
w2 + w2
w1 #> 'hellothere'
The same holds true for multiplication
*6
w1#> 'hellohellohellohellohellohello'
4.3 Functions & Methods
- Python is an object-oriented programming language. This means that objects have:
- Attributes, and
- Methods
Methods are functions that an object can call on itself, they are class-specific functions. Attributes are characteristics of an object.
We’ll get to creating our own classes later on. For now, let’s understand Functions in Python and see how to apply methods and examine attributes.
4.4 Packages
There are many functions built into Python, but most of what you’ll use in data science comes from a suite of packages that we installed and imported in the previous case studies. In the literature you’ll find the name package, library and module used interchangeably.
To import a package we use the import
keyword, like we saw in the case study:
import math
What actually happened was this:
from math import *
We implicitly used the *
as a wild-card to import all functions in the math package.
We also saw that we can give a package an alias name that will save us typing later on. These are pretty much standard use for some packages and you shouldn’t change them! To do this use the as
keyword as such:
import numpy as np
import pandas as pd
This means that we are loading the entire NumPy
package and giving it an alias, np
, to make it easier to access. When we want to use a function in NumPy, we need to preface it with np
, or whatever alias we have given. Although we can choose any alias we want, there’s not good reason not to use the standard naming conventions.
We can import a specific part of the package using the from
keyword, like so:
from scipy import stats
So in this case we’re just importing the stats
module from the whole scipy
package. We do this when there are potential conflicts or when it’s large. This makes it a bit confusing, because as you saw previously, we could also read in just a specific function.
from scipy.stats import ttest_ind
from statsmodels.stats.multicomp import pairwise_tukeyhsd
from statsmodels.formula.api import ols
So this is pretty convenient. Why should we read in the whole package when we want just a single function? But it can get very confusing very quickly! Keep to conventions and make sure you have all your import calls at the beginning of your script so that it’s easy to keep track of what your using.
4.5 Calling functions
Functions have a generic form of:
function(args)
For example:
= 8
numb
np.log2(numb)#> 3.0
Notice that we use the .
notation to access a function within a package, keep that in mind because it’s exactly what we’re going to do to access methods. This also means that you can’t use a .
in a variable or function name.
Can you predict what the above function will do?
We didn’t need to define the variable numb
, I just did that to show you that we can use a variable as a place-holder. We could have simply used np.log2(8)
. This is an example of a scalar variable, in that is contains a single value.
Typically, there is more than one way to get an answer, for example:
8, 2)
math.log(#> 3.0
would produce the same result.
4.6 Lists, Data Containers part I
Exercise 4.2 (Mixing different lengths) The first exercise was pretty easy… what would happen in these cases? Try to predict the answer without executing code!
[1, 6, 9, 36] + 100
[1, 6, 9, 36] + [10, 100]
[1, 6, 9, 36] + [100, 200, 300, 400]
[1, 6, 9, 36] + [100, "dogs", 300, 400]
Let’s make a small data set to play with.
= [167, 188, 178, 194, 171, 169] heights
heights
is an example of a list. It’s a builtin type and can take many values of different types. We use []
to create a list. We’ll consider more data containers in detail in the next section.
There are various things we can calculate on the values in a list:
Function | Description |
---|---|
len() |
The number of values, n |
np.mean() |
The mean |
stats.mode() |
The mode |
np.median() |
The median |
np.var() |
The variance |
np.std() |
The standard deviation |
stats.iqr() |
The inter-quartile range |
max() |
The maximum value |
min() |
The minimum value |
range() |
The range |
sum(heights)
#> 1067
len(heights)
#> 6
4.6.1 Exercises
Exercise 4.3 (Functions and math) Given the definition of the Normal 95% confidence interval
\[95\%CI = \bar{x} \pm 1.96 \times s/\sqrt n\] where \(\bar{x}\) is mean, \(s\) is the standard deviation and \(n\) is the number of observations, calculate the 95% confidence interval for the list of heights.
Exercise 4.4 Given the dist
list for some cities:
cities = ['Munich', 'Paris', 'Amsterdam', 'Madrid', 'Istanbul']
dist = [584, 1054, 653, 2301, 2191]
Answer the following questions:
- How many data points are there?
- What is the longest and shortest distance?
- Get the average distance
Exercise 4.5 Using what we’ve seen in the Plant Growth case study:
- Visualize the values in the
dist
list as a univariate strip plot or a histogram
4.7 Making your own functions
Making your own functions is extremely common in Python and understanding how they’re built will help you to understand how they’re used. In this section we’re going to make some small play example functions to get you used to the syntax and concepts.
To produce a function use the def
keyword. Give it a name and then define any arguments inside ()
. All the contents of the function are indented with four spaces. This is very important – remember the PEP8 Style Guide!
def addNumbs(x, y):
z = x + y
return z
A function ends with the return
keyword.
def addNumbs(x, y):
= x + y
z return z
We can call our function as such:
4, 6) addNumbs(
#> 10
Document your functions using Docstrings. This is a short description placed in triple quotes """..."""
directly below your def
call.
def addNumbs(x, y):
"""Add two numbers together"""
= x + y
z return z
You can access a function’s Docstrings as a method, which we’ll encounter later on.
Exercise 4.6 (Creating functions) We already figured out how to calculate the the 95% confidence interval. Here, create a function that takes a list and returns the lower limit. We’ll return both the lower and upper limit in a later exercise. Call the function confInt
. Don’t forget to include function documentation.
4.7.1 Returning multiple values as a tuple
The above functions returned a single value. A function may return multiple values, in which you can imagine that we’d return a list. Typically, functions actually return Tuples. which are like lists (e.g. heights
) in that they can contain multiple values, but in contrast to a list, that are an immutable type – you cannot change the values. Recall that we used []
to make a list, but we use ()
to make a tuple.
In this function, we return a tuple of two numbers.
def mathFun(x, y):
"""Add and subtract two numbers together"""
= (x + y, x - y)
result
return result
print(mathFun(4, 6))
#> (10, -2)
Exercise 4.7 (Returning tuples) Modify the function you made in the previous exercise, confInt
, so that it returns two values, the lower and upper 95% confidence interval for a list.
4.7.2 Inner functions
We can define a function within a function.
# Define three_shouts
def three_shouts(word1, word2, word3):
"""Returns a tuple of strings
concatenated with '!!!'."""
# Define inner
def inner(word):
"""Returns a string concatenated with '!!!'."""
return word + '!!!'
# Return a tuple of strings
return (inner(word1), inner(word2), inner(word3))
# Call three_shouts() and print
'a', 'b', 'c')
three_shouts(#> ('a!!!', 'b!!!', 'c!!!')
Exercise 4.8 (Inner functions) Returning to confInt
, create a function within this function which manually calculates
\[1.96 \times s/\sqrt n\]
and returns the value for a given list. Call this inner function calcErr
. The end result should still be the lower and upper limits of the 95% confidence interval for a list of numbers.
4.7.3 Function generators
We can even have a function return a function when it’s called:
def raise_value(n):
"""return a function"""
def inner(x):
"""raise x to the power of n"""
= x ** n
raised return raised
return inner
Now when we call raise_value()
with a numeric argument, we get a function using that argument as its hard-coded default.
= raise_value(2)
sq 2)
sq(#> 4
= raise_value(3)
cu 2)
cu(#> 8
Exercise 4.9 (Functions that create functions) Create a function that creates functions which take a list of numbers as input and returns the 95% confidence interval calculated according to a t-distribution. For example, the correction factor for a sample of 10 observations would be 2.26 instead of 1.96. We’ll produce functions which calculate the 95% confidence interval using whatever correction we provide.
4.7.4 The LEGB rule
We can access variables from a nested function using the nonlocal
keyword. Here, echo_word
is a variable defined within the function echo_shout()
. It’s used inside the nested function shout()
, but in that function’s environment, it doesn’t exists. To access it, we can call it using the nonlocal
keyword.
# Define echo_shout()
def echo_shout(word):
"""Change the value of a nonlocal variable"""
# Concatenate word with itself: echo_word
= word*2
echo_word
# Print echo_word
print(echo_word)
# Define inner function shout()
def shout():
"""Alter a variable in the enclosing scope"""
# Use echo_word in nonlocal scope
nonlocal echo_word
# Change echo_word to echo_word concatenated with '!!!'
= echo_word + '!!!'
echo_word
# Call function shout()
shout()
# Print echo_word
print(echo_word)
# Call function echo_shout() with argument 'hello'
'hello')
echo_shout(#> hellohello
#> hellohello!!!
If we want to access a variable from the global environment within a function, we can do so using the global
keyword. Python searches the namespaces in the following order: Local
, Enclosing
, Global
, Built-in
(aka the LEGB rule).
4.7.5 Default arguments and flexible arguments
Often, you’ll want to specify a default value for a function. You an do this by simply calling assigning with with the =
operator.
# Define shout_echo
def shout_echo(word1, echo = 1):
"""Concatenate echo copies of word1 and three
exclamation marks at the end of the string."""
# Concatenate echo copies of word1 using *: echo_word
= word1*echo
echo_word
# Concatenate '!!!' to echo_word: shout_word
= echo_word + '!!!'
shout_word
# Return shout_word
return shout_word
# Call shout_echo() with "Hey": no_echo
= shout_echo('Hey')
no_echo
# Call shout_echo() with "Hey" and echo=5: with_echo
= shout_echo('Hey', 5)
with_echo
# Print no_echo and with_echo
print(no_echo)
#> Hey!!!
print(with_echo)
#> HeyHeyHeyHeyHey!!!
Exercise 4.10 (Defining function defaults) Returning to confInt
with the nested calcErr
function (not generating a function), modify the function so that we can input the error corrrection as an argument.
4.7.6 Many unnamed arguments
If you want to allow many arguments, you can use *args
to returns all arguments as a tuple. Here, we have a function that allows us to add many values together. We’ll get into loop later on, if you’re not sure what’s happening there, don’t worry.
def add_all(*args):
"""sum all values in *args together."""
# Initialize sum
= 0
sum_all
# Accumulate the sum
for num in args:
+= num
sum_all
return sum_all
print(add_all(2,4,5))
#> 11
Exercise 4.11 Modify the previous confInt
function so that it accepts many lists instead of a single list. It should combine all the values into one large list.
4.7.7 Many named arguments
We can even allow for many arguments that have names by using **kwargs
. This is for argument’s proceeded by identifiers (key-word arguments, hence kwargs
). Again, don’t worry about the for loop if you’re not familiar with it, we’ll get there later on.
# Define report_status
def report_status(**kwargs):
"""Print out the status of a document."""
print("\nBEGIN: REPORT\n")
# Iterate over the key-value pairs of kwargs
for key, value in kwargs.items():
# Print out the keys and values, separated by a colon ':'
print(key + ": " + value)
print("\nEND REPORT")
# First call to report_status()
type="proposal", affiliation="Charite", status="incomplete")
report_status(
# Second call to report_status()
#>
#> BEGIN: REPORT
#>
#> type: proposal
#> affiliation: Charite
#> status: incomplete
#>
#> END REPORT
type="grant", affiliation="MPI", status="approved")
report_status(#>
#> BEGIN: REPORT
#>
#> type: grant
#> affiliation: MPI
#> status: approved
#>
#> END REPORT
Note that in the above example \n
is a special escape character which represents a carriage return, so that we begin the next output on a new line.
4.7.8 The lambda keyword
Sometimes, we don’t want to actually name a function, but just use it on the fly. For this we have the lambda
keyword. this is really convenient because it makes code most shorter, we can do everything in one line. This lambda function takes two arguments, x
and y
.
= lambda x, y: x ** y
raise_to_power 2, 3)
raise_to_power(#> 8
Can you see the relationship to a classical function definition:
def raise_to_power(x, y):
"""exponents"""
return x ** y
Let’s take a look at some useful use cases of lambda functions
.
4.7.8.1 map()
map()
takes two arguments. The first is a function that will be applied to all elements of the second object. Here, lambda functions can be passed to map without even having a name, i.e. they are anonymous functions.
= [3, 5, 6, 8, 3]
nums = map(lambda num: num ** 2, nums) square_all
# This returns a "map" object
square_all#> <map object at 0x283c251c0>
# so we need to convert it to a list
list(square_all)
#> [9, 25, 36, 64, 9]
4.7.8.2 filter()
The filter()
function allows you to quickly filter a list according to some specific criteria. Here we use the len()
function to get all the values larger than 6 characters. We’ll see more relational operators later on.
# Create a list of strings: fellowship
= ['frodo', 'samwise', 'merry', 'pippin', 'aragorn', 'boromir', 'legolas', 'gimli', 'gandalf']
fellowship
# Use filter() to apply a lambda function over fellowship: result
= filter(lambda member: len(member) > 6, fellowship) result
result#> <filter object at 0x283c25be0>
# We have to convert to a list
list(result)
#> ['samwise', 'aragorn', 'boromir', 'legolas', 'gandalf']
4.7.9 reduce()
The reduce()
function is useful for performing some computation on a list and, unlike map()
and filter()
, returns a single value as a result. To use reduce(), you must import it from the functools
module.
# Import reduce from functools
from functools import reduce
# apply reduce to the fellowship list, see above
= reduce(lambda item1, item2: item1 + item2, fellowship) result
# Print the result
result#> 'frodosamwisemerrypippinaragornboromirlegolasgimligandalf'
list(result)
#> ['f', 'r', 'o', 'd', 'o', 's', 'a', 'm', 'w', 'i', 's', 'e', 'm', 'e', 'r', 'r', 'y', 'p', 'i', 'p', 'p', 'i', 'n', 'a', 'r', 'a', 'g', 'o', 'r', 'n', 'b', 'o', 'r', 'o', 'm', 'i', 'r', 'l', 'e', 'g', 'o', 'l', 'a', 's', 'g', 'i', 'm', 'l', 'i', 'g', 'a', 'n', 'd', 'a', 'l', 'f']
4.7.10 Catching errors
An error in execution is called an exception. To catch an exception use a try-except
clause. i.e. if the code after try can be executed then proceed, if not execute the code in the except, which can be an error message contained in a print()
function.
def sqrt(x):
try:
return x ** 5
except:
print('nope')
3)
sqrt(#> 243
3.6)
sqrt(#> 604.6617600000001
'yes')
sqrt(#> nope
When the argument is not a number, we get an error, so the best case it to return a TypeError. The wrong type of a variable is one of the most common errors.
def sqrt(x):
try:
return x ** 5
except TypeError:
print('nope, you have the wrong type')
3)
sqrt(#> 243
3.6)
sqrt(#> 604.6617600000001
'yes')
sqrt(#> nope, you have the wrong type
Exercise 4.12 Add a TypeError to the calcInt
function that returns a message if a list of strings is used as input.
Another type of error is the ValueError
, when the given value is the right type, but the wrong value. We can raise an error on purpose by using a Raise
keyword.
def sqrt(x):
if x < 0:
raise ValueError('x must be positive')
try:
return x ** 5
except TypeError:
print('nope, you have the wrong type')
# sqrt(-9)
Exercise 4.13 Add a ValueError to the calcInt
function that returns a message if the maximum value is larger than 100.
4.8 String formatting
Use f-string, f
or F
, to format strings for output.
= "Rick"
name = "Misk Academy"
place
# use {} to display its value in the output
print(f"{name} is visiting the folks at {place}.")
## arithmetic expression
#> Rick is visiting the folks at Misk Academy.
print(f"{23 * 6}")
#> 138
= "Mr."
salutation def greet(name, salutation):
return "Hello, " + salutation + " " + name
print(f"{greet(name, salutation)}, how's it going?")
#> Hello, Mr. Rick, how's it going?
print(greet(name, salutation))
#> Hello, Mr. Rick
Exercise 4.14 (Composing sentences) Sometimes it’s really useful to include a sentence as output to a function, especially if you have a function that is just reporting a value or inside a loop (more on that later) which you need to see how it changes. Returning to the confInt
function, add a line at the end which return a sentence “There is a 95% chance the the true population parameter is covered by the region [XX, YY]” where XX and YY are the lower and upper limits of the 95% confidence interval, respectively.
4.9 Methods
4.9.1 Dot notation
Python uses .
notation, where periods have very specific meanings and uses. They call methods that are available. If you want to see the methods available to an object, you can just type .
and IntelliSense should auto-complete with a list of possibilities.
For example, this does not work:
l.len() # not a method
because methods are functions that an object calls, whereas you pass objects to functions. Here we pass l
a list to the function len()
, rather than calling the method len()
on l
using .
notation. Get it?
Let’s take a look at an example, append a list.
Call the append method on the list l
using the dot notation.
= [1, "2", True]
l 'appended value')
l.append( l
#> [1, '2', True, 'appended value']
So your not passing l
to the append()
function, rather you’re calling the append()
method on list l
. The append method modifies the object l
by appending a new value to it.
Methods are special functions that belong to specific objects. For example, we have specific methods for strings:
= "Berlin"
name
name.lower()#> 'berlin'
name.upper()#> 'BERLIN'
You can get a list of valid methods by calling dir()
on a variable. For example, a numeric list has the following:
For example:
# As above:
= [584, 1054, 653, 2301, 2191] dist
560)
dist.append(
dist#> [584, 1054, 653, 2301, 2191, 560]
dist.sort()
dist#> [560, 584, 653, 1054, 2191, 2301]
3)
dist.pop(#> 1054
dist#> [560, 584, 653, 2191, 2301]
653)
dist.remove(
dist#> [560, 584, 2191, 2301]
Exercise 4.15 In class we’ll explore some of the uses. Write examples for the remaining methods listed above.
One of the most fundamental things in any language is know what type of data you are working with. If you have they wrong type, you can’t calculate the things that you want to, so you’ll need to coerce into a different type, for example str()
is used to create a string.
# coercion
= 8
numb type(numb)
#> <class 'int'>
= str(numb)
numb type(numb)
#> <class 'str'>
Similarly, we can coerce to an integer
type(numb)
#> <class 'str'>
= int(numb)
numb type(numb)
#> <class 'int'>
or a float
= float(numb)
numb type(numb)
#> <class 'float'>
4.10 Attributes, part I
There are functions and methods, and last, we have attributes. Attributes are characteristics of a variable. They are also called with a .
notation. e.g.
__len__
l.#> <method-wrapper '__len__' of list object at 0x283c34dc0>
The ()
are optional for attributes. A variable that has a __len__
attribute can be accessed by using the len()
function, which is way easier.
len(l)
#> 4
Remember the docstrings from our functions earlier? Those are attributes and can be accessed using the following attribute
addNumbs.__doc__
## 'Add two numbers together'
The __
notation is pronounced dunder, i.e. “double underscore”. We’ll see this in more detail in the OOP chapter.
4.11 Dictionaries, Data Containers part II
Lists are fine, but we need some more elegant solutions. That’s where dictionaries, arrays (i.e. matrices) and DataFrames come into play. Dictionaries are a builtin data container, but arrays and DataFrames are not in Python by default. They come from NumPy
(arrays and matrices) and pandas
(DataFrame).
Let’s begin with dictionaries. Lists are, for the most part, 1-dimensional. Dictionaries (aka associative arrays) are 2-dimensional & heterogeneous, like many data sources you’ll encounter.
Lists are unlabeled, but dictionaries provide key:value
pairs using {}
and :
.
= {'int_value':3,
d 'bool_value':False,
'str_value':'hello'}
print(d) #
#> {'int_value': 3, 'bool_value': False, 'str_value': 'hello'}
print(d['str_value'])
#> hello
= {'name': ['Volkswagen', 'Daimler', 'Allianz', 'Charite'],
organizations 'structure': ['company', 'company', 'company', 'research']}
print(organizations['name'])
#> ['Volkswagen', 'Daimler', 'Allianz', 'Charite']
We can put two lists together to create a dictionary. We do this using dict(zip())
.
= [167, 188, 178, 194, 171, 169]
heights = ["Mary", "John", "Kevin", "Elena", "Doug", "Galin"]
persons
= dict(zip(persons, heights)) heights_persons
There are specific methods to use here also:
heights_persons.values()#> dict_values([167, 188, 178, 194, 171, 169])
heights_persons.keys()#> dict_keys(['Mary', 'John', 'Kevin', 'Elena', 'Doug', 'Galin'])
Notice that the order when printing is different than in definition. Order is not guaranteed! So don’t rely on the printed order of the keys.
= [0, 1, 2, 3, 4]
l
l#> [0, 1, 2, 3, 4]
= {'int_value':3, 'bool_value':False, 'str_value':'hello'}
d
d#> {'int_value': 3, 'bool_value': False, 'str_value': 'hello'}
Exercise 4.16 Returning to the two lists containing the cities and their distances from Berlin, above, create a dictionary with two key/value pairs: city
, containing a list of five cities, and distance
, containing a list of 5 integers. Assign this to the variable distDict
.
cities = ['Munich', 'Paris', 'Amsterdam', 'Madrid', 'Istanbul']
dist = [584, 1054, 653, 2301, 2191]
4.12 NumPy Arrays, Data Containers part III
Many of the popular packages we use for data science are built on top of NumPy
.
Here we can use matrices and vectors as data structures, which is exactly what we would like for data science, since most of our data is laid out in tables.
Many mathematical operations are based on matrices, called NumPy arrays, in Python you’ll see this as an ndarray
. This just means data grouped in rows and columns.
Arrays are similar to lists, but are more dynamic:
- They can be n-dimensional. The
nd
inndarray
stands for n-dimensional, we’ll only be dealing with 1 and 2-dimensional arrays in this workshop. - They only take one data type!
# lists
= [3, 8, 9, 23]
xx print(xx)
#> [3, 8, 9, 23]
type(xx)
#> <class 'list'>
# but what you ureally want is a NumPy array
import numpy as np
= np.array([3, 8, 9, 23])
xx
xx#> array([ 3, 8, 9, 23])
NumPy
arrays come in all shapes and sizes:
5,7,8,9,3],
np.array([[0,3,6,8,2],
[range(5)])
#> array([[5, 7, 8, 9, 3],
#> [0, 3, 6, 8, 2],
#> [0, 1, 2, 3, 4]])
When we refer to the rows as axis 0, and the columns as axis 1.
Here we introduce a new function, range()
. Can you guess what it does?
The primary reason we use NumPy arrays is to vectorize functions over all values. Let’s take a look at other common functions from NumPy
that do this really well in table 4.1.
4.13 Some references
4.13.1 Builtin functions and types
A summary of the Builtin functions and types is available on the help pages. Here is a summary of some common functions and types.
Function | Description |
---|---|
abs() |
Return the absolute value of a number |
all() |
Return True if all elements of the iterable are true |
any() |
Return True if any element of the iterable is true |
_class_ bool() |
Return a Boolean value, i.e. one of True or False |
@classmethod() |
Transform a method into a class method |
dict() |
Create a new dictionary |
dir([object]) |
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object |
enumerate(iterable, start=0) |
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. |
filter(function, iterable) |
Construct an iterator from those elements of iterable for which function returns true. |
_class_ float() |
Return a floating point number constructed from a number or string x |
hash(object) |
Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary look-up |
help([object]) |
Invoke the built-in help system |
input([prompt]) |
Get user input |
_class_ int() |
Return an integer object constructed from a number or string x, or return 0 if no arguments are given |
iter() |
Return an iterator object. |
len() |
Return the length (the number of items) of an object. |
_class_ list() |
a mutable sequence type, see here |
max() |
Return the largest item in an iterable or the largest of two or more arguments |
min() |
Return the smallest item in an iterable or the smallest of two or more arguments |
next() |
Retrieve the next item from the iterator |
print() |
Print objects |
range() |
an immutable sequence type, see here |
repr() |
Return a string containing a printable representation of an object |
reversed() |
Return a reverse iterator |
round() |
Return number rounded to ndigits precision after the decimal point |
_class_ slice(start, stop[, step]) |
Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None |
sorted() |
Return a new sorted list from the items in iterable |
@staticmethod() |
Transform a method into a static method |
sum(iterable[, start]) |
Sums start and the items of an iterable from left to right and returns the total |
tuple() |
an immutable sequence type, see here |
_class_ type() |
return the type of an object |
zip() |
Make an iterator that aggregates elements from each of the iterables |
4.13.2 keywords
We saw a lot of key words already. we won’t use them all the time, but here is a list
import keyword
keyword.kwlist#> ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
4.14 Wrap-up
So far in our Python journey we’ve covered the core basics of working
with data. We saw packages, functions, modules and attributes for
calculating and accessing information. In addition we now know the four
main types in Python (bool
, int
,
float
and str
) and the most common data
containers (lists, dictionaries, NumPy arrays and dictionaries).