1 Introduction to Python (1) Flashcards

Python basics , Numpy... Colab ipynb: https://colab.research.google.com/drive/18d3Tb0gSaOFfbUWL5yYlgYmaS9j-wOYs?usp=sharing

1
Q

1 What is Python?

A

A general-purpose programming language

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2 What is Data Science?

A

An interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 Data Science as an intersection of three fields. Which ones?

A
  1. Computer Science IT
  2. Math and Statistics
  3. Domains/Business Knowledge
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4 What is an algorithm ?

A

A finite sequence of computer-implementable instructions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

5 What is a Python script?

A

A collection of commands in a file designed to be executed like a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

6 What is a Shell?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7 Outcome of:

print(10/2)

A

5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8 How to write exponentiation?

A

**

2**3=8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9 What does % Modulo?

A

Returns the remainder of a division

18 % 7 =5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

10 What is a variable assignment

A

A name attached to a particular object

variable_a= 18

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

11 Create a variable ‘savings’ with value 100 and print it

A

savings = 100

print(savings)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

12 Write a script to compound these variables:

savings =100
growth_multiplier = 1.1
t=1

A

result = savings*growth_multiplier**t

result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13 What is type conversion?

A

An object from one data type to another data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

14 Outcome of:

type(‘a’)

A

str

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

15 Outcome of:

type(False)

A

bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

16 Outcome of:

type(3)

A

int

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

17 Outcome of:

type(3.3)

A

float

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

18 Complete code:

’'’savings = 100
result =savings*1.10**7
print(…)

Outcome:
I started with $100 and now I have 194.9

A
savings = 100
savings = 100
result = round(savings * 1.10**7,1)
print('I started with $' + str(savings)
 \+ ' and now I have $' +str(result))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

19 What is a list?

