Module 4 Flashcards

1
Q

List all the built in data types in python

A

Integers - int
Floating-point numbers - float
Strings - str
Booleans -bool
Lists - list
Tuples - tuple
Sets - set
Dictionaries - dict

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

what data type is this ?

age = 6
type(age)

A

int

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

age = 6.0
type(age)

what data type ?

A

float

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

which value cannot be converted to any other type other than a float ?

A

NaN

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

What is NoneType ?

A

NoneType is its own type, with only one possible value, None.

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

which type has two values: True and False.

A

Boolean

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

name_of_bed_monster = ‘Mike Wazowski’
name_of_bed_monster

Which DT is this ?

A

string

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

If the string contains quotation marks or apostrophes, how do we declare those strings in python ?

A

we can use double quotes or triple single quotes, or triple-double quotes to define the string.

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

len()

A

returns the length of the string

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

.upper()

A

converts into upper case

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

.lower()

A

converts into lower case

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

count()

A

We can also count the number of times a substring or character is present in a string with .count().

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

float(‘five’)

output ?

A

ValueError: could not convert string to float: ‘five’

Detailed traceback:
File “<string>", line 1, in <module></module></string>

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

float(None)

output ?

A

TypeError: float() argument must be a string or a number, not ‘NoneType’

Detailed traceback:
File “<string>", line 1, in <module></module></string>

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

sentence = “I always lose at least one sock when I do laundry.”
words = sentence.split()
words

output ?

A

[‘I’, ‘always’, ‘lose’, ‘at’, ‘least’, ‘one’, ‘sock’, ‘when’, ‘I’, ‘do’, ‘laundry.’]

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

sentence = “I always lose at least one sock when I do laundry.” sentence.split(“e”)

output ?

A

This argument uses the character “e” to separate the string and discards the separator.

[‘I always los’, ‘ at l’, ‘ast on’, ‘ sock wh’, ‘n I do laundry.’]

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

What type of elements are present in a List ?

A

The elements in a list can be any objects, and they don’t all need to have the same type.

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

lists_of_lists = [[1,2], [‘buckle’, ‘My’, ‘Shoe’], 3, 4]
lists_of_lists

output ?

A

4

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

Lists vs strings

A

Lists are mutable whereas strings are not

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

append() ?

A

adds new item to end of list

21
Q

List down some verbs in regards to lists in python

A

max, sum, append

22
Q

What is difference between lists and tuples ?

A

They are represented with parentheses instead of square brackets, and
They are immutable

23
Q

Difference between sets and lists and tuple ?

A

They are unordered, meaning there is no element 0 and element 1, and
The values contained are unique - meaning there are no duplicate entries.

24
Q

Which verb can be used to add elements into a Set ?

A

add

25
Q

What is a dictionary ?

A

A dictionary is a map between key-value pairs.

26
Q

Can we change dictionary values ? If so, why ?

A

And since dictionaries are mutable, we can change the values.

27
Q

how can we add values to a dictionary ?

A

using a new key name.

28
Q

What kind of data types can the dictionary keys represent ?

A

Keys are not limited to strings and can be many different data types, including numerical values and tuples (but they cannot be dictionaries or lists).

29
Q

How can you access all the key-value pairs in a dictionary ?

A

We can access all of the key-value pairs in a dictionary with the verb .items().

30
Q

What is a panda series ?

A

A pandas Series is a one-dimensional array of values with an axis label, sort of like a list with a name attached to it.

31
Q

how can you find the dtype of a df column ?

A

We can use the noun .dtypes to find the dtype of a column.

32
Q

what are dtypes ?

A

columns in a Pandas dataframe have types called dtypes.

33
Q

dtype(‘O’)

What does O stand for ?

A

Object

34
Q

‘The monster under my bed’ * 3

Output ?

A

‘The monster under my bedThe monster under my bedThe monster under my bed’

35
Q

‘The monster under my bed’ + 1200

output ?

A

TypeError: can only concatenate str (not “int”) to str

Detailed traceback:
File “<string>", line 1, in <module></module></string>

36
Q

‘The monster under my bed’ + str(1200)

Output ?

A

‘The monster under my bed1200’

37
Q

What happens if we add lists ?

A

If we add lists, similarly to strings, the lists concatenate together to create a single list containing the elements of both lists.

38
Q

How can a list support subtraction and multiplication ?

A

subtraction and multiplication, are not supported when working with lists.

39
Q

If we have these columns in a df, which of these columns will show up when we call .describe () ?

name object
mfr_type object
calories object
protein int64
fiber float64
fat int64
carbo float64
rating float64
hot bool
dtype: object

A

only numerical columns, not objects

40
Q

which method needs to be used to convert the string column “calories” into an int column ?

A

cereal[‘calories’].astype(‘int’)

41
Q

cereal = cereal.assign(calories=cereal[‘calories’].astype(‘int’))

Whaat is going on here ?

A

We are using the assign verb to cast and assign the calories column

42
Q

How can you calculate the mean for the “hot” column of the “cereal” DF ?

A

cereal[‘hot’].mean()

43
Q

cereal.loc[:, ‘protein’: ‘carbo’].sum(axis=1)

What is going on here ?

A

calculating the sum of each row from protien till carbo

44
Q

axis=1 meaning ?

A

refers to the calculation being done for each row, across multiple columns,

45
Q

axis = 0 meaning ?

A

(which is the default for aggregation verbs) refers to the calculation for each column across multiple rows.

46
Q

cereal = cereal.assign(total_pffc=cereal.loc[:, ‘protein’: ‘carbo’].sum(axis=1))

A

calculating the sum and also assigning the sum value into a separate column

47
Q

new = cereal_amended[‘mfr_type’].str.split(‘-‘, expand=True)

A

splitting a column

48
Q

new = cereal_amended[‘mfr_type’].str.split(‘-‘, expand=False)

A

Our output is now a Pandas Series data type with a list containing both column values as the Series values.