1 Introduction to Python (1) Flashcards
Python basics , Numpy... Colab ipynb: https://colab.research.google.com/drive/18d3Tb0gSaOFfbUWL5yYlgYmaS9j-wOYs?usp=sharing
1 What is Python?
A general-purpose programming language
2 What is Data Science?
An interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from data
3 Data Science as an intersection of three fields. Which ones?
- Computer Science IT
- Math and Statistics
- Domains/Business Knowledge
4 What is an algorithm ?
A finite sequence of computer-implementable instructions
5 What is a Python script?
A collection of commands in a file designed to be executed like a program
6 What is a Shell?
A software program that interprets commands from the user so that the operating system can understand them and perform the appropriate functions. The shell is a command-line interface, which means it is solely text-based
7 Outcome of:
print(10/2)
5
8 How to write exponentiation?
**
2**3=8
9 What does % Modulo?
Returns the remainder of a division
18 % 7 =5
10 What is a variable assignment
A name attached to a particular object
variable_a= 18
11 Create a variable ‘savings’ with value 100 and print it
savings = 100
print(savings)
12 Write a script to compound these variables:
savings =100
growth_multiplier = 1.1
t=1
result = savings*growth_multiplier**t
result
13 What is type conversion?
An object from one data type to another data type
14 Outcome of:
type(‘a’)
str
15 Outcome of:
type(False)
bool
16 Outcome of:
type(3)
int
17 Outcome of:
type(3.3)
float
18 Complete code:
’'’savings = 100
result =savings*1.10**7
print(…)
Outcome:
I started with $100 and now I have 194.9
savings = 100 savings = 100 result = round(savings * 1.10**7,1) print('I started with $' + str(savings) \+ ' and now I have $' +str(result))
19 What is a list?
A data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item.
Lists are defined by having values between square brackets [ ]
20 Create a list called house with elements ‘room, kitchen’
#Outcome: ['room', 'kitchen']
house = [‘room’, ‘kitchen’]
house
21 Can a list contain different types of data such as a string and a float?
Yes
[1+2,’a’*5]
22 What is a list of lists or a multi-dimensional list?
A list inside a list (nested)
23 Use list a get this outcome:
[[‘a’,’b’],’c’]
m_list=[[‘a’,’b’],’c’]
m_list
24 What is subsetting?
The process of retrieving just the parts of large files
25 Get the output (2 ways)
x= [‘a’, ‘b’ , ‘c’, ‘d’]
x[1];x[-3]
26 What is an index in a list?
Refers to a position within an ordered list
27 Subset and operate
x= [‘a’, ‘b’ , ‘c’, ‘d’]
print(x[1]+x[2])
x[1]+x[2]
28 What is slicing?
Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists
29 Slice
x= [‘a’, ‘b’ , ‘c’, ‘d’]
x[1:3]
30 Slice (2 ways)
x= [‘a’, ‘b’ , ‘c’, ‘d’]
print(x[1:3])
print(x[-3:-1])
31 Subsetting lists of lists
x=[[‘a’, ‘b’, ‘c’ ],
[‘d’,’e’, ‘f’]]
print(x[1][0])
32 Replace list elements
x = [‘a’, ‘b’, ‘c’,’d’]
x[2:]= [7,8]
x
33 Code to extend the list
x = ['a', 'b', 'c','d'] y= x+(...code..)
x = [‘a’, ‘b’, ‘c’,’d’]
y = x + [‘e’, ‘f’]
print(y)
34 Delete list elements
x = [‘a’, ‘b’, ‘c’,’d’]
del(x[1])
print(x)
35 What is ‘;’ used for?
To place commands on the same line
command1; command2
36 What is # used for?
A comment in Python starts with the hash character, # , and extends to the end of the physical line
#This is a comment print('a')
37 Outcome of:
x = [‘a’, ‘b’, ‘c’,’d’]
y = x
del(y[0])
print(x)
[‘b’, ‘c’, ‘d’]
38 Outcome of:
x = [‘a’, ‘b’, ‘c’,’d’]
y = list(x)
del(y[0])
print(x)
[‘a’, ‘b’, ‘c’, ‘d’]
Outcome: 4
#39 Get number of elements: x = ['a', 'b', 'c','d']
len(x)
40 Function to convert to string
str()
41 Function to convert to integer
int()
42 Function to convert to float
float()
43 Function to convert to boolean
bool()
44 Outcome of:
a= 3.4
str(a)
3.4’
45 Outcome of:
a= 3.4
int(a)
3
46 Outcome of:
a= 3.4
bool(a)
TRUE
47 Think of two ways to get information about a function
help(function)
?function
48 Code (look at the order of the elements)
x = [‘z’,’a’, ‘c’,’d’]
sorted(x,reverse=True)
49 Three arguments in the function sorted()
sorted(iterable,key,reverse)
50 What is an iterable in python
When an object is said to be iterable, it means that you can step through (i.e. iterate) the object as a collection
51 Code (look at the order of the elements)
x = [“cccc”, “b”, “dd”, “aaa”]
sorted(l, key =len)
52 Code (string method)
my_house =’My house’
my_house.upper()
53 Code (string method)
#Count letter 'o' p='pool'
Outcome: 2
p.count(‘o’)
54 What is a method in python?
A function that “belongs to” an object
55 Get the position of c (list method)
x=[‘a’, ‘b’, ‘c’ ]
x.index(‘b’)
56 How many a in the list? (list method)
x=[‘a’, ‘b’, ‘c’ ,’a’]
x.count(‘a’)
57 Add d (list method)
x=[‘a’, ‘b’, ‘c’ ]
x.append(‘d’)
print(x)
58 Delete c (list method)
x=[‘a’, ‘b’, ‘c’ ]
x.remove(‘c’)
print(x)
59 Change order (list method)
x=[‘a’, ‘b’, ‘c’ ]
x.reverse()
print(x)
60 Transform it into a numpy array
baseball = [180, 215, 210, 210]
#Outcome: array([180, 215, 210, 210, 188])
import numpy as np
np_baseball =np.array(baseball)
np_baseball
61 What is a numpy array?
A grid of values, all of the same type, and is indexed by a tuple of nonnegative integers
62 What is a tuple in python?
An immutable sequence of Python objects
2,3,6,9
63 Code
baseball = [180, 215, 210, 210]
import numpy as np
np_baseball=np.array(baseball)
type(np_baseball)
64 Code
baseball = [180, 215, 210, 210]
import numpy as np
np_baseball=np.array(baseball)
print(type(np_baseball))
65 Code
baseball = [180, 215, 210, 210]
print(np_baseball*10)
66 Complete code (subset arrays)
’'’height = [180, 215, 210]
height_np=…
tall = …>200
print(…)’’’
import numpy as np
height = [180, 215, 210]
height_np=np.array(height)
tall = height_np>200
print(height_np[tall])
67 Why do we do vectorization?
Vectorization is the process of converting an algorithm from operating on a single value at a time to operating on a set of values (vector) at one time
68 What is type coercion?
Coercion is the implicit conversion of an instance of one type to another during an operation which involves two arguments of the same type.
69 What happens if one tries to build a numpy array with different data types?
If you try to build such a list, some of the elements’ types are changed to end up with a homogeneous list. This is known as type coercion.
70 Transform it into a 2D array and show its dimensions (rows,columns)
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
import numpy as np
np_baseball=np.array(baseball)
np_baseball.shape
71 Difference between method and attribute in python
Attributes are the features of the objects or the variables used in a class whereas the methods are the operations or activities performed by that object defined as functions in the class.
Attribute = np_ar.shape
Method =np_ar.sum()
72 Code (Subset 2D NumPy arrays)
x = [[“a”, “b”], [“c”, “d”]]
import numpy as np
np_x = np.array(x)
print(np_x[:,0])
73 Code
x = [[“a”, “b”], [“c”, “d”]]
#Outcome: #[['a' 'b'] #['c' 'd']]
import numpy as np
np_x = np.array(x)
print(np_x)
74 Code (Arithmetic)
np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])
#Outcome: #array([[11, 12], #[13, 14], #[15, 16]])
import numpy as np
np_mat + np.array([10, 10])
75 Get the mean (numpy)
x = [1, 4, 8, 10, 12]
import numpy as np
np.mean(x)
Outcome: 8.0
#76 Get the median (numpy) x = [1, 4, 8, 10, 12]
import numpy as np
np.median(x)
77 Get the standard deviation(numpy)
x = [1, 4, 8, 10, 12]
import numpy as np
np.std(x)
78 Get the correlation between both columns (numpy)
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
#Outcome: # [[1. 0.95865738] # [0.95865738 1. ]]
import numpy as np np_baseball=np.array(baseball) a=np_baseball[:,0] b=np_baseball[:,1] print(np.corrcoef(x=a,y=b))
79 What is a correlation matrix?
A correlation matrix is a table showing correlation coefficients between variables.
[[1. 0.95865738]
[0.95865738 1. ]]
80 Get Mike’s height:
np_heights=np.array([180,170,130,150])
np_name=np.array([‘Mike’,’Maria’,’David’,’Wang’])
mike_height=np_heights[np_name ==’Mike’]
print(mike_height)