A

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 [ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

20 Create a list called house with elements ‘room, kitchen’

#Outcome:
['room', 'kitchen']
A

house = [‘room’, ‘kitchen’]

house

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

21 Can a list contain different types of data such as a string and a float?

A

Yes

[1+2,’a’*5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

22 What is a list of lists or a multi-dimensional list?

A

A list inside a list (nested)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

23 Use list a get this outcome:

[[‘a’,’b’],’c’]

A

m_list=[[‘a’,’b’],’c’]

m_list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

24 What is subsetting?

A

The process of retrieving just the parts of large files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
#25 Get the output (2 ways) x= ['a', 'b' , 'c', 'd'] #Output: 'b'
x[1];x[-3]
26
#26 What is an index in a list?
Refers to a position within an ordered list
27
#27 Subset and operate x= ['a', 'b' , 'c', 'd'] #Outcome:bc 'bc'
print(x[1]+x[2]) | x[1]+x[2]
28
#28 What is slicing?
Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists
29
#29 Slice x= ['a', 'b' , 'c', 'd'] #Outcome: ['b', 'c']
x[1:3]
30
#30 Slice (2 ways) x= ['a', 'b' , 'c', 'd'] #Outcome: ['b', 'c']
print(x[1:3]) | print(x[-3:-1])
31
#31 Subsetting lists of lists x=[['a', 'b', 'c' ], ['d','e', 'f']] #Outcome: d
print(x[1][0])
32
#32 Replace list elements x = ['a', 'b', 'c','d'] #Outcome: ['a', 'b', 7, 8]
x[2:]= [7,8] | x
33
#33 Code to extend the list ``` x = ['a', 'b', 'c','d'] y= x+(...code..) ``` #Outcome: ['a', 'b', 'c', 'd', 'e', 'f']
x = ['a', 'b', 'c','d'] y = x + ['e', 'f'] print(y)
34
#34 Delete list elements x = ['a', 'b', 'c','d'] #Outcome: ['a', 'c', 'd']
del(x[1]) | print(x)
35
#35 What is ';' used for?
To place commands on the same line command1; command2
36
#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
#37 Outcome of: x = ['a', 'b', 'c','d'] y = x del(y[0]) print(x)
['b', 'c', 'd']
38
#38 Outcome of: x = ['a', 'b', 'c','d'] y = list(x) del(y[0]) print(x)
['a', 'b', 'c', 'd']
39
``` #39 Get number of elements: x = ['a', 'b', 'c','d'] ``` #Outcome: 4
len(x)
40
#40 Function to convert to string
str()
41
#41 Function to convert to integer
int()
42
#42 Function to convert to float
float()
43
#43 Function to convert to boolean
bool()
44
#44 Outcome of: a= 3.4 str(a)
3.4'
45
#45 Outcome of: a= 3.4 int(a)
3
46
#46 Outcome of: a= 3.4 bool(a)
TRUE
47
#47 Think of two ways to get information about a function
help(function) ?function
48
#48 Code (look at the order of the elements) x = ['z','a', 'c','d'] #Outcome: ['z', 'd', 'c', 'a']
sorted(x,reverse=True)
49
#49 Three arguments in the function sorted()
sorted(iterable,key,reverse)
50
#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
#51 Code (look at the order of the elements) x = ["cccc", "b", "dd", "aaa"] #Outcome: ['b', 'dd', 'aaa', 'cccc']
sorted(l, key =len)
52
#52 Code (string method) my_house ='My house' #Outcome: 'MY HOUSE'
my_house.upper()
53
#53 Code (string method) ``` #Count letter 'o' p='pool' ``` #Outcome: 2 Outcome: 2
p.count('o')
54
#54 What is a method in python?
A function that “belongs to” an object
55
#55 Get the position of c (list method) x=['a', 'b', 'c' ] #Outcome: 1
x.index('b')
56
#56 How many a in the list? (list method) x=['a', 'b', 'c' ,'a'] #Outcome: 2
x.count('a')
57
#57 Add d (list method) x=['a', 'b', 'c' ] #Outcome: ['a', 'b', 'c', 'd']
x.append('d') | print(x)
58
#58 Delete c (list method) x=['a', 'b', 'c' ] #Outcome: ['a', 'b']
x.remove('c') | print(x)
59
#59 Change order (list method) x=['a', 'b', 'c' ] #Outcome: ['c', 'b', 'a']
x.reverse() | print(x)
60
#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
#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
#62 What is a tuple in python?
An immutable sequence of Python objects | 2,3,6,9
63
#63 Code baseball = [180, 215, 210, 210] #Outcome: numpy.ndarray
import numpy as np np_baseball=np.array(baseball) type(np_baseball)
64
#64 Code baseball = [180, 215, 210, 210] #Outcome:
import numpy as np np_baseball=np.array(baseball) print(type(np_baseball))
65
#65 Code baseball = [180, 215, 210, 210] #Outcome: [1800 2150 2100]
print(np_baseball*10)
66
#66 Complete code (subset arrays) '''height = [180, 215, 210] height_np=... tall = ...>200 print(...)''' #Outcome: [215 210]
import numpy as np height = [180, 215, 210] height_np=np.array(height) tall = height_np>200 print(height_np[tall])
67
#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
#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
#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
#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]] #Outcome: (4,2)
import numpy as np np_baseball=np.array(baseball) np_baseball.shape
71
#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
#72 Code (Subset 2D NumPy arrays) x = [["a", "b"], ["c", "d"]] #Outcome: ['a' 'c']
import numpy as np np_x = np.array(x) print(np_x[:,0])
73
#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
#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
#75 Get the mean (numpy) x = [1, 4, 8, 10, 12] #Outcome: 7.0
import numpy as np | np.mean(x)
76
``` #76 Get the median (numpy) x = [1, 4, 8, 10, 12] ``` #Outcome: 8.0
import numpy as np | np.median(x)
77
#77 Get the standard deviation(numpy) x = [1, 4, 8, 10, 12] #Outcome: 4.0
import numpy as np | np.std(x)
78
#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
#79 What is a correlation matrix?
A correlation matrix is a table showing correlation coefficients between variables. [[1. 0.95865738] [0.95865738 1. ]]
80
#80 Get Mike's height: np_heights=np.array([180,170,130,150]) np_name=np.array(['Mike','Maria','David','Wang']) #Outcome: [180]
mike_height=np_heights[np_name =='Mike'] print(mike_height)