C4: Python for Data Science, AI & Development Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

str()

A

String data type object.

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

int()

A

Integer data type object.

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

float()

A

Floating Point data type object.

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

list()

A

Returns a list of the elements in an object.

E.g.,

Var = “Panks”
list(Var) : (“P”,”a”,”n”,”k”,”s”)

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

int(3.55):?

A

3

Here you are casting a floating point through a integer data type, which may have strange results.

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

float(15):?

A

15.0

Here you are casting a integer into a floating point data type.

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

str(4.558):?

A

“4.558”

Here you are casting a floating point number into a string data type

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

What are the two Boolean expressions in Python?

A

True & False*.

They must be camel case.

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

int(True):?

A

1

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

int(False):?

A

0

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

bool(1):?

A

True.

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

bool(0):?

A

False.

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

What is an Operand?

A

The thing to be operated on.

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

What is an Operator?

A

What the Operand opperates on.

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

type()?

A

Returns the data type of variable.

Good for debugging.

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

What’s Typecasting?

A

Converting one data type into another.

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

Define Tuple.

A

1) An Ordered set of numbers.
2) Where the order cannot change.

  • The sequence cannot be changed.
  • Set is finite.
  • Set may contain duplicates.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

n-tuple

A

(X1, … , Xn)

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

What does RegEx mean?

A

Regular Expression.

  • A Python module.
  • A tool for matching and handling strings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

> > > import re

A

Imports the Regular Expression module built into Python 3.

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

Concatenation

A

Combines strings.

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

Major Data Type Objects in Python.

A

Integers
Floating Points
Strings
Booleans

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

Coupoud Data Type Objects? - Python

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

Tuples syntax? - Python

A

var = (X1,X2,…,…,Xn)

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

For var = (X1,X2,…,…,Xn),
var[0] returns?

A

X1

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

Slicing syntax? - Python

A

[ n1:n2:n3]

Where:
* n1 = The first index position to return.
* n2 = The last index position to pull, +1.
* n3 = The stride, if any.

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

Immutable? - Python

A

Cannot be changed.

E.g.,
* Strings.
* Tuples.

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

List syntax? - Python

A

var = [X1,X2,…,…,Xn]

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

Difference between Lists and Tuples?

A

Lists are mutable. Tupples are not.

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

var = [33,55,2,10]
var.extend([9,19])
?

A

print(var) :
[33,55,2,10,9,19]

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

var = [33,55,2,10]
var.append([9,19])
?

A

print(var) :
[33,55,2,10,[9,19]]

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

var = [33,55,2,10]
del(var[1])
?

A

var = [33,2,10]

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

What method would you use to convert a string to a list?

A

.split()

E.g.,

str(“The rain in spain”)
str2 = str.split()
str2 = [“The”,”rain”,”in”,”spain”]

() is blank, the delimiter is space. Otherwise, pass in a delimiter, such as a colon.

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

Define Aliasing.

A

Multiple names refering to the same object.

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

A = [X1,X2,…,…,Xn]
B = A
list(B) = ?

A

[X1,X2,…,…,Xn]

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

A = [X1,X2,…,…,Xn]
B = A[:]
A[2] = 100

list(A) = ?
list(B) = ?

A

A = [X1,X2,100,…,…,Xn]
B = [X1,X2,…,…,Xn]

A[:] clones the list in variable A.

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

copy() - Python

A

A Method used to create a shallow copy of a list.

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

count() - Python

A

A Method used to count the number of occurrences of a specific element in a list.

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

Define these list methods:

Below, let “var”, be any variable that holds a list:

var.pop()
var.reverse()
var.sort
var.remove()
var.insert()
var.count()
var.copy()

A
  • pop() method is another way to remove an element from a list in Python. It removes and returns the element at the specified index. If you don’t provide an index to the pop() method, it will remove and return the last element of the list by default
  • The reverse() method is used to reverse the order of elements in a list
  • The sort() method is used to sort the elements of a list in ascending order. If you want to sort the list in descending order, you can pass the reverse=True argument to the sort() method.
  • To remove an element from a list. The remove() method removes the first occurrence of the specified value.
  • The count() method is used to count the number of occurrences of a specific element in a list in Python.
  • The copy() method is used to create a shallow copy of a list.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Dictionaries object syntax?

A

{'A':'a','B':'b',...,...,'N':'n'}

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

Dictionary attributes.

A
  • Keys must be unique.
  • Keys are immutable.
  • Values are mutable
  • Values don’t have to be unique.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Assume:
{'Key1':'Value1','Key2','Value2',...,...,'KeyN','ValueN'}.

