Session 17 – Python: Data Types
Common data types in Python include: Booleans, Numbers, Strings, Bytes, Lists, Tuples, Sets and Dictionaries.
17.1 Booleans
Booleans have two possible values: True of False. You can think of booleans as simple lie detectors.
#boolean, test if true
= True
bee_allergy if bee_allergy == True:
print('patient is allergic to bees')
else:
print('patient is not allergic to bees')
#short-hand boolean condition
= True
bee_allergy if bee_allergy:
print('patient is allergic to bees')
else:
print('patient is not allergic to bees')
#one-liner!
print('patient is allergic to bees') if bee_allergy else print('patient is not allergic to bees')
17.2 Numbers: int, float and complex
It is important to note that floats will only be precise up to 15 decimal places. Any smaller should be abbreviated accordingly.
#using type and instance to decipher number type
type(2)
type(2.0)
type(2+3x)
type(2+3j)
isinstance(2+3j, complex)
#what does j mean??
#create a complex number
complex(2,3)
17.3 Strings
Strings are sequences of characters between double or single quotes.
#simple string
print("the store is out of apples")
#simple string 2
print('the store is out of apples')
#multiline string
print("""the store
is out of
apples
""")
#One-liner, multiline string?!
print("the store \n is out of \n apples")
#One-liner, multiline string?! fixed for spacing issues
print("the store \nis out of \napples")
17.4 Bytes
Byte can store bytes ranging from 0 - 255. We will not go over bytes as we will not use them directly in this setting. Called with ‘bytes()’
17.5 Lists
Lists are an array that stores typed objects in a specific sequence. Index in lists will start at 0 not at 1. Lists are contained in brackets.
#lets make a list containing different classes
= [True, False, 1, 1.1, 1+2j, 'Learn', b'Python']
my_list
print(my_list)
print(my_list[0])
print(my_list[-1])
#lets print the class of each item in the list
for item in my_list:
print(type(item))
#you can target and change items in the list
3] = 4
my_list[print(my_list)
#you can slice a list
print(my_list[0:3])
17.6 Tuples
Unlike lists, tuples are contained within parentheses. Tuples can contain objects of different types. Objects are still maintained in an order. Tuples cannot be modfied after creation. The objects themselves can be altered if they are alterable however, like identifiers.
#empty tuple
= ()
tuple_1
tuple_1
#tuple in tuple; nested tuple
= (1,2,3)
tuple_1 = ('apple', 'bannana')
tuple_2 = (tuple_1, tuple_2)
tuple_3
tuple_3
#modify tuples
= ("na",)*3
tuple_na
tuple_na
= (tuple_na, 'batman')
tuple_bat
tuple_bat
#slicing tuples
= (1,2,3,4)
tuple_num 1:] tuple_num[
17.7 Sets
Sets in python are used for mathematical operations and are incased in braces.
#Simple set
= set("strawberry mango")
simple_set type(simple_set)
simple_set
#Another Simple Set
= {'strawberry', 'mango', 'grapefruit'}
simpler_set type(simpler_set)
simpler_set
#Frozenset
= {'fish', 'shrimp'}
food
food= frozenset(food)
frozen_food type(frozen_food)
frozen_food
17.8 Dictionary
A dictionary is an unordered pair of key-value pairs. Highly optimized to store large datasets. Dictionaries use braces to define keys and their respective values.
#dictionary, below are random dates
= {'key':'value', 'feb':28, 'oct':2, 'dec':8}
worst_days_of_year type(worst_days_of_year)
worst_days_of_year
#drawing information from the dictionary
'feb']
worst_days_of_year[
#key filtering
worst_days_of_year.keys()
#value filtering
worst_days_of_year.values()
#item listing
worst_days_of_year.items()
#update a dictionary
worst_days_of_year.update({'mar':4})
worst_days_of_year
#reassign data
'mar'] = 5
worst_days_of_year[
worst_days_of_year
#delete data
'mar']
del worst_days_of_year[ worst_days_of_year