Module 4 Flashcards
List all the built in data types in python
Integers - int
Floating-point numbers - float
Strings - str
Booleans -bool
Lists - list
Tuples - tuple
Sets - set
Dictionaries - dict
what data type is this ?
age = 6
type(age)
int
age = 6.0
type(age)
what data type ?
float
which value cannot be converted to any other type other than a float ?
NaN
What is NoneType ?
NoneType is its own type, with only one possible value, None.
which type has two values: True and False.
Boolean
name_of_bed_monster = ‘Mike Wazowski’
name_of_bed_monster
Which DT is this ?
string
If the string contains quotation marks or apostrophes, how do we declare those strings in python ?
we can use double quotes or triple single quotes, or triple-double quotes to define the string.
len()
returns the length of the string
.upper()
converts into upper case
.lower()
converts into lower case
count()
We can also count the number of times a substring or character is present in a string with .count().
float(‘five’)
output ?
ValueError: could not convert string to float: ‘five’
Detailed traceback:
File “<string>", line 1, in <module></module></string>
float(None)
output ?
TypeError: float() argument must be a string or a number, not ‘NoneType’
Detailed traceback:
File “<string>", line 1, in <module></module></string>
sentence = “I always lose at least one sock when I do laundry.”
words = sentence.split()
words
output ?
[‘I’, ‘always’, ‘lose’, ‘at’, ‘least’, ‘one’, ‘sock’, ‘when’, ‘I’, ‘do’, ‘laundry.’]
sentence = “I always lose at least one sock when I do laundry.” sentence.split(“e”)
output ?
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.’]
What type of elements are present in a List ?
The elements in a list can be any objects, and they don’t all need to have the same type.
lists_of_lists = [[1,2], [‘buckle’, ‘My’, ‘Shoe’], 3, 4]
lists_of_lists
output ?
4
Lists vs strings
Lists are mutable whereas strings are not
append() ?
adds new item to end of list
List down some verbs in regards to lists in python
max, sum, append
What is difference between lists and tuples ?
They are represented with parentheses instead of square brackets, and
They are immutable
Difference between sets and lists and tuple ?
They are unordered, meaning there is no element 0 and element 1, and
The values contained are unique - meaning there are no duplicate entries.
Which verb can be used to add elements into a Set ?
add
What is a dictionary ?
A dictionary is a map between key-value pairs.
Can we change dictionary values ? If so, why ?
And since dictionaries are mutable, we can change the values.
how can we add values to a dictionary ?
using a new key name.
What kind of data types can the dictionary keys represent ?
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).
How can you access all the key-value pairs in a dictionary ?
We can access all of the key-value pairs in a dictionary with the verb .items().
What is a panda series ?
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.
how can you find the dtype of a df column ?
We can use the noun .dtypes to find the dtype of a column.
what are dtypes ?
columns in a Pandas dataframe have types called dtypes.
dtype(‘O’)
What does O stand for ?
Object
‘The monster under my bed’ * 3
Output ?
‘The monster under my bedThe monster under my bedThe monster under my bed’
‘The monster under my bed’ + 1200
output ?
TypeError: can only concatenate str (not “int”) to str
Detailed traceback:
File “<string>", line 1, in <module></module></string>
‘The monster under my bed’ + str(1200)
Output ?
‘The monster under my bed1200’
What happens if we add lists ?
If we add lists, similarly to strings, the lists concatenate together to create a single list containing the elements of both lists.
How can a list support subtraction and multiplication ?
subtraction and multiplication, are not supported when working with lists.
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
only numerical columns, not objects
which method needs to be used to convert the string column “calories” into an int column ?
cereal[‘calories’].astype(‘int’)
cereal = cereal.assign(calories=cereal[‘calories’].astype(‘int’))
Whaat is going on here ?
We are using the assign verb to cast and assign the calories column
How can you calculate the mean for the “hot” column of the “cereal” DF ?
cereal[‘hot’].mean()
cereal.loc[:, ‘protein’: ‘carbo’].sum(axis=1)
What is going on here ?
calculating the sum of each row from protien till carbo
axis=1 meaning ?
refers to the calculation being done for each row, across multiple columns,
axis = 0 meaning ?
(which is the default for aggregation verbs) refers to the calculation for each column across multiple rows.
cereal = cereal.assign(total_pffc=cereal.loc[:, ‘protein’: ‘carbo’].sum(axis=1))
calculating the sum and also assigning the sum value into a separate column
new = cereal_amended[‘mfr_type’].str.split(‘-‘, expand=True)
splitting a column
new = cereal_amended[‘mfr_type’].str.split(‘-‘, expand=False)
Our output is now a Pandas Series data type with a list containing both column values as the Series values.