If:
Dict['Key2'] :

A

Value2

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

Assume:
{'Key1':'Value1','Key2','Value2'}.

If:
Dict['New1'] = 100 :

A

{'Key1':'Value1','Key2','Value2','New1':100}.

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

Assume:
{'Key1':'Value1','Key2':'Value2','New1':100}.

If:
del(Dict['Key1']) :

A

{‘Key2’:’Value2’,’New1’:100}

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

Assume:
{'A':'a','B':'b',...,...,'N':'n'}

Query this Dictionary for B.

A

'B' in Dict returns True

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

Dict.keys() : ?

A

This method lists all the keys in the Dictionary.

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

Dict.values() : ?

A

This method lists all the values in the Dictionary.

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

Set properties?

A
  • Unordered.
  • Containes no duplicate values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Set object syntax?

A

{3,1,2,...,...,N}

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

Typecast a List into a Set.

A

set(list[x,y,z])

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

Assume:
var = [3,58,5,5,10,10,8,6]

set(var) : ?

A

{3,58,5,10,8,6}

Note: A set cannnot contain duplicates. They are removed from the list.

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

Assume:
var = {'lake','fire','water'}

var.add('smores')
var : ?

A

{'lake','fire','water','smores'}

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

Assume:
var = {'lake','fire','water','smores'}

var.add('fire')
var : ?

A

var = {'lake','fire','water','smores'}

*Note: A set cannnot contain duplicates. “fire’ cannot be added.

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

Assume:
var = {'lake','fire','water','smores'}

var.remove('fire')
var : ?

A

var = {'lake','water','smores'}

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

Assume:
var = {'lake','fire','water','smores'}

‘lake’ in var: ?

A

True

56
Q

Set Theory - Intersection

Assume:
Set1 = {1,2,3,4}
Set2= {3,4,5,6}

Instruct Python to find the intersection of both sets.

A

Set3 = Set1 & Set2
~~~
Set3 : {3,4}

57
Q

Set Theory - Union

Assume:
Set1 = {1,2,3,4}
Set2= {3,4,5,6}

Instruct Python to find the union of both sets.

A

Set3 = Set1.union(Set2)
~~~
{1,2,3,4,5,6}

58
Q

What method clears all the entries from a Dictionary?

A

var.clear()

59
Q

List the six common Comparison Operators.

A

A == B - Is A equal to B?
A != B - Is A not equal to B?
A > B - Is A greater than B?
A < B - Is A less than B?
A >= B - Is A greater or equal to B?
A <= B - Is A less or equal to B?

60
Q

Comparison Operators return what?

A

True or False.
These are boolean.

61
Q

‘Baxter’ == ‘Baker’: ?

A

False
~~~
Note: String data types can be compared.

62
Q

if syntax?

A

if(condition) : Execute if true.
______Indented code.
______Indented code.
~~~
Note: Indents are required.

63
Q

if / else syntax?

A

if(condition) : Execute if true.
______Indented code.
______Indented code.
else: Execute if above is false.
______Indented code.
______Indented code.
~~~
Note: Indents are required.

64
Q

if / elif / else syntax?

A

if(condition) : Execute if true.
______Indented code.
______Indented code.
elif(condition) : Execute if above is false & this is true.
______Indented code.
______Indented code.
else: Execute if both above are false.
______Indented code.
______Indented code.
~~~
Note: Indents are required.

65
Q

Logic Operators ?

A

not()
or()
and()

Python supports these three logic operators.

66
Q

if(True) and (False): ?

A

False

67
Q

if(True) or (False): ?

A

True

68
Q

range(N)
or
range(0,N)

A

[0,1,2,…,…,N-1]

Note:
* The last value of arange function is always one less than the number input into the function.
* A range sequence always starts at 0.
* N must be a an whole number.

69
Q

range(10,15) : ?

A

[10,11,12,13,14]

70
Q

i in for loops?

A

Index or increment.

71
Q

Incement i by 1.
Decrement i by 5.

A

i += 1
i -= 5

72
Q

for syntax?

A

for counter in iterable object :
______Indented code.
______Indented code.
~~~
Note:
* for will increment the “counter” at the end of each loop.
* “counter” can be any variable that can hold an integer data type. It is usualy i.
* It is a temporary variable that is constrained in scope to the loop.

73
Q

while syntax?

A

while some conditional True :
______Indented code.
______Indented code.

Note:
* It is not uncommon to itterate some counter variable within the while loop.

74
Q

list(enumerate(range(11))) : ?

A

[(0, 0),
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8),
(9, 9)
(10,10)]]

75
Q

