# len() function
# len() can take a string or a container
print(len("ello")) # argument is a string
print(len([1, 2, 3])) # argument is a list of numbers
4
3
# max() function works on containers
# max() function works on strings too
name_list = max(["Joey", "Antonio", "Leslie", "Indra"])
print(name_list)
Leslie
# max() function does not work on a mixed list
mixed_list = [25, "December"]
max(mixed_list) # will give error
TypeError: '>' not supported between instances of 'str' and 'int'
"a" > "b" # "a" is alphabetically first before "b"
def custom_max(my_list):
result = my_list[0]
for element in my_list:
if element > result:
result = element
return result
custom_max([1, 3, 2]) # the function checks if one element is greater than all the others
max([1, 2, str(1)]) # will give error
TypeError: '>' not supported between instances of 'str' and 'int'
max(str(1), "a", "b") # this works, all elements must be of the same type
max([1, 2.0]) # using max on ints and floats
def custom_min(my_list):
result = my_list[0]
for element in my_list:
if element < result:
result = element
return result
custom_min([1, 3, 0, 2, 1.5]) # the function checks if one element is less than all the others
sorted([3, 2,4, 1])
sorted(["Banna", "Ann", "Donna"])
print("I am Kevin\nI am the best") # \n is displayed as a newline
I am Kevin
I am the best
" ".join(['Antonio', 'Ruiz'])
" ".join(['Antonio', 'Ruiz'] + ['Leal'])
[1, 2] * 3
my_list = ['World']
my_list.append('e')
print(my_list)
['World', 'e']
location = 13.4, 103.5 # this is a tuple
location[0] # tuple indexing
latitude, longitude = location # tuple unpacking
latitude, longitude = 13.4, 103.5 # defining a tuple and then unpack it
print("Latitude:", latitude)
print("longitude:", longitude)
Latitude: 13.4
longitude: 103.5
lat, lon = [13.4, 103.5] # h
lat
empty_tuple = ()
type(empty_tuple)
empty_list = []
type(empty_list)
coordinates = 13.4, 103.8, 0.4
coordinates[1:3] # slicing
nested_list = [[1, 2], [3,4]]
print(nested_list)
[[1, 2], [3, 4]]
nested_tuple = ((1,2), (3,4))
print(nested_tuple)
((1, 2), (3, 4))
numbers = [1, 2, 6, 1, 4, 6 ,6, 1]
set(numbers)
fruits = {'apple', 'banana', 'coconut', 'durian'}
fruits = list(fruits)
fruits.append('eggplant')
print(fruits)
['coconut', 'apple', 'durian', 'banana', 'eggplant']
# a set is an unordered containe
fruits = {'apple', 'banana', 'coconut', 'durian'}
fruits.pop()
print(fruits)
{'coconut', 'banana', 'durian'}
fruits = ['apple', 'banana', 'coconut', 'durian']
fruits = set(fruits) # the printed value is not in order
print(fruits)
{'durian', 'banana', 'apple', 'coconut'}
elements = {"carbon": 6, "helium":2, "hydrogen":1}
type(elements)
elements = {"carbon": 6, "helium":2, "hydrogen":1}
elements["carbon"] # you can index into a dictionary using keys
elements = {"carbon": 6, "helium":2, "hydrogen":1}
elements.get('nitrogen')
elements = {"carbon": 6, "helium":2, "hydrogen":1}
"carbon" in elements
# identity operators check if two objects are identical
# two objects are identical if they point to the same thing (memory address)
a = 12
b = 11
a is a
# comparison operators check if two objects are equal
# two objects are equal if they have the same contents
a = 11
b = 12
c = 11
a == c
a = 11
b = 12
c = 11
a is c
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a is b # they both point to the same thing
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a is c
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a == c
a =10
b = 11
a is b - 1
print(hex(id(b - 1)))
print(hex(id(a)))
0x7f3678348560
0x7f3678348560
# TODO: Define a Dictionary that provides information on Indonesia's largest cities
# 1. name it population (assign it to a variable called "population")
# 2. The key is the name of a city (a string), and the associated value is its population in millions of people.
# Key | Value
# Jakarta | 10.1
# Surabaya | 2.84
# Bandung | 2.56
# Medan | 2.50
population = {"Jakarta": 10.1, "Surabaya": 2.84, "Bandung": 2.56, "Medan":2.50}
print(population)
{'Jakarta': 10.1, 'Surabaya': 2.84, 'Bandung': 2.56, 'Medan': 2.5}
%run -i students.py
Unsupported output type: clearOutput
The chosen one is: Owen Polim
# Exercise: How many unique words are there?
# In this exercise, you are given the text of a poem by Rudyard Kipling, "If"
# https://www.poetryfoundation.org/poems/46473/if---
# Your task is to find how many unique words there are in it.
# You can do this in three steps:
# 1. Split the text into words using the `.split(..)` method
# 2. Convert the list into a container (i.e. data structure) that keeps only unique elements
# 3. Get the length of the container
# this is the text of the poem "IF"
verse = "if you can keep your head when all about you are losing theirs and blaming it on you if you can trust yourself when all men doubt you but make allowance for their doubting too if you can wait and not be tired by waiting or being lied about don’t deal in lies or being hated don’t give way to hating and yet don’t look too good nor talk too wise"
# TODO: step 1 -- split the text into a list of words
verse_list = verse.split()
# TODO: step 2 -- convert the list into a set
verse_list = set(verse.split())
# TODO: step 3 -- get the number of elements in the set
number_of_elements = len(verse_list)
print("number of unique words: ", len(set(verse.split())))
number of unique words: 51