Python for Data Science Flashcards
True or False
The IPython Shell is typically used to work with Python interactively
TRUE
Which file extension is used for Python script files?
.py
Python scripts have the extension .py. my_analysis.py is an example of a script name.
You need to print the result of adding 3 and 4 inside a script. Which line of code should you write in the script?
print(3 + 4)
If you do a calculation in the IPython Shell, the result is immediately printed out. If you do the same thing in the script and run it, this printout will not occur.
In Python 3, you will need print(3 + 4). You need to explicitly include this print() function; otherwise the result will not be printed out when you run the script.
Python as a calculator
Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:
Exponentiation: **. This operator raises the number to its left to the power of the number to its right: for example 4**2 will give 16.
Modulo: %. It returns the remainder of the division of the number to the left by the number on its right, for example 18 % 7 equals 4.
Which line of code creates a variable x with the value 15?
x = 15
In Python, variables are used all the time. They make your code reproducible.
You use a single equals sign to create a variable and assign a value to it.
What is the value of the variable z after executing these commands?
x = 5
y = 7
z = x + y + 1
In the command z = x + y + 1, x has the value 5 and y has the value 7. 5 + 7 + 1 equals 13.
You execute the following two lines of Python code:
x = “test”
y = False
You can recognize strings from the quotes. Booleans can either be True or False.
What is a list?
A list is a way to give a single name to a collection of values. These values, or elements, can have any type; they can be floats, integer, booleans, strings, but also more advanced Python types, even lists.
Which of the following is a characteristic of a Python list?
It is a way to name a collection of values, instead of having to create separate variables for each element.
Which three Python data types does this list contain?
x = [“you”, 2, “are”, “so”, True]
“you”, “are” and “so” are strings. 2 is an integer and True is a boolean.
Which command is invalid Python syntax to create a list x?
x = [“this”, “is”, “a” True “list”]
Only the first command will result in a so-called SyntaxError, because there are no commas before and after True.
Create list with different types
A list can contain any Python type.
Although it’s not really common, a list can also contain a mix of Python types including strings, floats, booleans, etc.
How does python access elements in a list?
By using an index
What is slicing?
Apart from indexing, there’s also something called slicing, which allows you to select multiple elements from a list, thus creating a new list. You can do this by specifying a range, using a colon.
eg: x[2 : 8]
Which pair of symbols do you need to do list subsetting in Python?
You use square brackets to subset lists in Python.
What Python command should you use to extract the element with index 1 from a Python list x?
x[1] or x[1] or x[1] or x[1]
You use square brackets for subsetting. Inside the square brackets, simply put the the index of the element you want to access.
You want to slice a list x. The general syntax is:
x[begin:end]
List slicing is a very powerful technique to extract several list elements from a list at the same time.
In Python, the begin index is included in the slice, the end index is not.
What is manipulating lists consist of?
- Changing elements
- Adding elements
- Removing elements
You have a list x that is defined as follows:
x = [“a”, “b”, “b”]
You need to change the second “b” (the third element) to “c”.
Which command should you use?
x[2] = “c”
The third element has index 2. You want to change this element with a string, so you need “c” instead of c.
You have a list x that is defined as follows:
x = [“a”, “b”, “c”]
Which line of Python code do you need to add “d” at the end of the list x?
x = x + [“d”]
You basically have to create a single-element list containing “d” and add that to the list x.
Next you have to assign the result of this addition to x again to actually update x for the future.
You have a list x that is defined as follows:
x = [“a”, “b”, “c”, “d”]
You decide to remove an element from it by using del:
del(x[3])
How does the list x look after this operation?
[“a”, “b”, “c”]
The operation removed the element with index 3, so the fourth element in the list x.
But what is a function?
Simply put, a function is a piece of reusable code, aimed at solving a particular task.
You can call functions instead of having to
write code yourself.
What is a Python function?
A piece of reusable Python code that solves a particular problem.
A function are a block of code, that perform a specific, related action. Functions make your code more modular, so that you can reuse code without having to retype it over and over again.
What Python command opens up the documentation from inside the IPython Shell for the min function?
You can use help(min). Notice that help() is also a function!
What are methods?
You can think of methods as _functions_ that “belong to” Python objects.
A Python object of type string has methods, such as capitalize and replace, but also objects of type float and list have specific methods depending on the
type.
In Python, everything is an object, and each object has specific________________________
method associated.
Different objects may have the same methods but_____________________________
depending on the type of the object, the methods behave differently.
eg. index() exists for both strings and lists
Some methods can change___________
the objects they are called on.
What is append() in Python?
append() is a method, and therefore also a function.
In Python, practically everything is an object. Every python object can have functions associated. These functions are also called methods.
You have a string x defined as follows:
x = “monty python says hi!”
Which Python command should you use to capitalize this string x?
x.capitalize()
Use the dot notation to call a method on an object, x in this case. Make sure to include the parentheses at the end, even if you don’t pass any additional arguments.
How does the list x look after you execute the following two commands?
x = [4, 9, 5, 7]
x.append(6)
[4, 9, 5, 7, 6]
If you call append() on a list, you’re actually adding the element to the list you called append() on; there’s no need for an explicit assignment (with the = sign) in this case.
What are Packages?
You can think of package as a directory of python scripts. Each such script is a so-called module. These modules specify functions, methods and new Python types aimed at solving particular problems
Are all packages available in Python by default.
Yes or No
No
How to use Python packages?
To use Python packages, you’ll first have to install them on your system, and then put code in your script to tell Python that you want to use these packages.
What are the main python packages for:
- Data Science
- Data Visualization
- Machine Learning
- data science: there’s numpy (toefficiently work with arrays)
- matplotlib for data visualization,
- scikit-learn for machine learning