Function syntax ?

A

def function_name(Argument(s)):
______”””
______Notes
______”””
______Indented code.
______return b

~~~
Note:
* Functions cannot have an empty body. Use none in the body satsify this requirement that the function not be blank.
* You do not have to define a return in a function.

76
Q

Do Local Variables confilict with Global Variables?

A

No.
Variables inside a functon do not change variables outside a function, even if they have the same name.

77
Q

What will Python do if if finds an undefined variable in a function?

A

Check for the variable in the global scope.

78
Q

What does pass do in a function?

A
  • Acts as a temporary placeholder for future code.
  • It ensures that the code remains syntactically correct, even if it doesn’t do anything yet.
  • Doesn’t perform any meaningful action. When the interpreter encounters “pass”, it simply moves on to the next statement without executing any code.
79
Q

Define Global Scope variables.

A

Variables defined outside functions; accessible everywhere.

80
Q

Define Local Scope variables.

A

Variables inside functions; only usable within that function.

81
Q

What does global do in a function?

A

Changes the scope of a variable in the function from local to global.
Meaning that it can be referenced even after the function has completed.

82
Q

How would you modify the following function to allow you to pass in a variable number of arguments?
~~~
def a_function(argument):
______”””
______Notes
______”””
______Indented code.
______return b

A

Place an asterisk before the argument.
~~~
E.g., def a_function( *argument):

83
Q

Try syntax?

A

try:
______code or pass
except Exception:
______code or pass
except Exception:
______code or pass
else:
______code or pass
finally:
______code or pass

84
Q

Complete the sentence:
Methods are “…” with Objects.

A

…how you change or interact…

85
Q

Class > Object > ? > Method

A

Attribute.

86
Q

? > Object > Attribute > Method

A

Class

87
Q

Class > ? > Attribute > Method

A

Object

88
Q

Class > Object > Attribute > ?

A

Method

89
Q

A class is a template for?

A

Objects.

90
Q

Class syntax?

A

class className():

______class_attribute = value

______def __init__(self, attribute1, attribute2, …):
____________self.attribute1 = attribute1
____________self.attribute2 = attribute2
____________...

