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
Q

25 Get the output (2 ways)

x= [‘a’, ‘b’ , ‘c’, ‘d’]

A

x[1];x[-3]

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

26 What is an index in a list?

A

Refers to a position within an ordered list

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

27 Subset and operate

x= [‘a’, ‘b’ , ‘c’, ‘d’]

A

print(x[1]+x[2])

x[1]+x[2]

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

28 What is slicing?

A

Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists

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

29 Slice

x= [‘a’, ‘b’ , ‘c’, ‘d’]

A

x[1:3]

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

30 Slice (2 ways)

x= [‘a’, ‘b’ , ‘c’, ‘d’]

A

print(x[1:3])

print(x[-3:-1])

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

31 Subsetting lists of lists

x=[[‘a’, ‘b’, ‘c’ ],
[‘d’,’e’, ‘f’]]

A

print(x[1][0])

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

32 Replace list elements

x = [‘a’, ‘b’, ‘c’,’d’]

A

x[2:]= [7,8]

x

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

33 Code to extend the list

x = ['a', 'b', 'c','d']
y= x+(...code..)
A

x = [‘a’, ‘b’, ‘c’,’d’]
y = x + [‘e’, ‘f’]
print(y)

34
Q

34 Delete list elements

x = [‘a’, ‘b’, ‘c’,’d’]

A

del(x[1])

print(x)

35
Q

35 What is ‘;’ used for?

A

To place commands on the same line

command1; command2

36
Q

36 What is # used for?

A

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
Q

37 Outcome of:

x = [‘a’, ‘b’, ‘c’,’d’]
y = x
del(y[0])
print(x)

A

[‘b’, ‘c’, ‘d’]

38
Q

38 Outcome of:

x = [‘a’, ‘b’, ‘c’,’d’]
y = list(x)
del(y[0])
print(x)

A

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

39
Q

Outcome: 4

#39 Get number of elements:
x = ['a', 'b', 'c','d']
A

len(x)

40
Q

40 Function to convert to string

A

str()

41
Q

41 Function to convert to integer

A

int()

42
Q

42 Function to convert to float

A

float()

43
Q

43 Function to convert to boolean

A

bool()

44
Q

44 Outcome of:

a= 3.4
str(a)

A

3.4’

45
Q

45 Outcome of:

a= 3.4
int(a)

A

3

46
Q

46 Outcome of:

a= 3.4
bool(a)

A

TRUE

47
Q

47 Think of two ways to get information about a function

A

help(function)

?function

48
Q

48 Code (look at the order of the elements)

x = [‘z’,’a’, ‘c’,’d’]

A

sorted(x,reverse=True)

49
Q

49 Three arguments in the function sorted()

A

sorted(iterable,key,reverse)

50
Q

50 What is an iterable in python

A

When an object is said to be iterable, it means that you can step through (i.e. iterate) the object as a collection

51
Q

51 Code (look at the order of the elements)

x = [“cccc”, “b”, “dd”, “aaa”]

A

sorted(l, key =len)

52
Q

52 Code (string method)

my_house =’My house’

A

my_house.upper()

53
Q

53 Code (string method)

#Count letter 'o'
p='pool'

Outcome: 2

A

p.count(‘o’)

54
Q

54 What is a method in python?

A

A function that “belongs to” an object

55
Q

55 Get the position of c (list method)

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

A

x.index(‘b’)

56
Q

56 How many a in the list? (list method)

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

A

x.count(‘a’)

57
Q

57 Add d (list method)

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

A

x.append(‘d’)

print(x)

58
Q

58 Delete c (list method)

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

A

x.remove(‘c’)

print(x)

59
Q

59 Change order (list method)

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

A

x.reverse()

print(x)

60
Q

60 Transform it into a numpy array

baseball = [180, 215, 210, 210]

#Outcome:
array([180, 215, 210, 210, 188])
A

import numpy as np
np_baseball =np.array(baseball)
np_baseball

61
Q

61 What is a numpy array?

A

A grid of values, all of the same type, and is indexed by a tuple of nonnegative integers

62
Q

62 What is a tuple in python?

A

An immutable sequence of Python objects

2,3,6,9

63
Q

63 Code

baseball = [180, 215, 210, 210]

A

import numpy as np

np_baseball=np.array(baseball)
type(np_baseball)

64
Q

64 Code

baseball = [180, 215, 210, 210]

A

import numpy as np

np_baseball=np.array(baseball)
print(type(np_baseball))

65
Q

65 Code

baseball = [180, 215, 210, 210]

A

print(np_baseball*10)

66
Q

66 Complete code (subset arrays)

’'’height = [180, 215, 210]
height_np=…
tall = …>200
print(…)’’’

A

import numpy as np

height = [180, 215, 210]
height_np=np.array(height)
tall = height_np>200
print(height_np[tall])

67
Q

67 Why do we do vectorization?

A

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
Q

68 What is type coercion?

A

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
Q

69 What happens if one tries to build a numpy array with different data types?

A

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
Q

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

A

import numpy as np
np_baseball=np.array(baseball)
np_baseball.shape

71
Q

71 Difference between method and attribute in python

A

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
Q

72 Code (Subset 2D NumPy arrays)

x = [[“a”, “b”], [“c”, “d”]]

A

import numpy as np
np_x = np.array(x)
print(np_x[:,0])

73
Q

73 Code

x = [[“a”, “b”], [“c”, “d”]]

#Outcome:
#[['a' 'b']
#['c' 'd']]
A

import numpy as np
np_x = np.array(x)
print(np_x)

74
Q

74 Code (Arithmetic)

np_mat = np.array([[1, 2],
[3, 4],
[5, 6]])

#Outcome:
 #array([[11, 12],
 #[13, 14],
 #[15, 16]])
A

import numpy as np

np_mat + np.array([10, 10])

75
Q

75 Get the mean (numpy)

x = [1, 4, 8, 10, 12]

A

import numpy as np

np.mean(x)

76
Q

Outcome: 8.0

#76 Get the median (numpy)
x = [1, 4, 8, 10, 12]
A

import numpy as np

np.median(x)

77
Q

77 Get the standard deviation(numpy)

x = [1, 4, 8, 10, 12]

A

import numpy as np

np.std(x)

78
Q

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. ]]
A
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
Q

79 What is a correlation matrix?

A

A correlation matrix is a table showing correlation coefficients between variables.

[[1. 0.95865738]
[0.95865738 1. ]]

80
Q

80 Get Mike’s height:

np_heights=np.array([180,170,130,150])
np_name=np.array([‘Mike’,’Maria’,’David’,’Wang’])

A

mike_height=np_heights[np_name ==’Mike’]

print(mike_height)