______def method1(self, parameter1, parameter2, ...): \_\_\_\_\_\_\_\_\_\_\_\_Method logic \_\_\_\_\_\_\_\_\_\_\_\_…`

~~~
* Class attributes (shared by all instances).
* Constructor method (initialize instance attributes).
* Instance methods (functions). Do not have to have a value passed.

91
Q

An objects attributes define it’s?

A

State.
A.k.a., The data that describes the object.

92
Q

An objects methods define it’s?

A

Behavior.
A.k.a., Actions assigned to the object that can change it’s state.

93
Q

An Object is an … of a Class?

A

Instance.

94
Q

Name the two loop controls.

A

Break.
Continue.

95
Q

What does dir(object) do?

A

Returns all properties and methods of the specified object, without the values.

96
Q

Open a file syntax?

A

open(*…/path/filename.txt,x)
~~~
Where:
* *The value of x may be:
* r for reading mode.
* w for writing mode.
* a for appending mode.

97
Q

fileObject.name() returns?

A

The name of the file in the file object.

98
Q

Open the following file in append mode inside a file object named darth :
~~~
…/path/tothe.darkside

A

with open('…/path/tothe.darkside','a') as darth

99
Q

fileObject.read() ?

A

Reads the entire content of the file.
e.g., darth = vader.read()

100
Q

fileObject.read(x) ?

A

Reads x characters starting from the current position of the file pointer.
e.g., darth = vader.read(10)

101
Q

fileObject.readline() ?

A

Reads the contents of a file line by line.
Note, it remembers where it last read. Then itterates and reads the next line if called again. Also, if given an integer as an argument, it will read tham many posisitons of that line. But only from that line.

102
Q

fileObject.seek(x) ?

A

Moves the files pointer to a specific position in the file. Then reads that character.
The position is specified in bytes, so you’ll need to know the byte offset of the characters you want to read.

103
Q

fileObject.close() ?

A

Closes a file.
This is essential.
However, you should not have to use this if you are using the with method.

104
Q

Create a new file in the local directory named obiwan.master mapped to a file object named jedi.

A

with open('…/path/tothelight/obiwan.master','w') as jedi:

~~~
Note:
If the file requested by open does not exist in the directory, the action will create it.

105
Q

Dependencies is another name for ?

A

Libraries.

106
Q

.tell() does what?

A

Returns where the system caret ^ is in the file object.

107
Q

Diferentiate the following;
.readline() .readlines()

A

.readline():
This method returns one full line from the file object as a string.

.readlines():
This method returns all the line in a file object as a list.

108
Q

How do you querying a variable for it’s attributes and methods?

A

dir(var)

109
Q

Load pandas?

A

import pandas as pd

110
Q

With pandas imported:
df.head() does what?

A

Prints the first five rows of the data frame.

111
Q
  • With pandas imported:
  • Assume var is a dictionary.
    ~~~
    How would you cast the dictionary into a dataframe?
A

varInFrame = pd.DataFrame(var)

112
Q

With pandas imported:
When you cast a dictionary into a data frame, the values become?

A

Columns.
~~~
The keys become the column headers.

113
Q

With pandas imported:
How would you pull an entire column(s) from a data frame and make it a new object?

A

var = df [[ ‘key1’ , ‘key2’ , ‘key5’ ]]

Double brackets extracts a column by key (header)name.

114
Q
  • With pandas imported +
  • An active data frame, df:
    How would you reference cell a1?
A

df.iloc[0,0]

115
Q

With pandas imported:
Read a decks.csv into the data frame object, df.

A

df = pd.read_csv('decks.csv')

116
Q
  • With pandas imported:
  • Assume: data = [10, 20, 30, 40, 50]
    Create a series in series object s from this list.
A

s = pd.Series(data)

117
Q

Diferentiate,
print(df.iloc[n])
from
print(df.loc[n])

A

iloc accesses the row by position.
iloc[row_index, column_index]
i.e., index location.
Counts from 0.
~~~
loc acesses the row by label
loc[row_label, column_label]
Use method set_index() to set a columns contents as the index from the default.
Counts from 1.

118
Q

Pandas generally provides two data structures for manipulating data, They are?

A

Data Frames.
Series.

119
Q

df.iloc[1:3]
vs.
df.iloc[1,3]

A

The first slices out and returns rows 2 & 3.
The second selects the data a row 2 / column 3.

120
Q

What is df.iloc[0:2, 0:3] doing?

A

Slicing out the first two rows, three columns deep.

121
Q

NumPy stands for?

A

Numerical Python.

122
Q

NumPy is meant for working with?

A
  • Arrays
  • Linear algebra
  • Fourier transforms
  • Matrices
123
Q

The ‘Pandas library’ is built on?

A

The NumPy library.

124
Q

Syntax for making a 2D array in Python withNumPy?

A

my_2d_array = np.array [
_________________________[0,0,0,0,0,0],
_________________________[0,0,0,0,0,0],
_________________________[0,0,0,0,0,0],
_________________________[0,0,0,0,0,0],
_________________________[0,0,0,0,0,0],
_________________________],

125
Q

if var is an numpy array what do these methods do?
var.dtype
var.size
var.ndim
var.shape
var.dot()
var.mean()
var.max()
var.min()
var.std()

A

.dtype returns the type of the data stored in the array.
.size returns the number of elements in the array.
.ndim returns the number of dimensions of the array.
.shape returns the length of each dimension of the array.
.dot(a,b) returns the dot product of the np arrays a and b.
.mean() returns the mean value of the array.
.max() returns the max value of the array.
.std() returns the standard deviation of the array.

126
Q

np.pi ?

A

Return the value of pi.

127
Q

np.linspace(a,b,c)

A

np.linspace(a,b,c) returns a line (a list) starting at ‘a’ and ending at ‘b’ with ‘c’ evenly spaced intevals as a array. (Which include the intervals ‘a’ and ‘b’.

128
Q

What does URI stand for?

A

Uniform resource identifier.
This is distinct from a URL.

129
Q

What are the four HTTP methods?

A

GET: Retrieves data from the Server or API.
POST: Submits data to the Server or API.
PUT: Updates data on the Server or API.
DELETE: Deletes data on the the Server or in the API.

130
Q

What does the Requests library do?

A

Allows you to make HTTP 1.1 requests from within Python.

131
Q

A URL has three parts.
They are?

A

Scheme.
Address.
Path.

e.g.,

https://
www.google.com
/a/admin/login

132
Q

Which contanins which?
URL
URI

A

Uniform Resource Identifier > Uniform Resource Locator
or
URI {URL}

133
Q

What are the 5 HTTP response status code classes and their general meaning?

A

100: Info.
200: Success.
300: Make a choice.
400: Failure.
500: Big failure.

134
Q

HTML tags hierarchy?

A

Parent > Child > Decendent
Siblings = Siblings

135
Q

What is Beautiful Soup?

A

A module that facilitates webscraping.

136
Q

What does DOM stand for?

A

Document Object Model

137
Q
A