0% found this document useful (0 votes)
45 views

Python - Basic - 2 - Jupyter Notebook (Student)

The document provides an overview of topics to be covered in Python Fundamentals Day 2, including while loops, casting between data types, list and dictionary operations, list comprehensions, and lambda functions. It discusses using while loops until a condition is no longer true, casting strings to integers with str.isnumeric(), and using enumerate() to loop over a list with indexes. List and dictionary operations like list(), dict(), and tuple() are demonstrated along with valid type conversions between str, int, float, tuple, list and dict.

Uploaded by

Mesbahur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python - Basic - 2 - Jupyter Notebook (Student)

The document provides an overview of topics to be covered in Python Fundamentals Day 2, including while loops, casting between data types, list and dictionary operations, list comprehensions, and lambda functions. It discusses using while loops until a condition is no longer true, casting strings to integers with str.isnumeric(), and using enumerate() to loop over a list with indexes. List and dictionary operations like list(), dict(), and tuple() are demonstrated along with valid type conversions between str, int, float, tuple, list and dict.

Uploaded by

Mesbahur Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

Python Fundamentals Day 2


Welcome to Python Fundamentals Day 2.

So what is on the menu for today?

We start with the While loop and provide you some additional useful tricks to apply in the For loop.
We spend a bit of time on casting objects from one type to the other and explain you what these
__double__ underscores are about.

Then we go through the overviews of list and dictionary operations. There is quite a lot you can do
with these 2 powerfull and common used objects.

Then list comprehension. This is a very powerful construct that allows you to create a new list from
another while applying a filter and an expression to each item. It is a for loop inside a list.

And finally the nameless lambda function, which comes in very handy in numerous applications.

Table of Contents
More on loops
While loop
Range
Enumerate
Casting
Dunder methods
Summary - More on loops
while
range
enumerate
casting
dunder methods
List operations
Summary - List operations
list operations
localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 1/34
8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

RUN ME
Exercises - List operations
Exercise 1 - Month days
Exercise 2 - Get unevens
Exercise 3 - Sum top
Exercise 4 - Match ends
Dictionary operations
Summary - Dictionary operations
dictionary operations
RUN ME
Exercises - Dictionary operations
Exercise 1 - Numbers info
Exercice 2 - Cart checkout
Exercise 3 - Student Marks
List comprehension and lambda
List Comprehensions
Lambda
Summary - List comprehension and lambda
list comprehension
lambda
RUN ME
Exercises - List comprehension and lambda
Exercise 1 - Negative ints
Exercise 2 - Power odds
Exercise 3 - Word lengths
Exercise 4 - Celsius to Fahrenheit
Exercise 5 - Square odds lambda
Exercise 6 - Square odds lc
Exercise 7 - Extract A names lambda
Exercise 8 - Extract B names lc
Exercise 9 - Sort last
Exercise 10 - Coordinates

More on loops

While loop

Besides the for loop there is also the while loop.

The while loop continues iterating until it's condition stops being True.

Let's see it.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 2/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC
# THREE basic requirements in a loop:
# 1) start point,
# 2) end point,
# 3) what keeps it going from start to end

i = 0

while i < 10:
i += 1
print(i)

Let's write a code that asks the user for integer numbers until the total is 50.

In [ ]: #### MC

total = 0

while total < 50:
x = int(input())
total += x
print("total:", total, "x:", x)

What did we do here?

Notice the condition " total < 50". The loop keeps iterating until that condition stops being
True. The input() function asks the user for input. The int() function casts a string to a float.
This is called casting, we cast an object of one type to be of another type. Then we add the
resulting number x to the total . We repeat until the total is 50 or higher.

Now we want to check if the input string is convertible to an integer. Let's use dir(str) to see
which method we could use for that.

In [ ]: # MC

dir(str)

Question: Which string function can we use to check can cast a string (str) to an integer (int)?

Answer:

str.isnumeric,

str.isdigit,

str.isdecimal
all work

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 3/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

help(str.isnumeric)

We can now add this if-else statement to make it more robust.

In [ ]: ## MC

total = 0

while total < 50:
x = input()
if x.isnumeric():
total += int(x)
else:
print("{} is not a valid round number!".format(x))
print("total:", total, "x:", x)

What did we do here?

Variable x is a string. If that string is numeric it can be casted to an integer. Else, we print that it
is not a valid round number.

Range
The for loop is used often in combination with range(). Let's have a look.

In [ ]: # MC

help(range)

In [ ]: # MC

range(5)

In [ ]: # MC

for i in range(5):
print(i)

In [ ]: # MC

for i in range(0, 11, 2):
print(i)

Enumerate

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 4/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

Sometimes you want to loop over a list of items and also use the index numbers. For this we can
use the enumerate function.

In [ ]: # MC

items = ["banana", "orange", "apple"]

In [ ]: # MC

list(enumerate(items))

In [ ]: # MC

list(enumerate(items, start=1))

In [ ]: # MC

for i, item in enumerate(items, start=1):
print(i, item)

Casting

We casted a str to an int. We can do this for any of types. If the obj is not castable then we will
get an Exception.

Question: Which of these conversions will work?

In [ ]: # MC

str(True)

In [ ]: # MC

int("10")

In [ ]: # MC

float("10.0")

In [ ]: # MC

tuple([1,2,3])

In [ ]: # MC

list((1,2,3))

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 5/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

dict([1,2,3])

In [ ]: # MC

dict([('a', 1), ('b', 2)])

Dunder methods
So what's with all the '__blah__'?

Object methods with two underscores are called "dunder methods". This stands for double
underscore. These tell Python what to do in certain operations, for example __eq__ is a function
to compare with another object and __add__ handles the + operation under the hood.

In [ ]: # MC

dir(list)

We can get help on these methods as well.

In [ ]: # MC

help(list.__add__)

In [ ]: # MC

list_1 = [1,2,3]
list_2 = [4,5,6]

list3 = list_1 + list_2
list3

In [ ]: # MC

list_1.__add__(list_2)

So dunder method __add__ implements what happens when the + operator is applied.

Summary - More on loops

while

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 6/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

A while loop statement in Python programming language repeatedly executes a


target statement as long as a given condition is True.

while i < 5:

i+=1

print(i)

range

A sequence of integers from start (inclusive) to stop (exclusive) by step.

range(5)

range(1,6)

range(0,11,2)

for i in range(5):
print(i)

enumerate

Returns a tuple containing a count (default start s with 0) and the values
obtained from iterating over iterable.

items = ['orange','apple','mango']

enumerate(items)

for i,item in enumerate(items):

print(i,item)

casting

Specify a type on to a variable.

int("10")

float("10.2")

dict([('a',1), ('b', 2)])

dunder methods

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 7/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

Double underscore methods. This is how Python operations as + and == or


implemented for the object.

str.__add__

str.__eq__

List operations
The list is a very power object with many functionalities.

Here is an overview of the most important functionality.

len( dict ) – Gives the total length of the dictionary. This would be equal to the number of
items in the dictionary.
list.clear() – Removes all elements of the list
list.copy() – Returns a shallow copy of the list
list.append( element ) – adds a single element to the end of the list. Common error:
does not return the new list, just modifies the original.
list.insert( index , element ) – inserts the element at the given index, shifting
elements to the right.
list.extend( list2 ) adds the elements in list2 to the end of the list. Using + or += on
a list is similar to using extend().
list.index( element ) – searches for the given element from the start of the list and
returns its index. Throws a ValueError if the element does not appear (use in to check
without a ValueError).
item in list – check if the item is in the list
list.remove( element ) – searches for the first instance of the given element and removes
it (throws ValueError if not present)
list.sort() – sorts the list in place (does not return it).
sorted( list ) – return sorted list but keeps the original order of the list
list.reverse() – reverses the list in place (does not return it)
list.pop( index ) – removes and returns the element at the given index. returns the
rightmost element if index is omitted (roughly the opposite of append()).

This is to give a overview, so that you know of the existence of these functionalities.

Do you need to learn these by heart? No. Because you can always look it up using help() or
dir() or the online python documentation.

Let's go through these functionalities.

Create a list, show the size, clear it

In [ ]: # MC

numbers = [1,2,3,4]
numbers

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 8/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

len(numbers)

In [ ]: # MC

numbers.clear()
numbers

You can use the list.copy() method when you want to copy the list and make a new object out
of it.

In [ ]: # MC

numbers = [1,2,3,4]
numbers_2 = numbers
numbers_3 = numbers.copy()

In [ ]: print(id(numbers))
print(id(numbers_2))
print(id(numbers_3))

Append an item at the end.

In [ ]: # MC

numbers.append(5)

Notice this doesn't return a object. This is an in-place operation. The original object is changed.
Now 5 is added to the list. Let's see what happened to variable numbers_2 and variable
numbers_3 .

In [ ]: # MC

print(numbers)
print(numbers_2)
print(numbers_3)

In [ ]: print(id(numbers))
print(id(numbers_2))
print(id(numbers_3))

Be careful not to assign an in-place operation to a variable.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 9/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC
## wrong way

new_numbers = numbers.append(6)

print("numbers", numbers)
print("new_numbers", new_numbers)

Notice that the value of new_numbers is None. This is what happens when you assign an in-
place operation to a variable.

In [ ]: # MC
## correct way

numbers.append(6)
new_numbers = numbers

print("numbers", numbers)
print("new_numbers", new_numbers)

We can also insert an item anywhere in the list. This is also an in-place operation.

In [ ]: numbers

In [ ]: # MC

numbers.insert(2, 100) # insert at index = 2
numbers

We can also extend a list with another list. Notice this in an in-place operation.

In [ ]: # MC

numbers.extend([10, 20, 30])
numbers

We can add two lists with the + operator.

In [ ]: # MC

[1,2,3] + [4,5,6]

In [ ]: # MC

numbers.append([10, 20, 30])
numbers

We can get the index of the first occurence of an item.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 10/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

numbers.index(10)

And then get the list until the 10 value.

In [ ]: # MC

print(numbers)
print(numbers[0:numbers.index(10)])

Check if the list numbers holds 200.

In [ ]: # MC

200 in numbers

We can remove an item. It will remove the first occurence of that item.

In [ ]: # MC

print(numbers)
numbers.remove(6)
print(numbers)

We can sort the list in-place.

In [ ]: # MC

# numbers = [1, 2, 200, 5]
print(numbers)
numbers.sort()
print(numbers)

We can also sort reversed by adding keyword argument reverse = True. The sorted() built-
in function returns a sorted list but it does not affect the original.

In [ ]: # MC

print(sorted(numbers, reverse=True))
numbers

We can reverse a list in place.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 11/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

numbers = [10, 20, 500, 60, 50]
numbers.reverse()
numbers

The pop() function returns the last items and removes it from the list.

In [ ]: # MC

while numbers:
print(numbers, numbers.pop())

A list can be casted to a bool, which happens if a list is evaluated like here in the while
statement.

In [ ]: # MC

bool([1,2,3])

In [ ]: # MC

bool([])

## check whether list is not empty or list is empty

In [ ]: kit = ['medic', 'hotdog', 'soap', 555]



print('kit:', kit, end = '\n\n')

while kit:
print('I consumed', kit.pop())

print('\nkit:', kit)

Summary - List operations

list operations

The list is a powerful object with many functionalities. The list is a mutable
object.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 12/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

numbers = []

numbers = [1,2,3,4]

numbers[0]

numbers[0] == 100

numbers.append(0)

numbers.insert(1, 100) # insert at index 1

numbers.extend([5,6,7])

numbers.remove(5)

numbers.copy()

numbers.clear()

numbers.index(5)

numbers.sort()

numbers.sort(reverse=True)

item = numbers.pop()

sorted(numbers, reverse=True)

5 in numbers

len(numbers)

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

In [ ]: def test(got, expected):


if got == expected:
prefix = ' OK '
else:
prefix = ' FAIL '
print((f' {prefix} got: {got} expected: {expected}'))

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises - List operations

Exercise 1 - Month days


localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 13/34
8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

define a function that inputs a string and returns the numbers of days in that month.

Hints:

help(str.index)

In [ ]: months = ["January", "February", "March", "April", "May", "June", "July",


"August", "September", "October", "November", "December"]
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def month_days(month):
pass ## your code here

print("month_days", end = '\n\n')
test(month_days("January"), 31)
test(month_days("February"), 28)
test(month_days("September"), 30)
test(month_days("December"), 31)

In [ ]: # MC

def month_days(month):
months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

i = months.index(month)
return days[i]

print("month_days", end = '\n\n')
test(month_days("January"), 31)
test(month_days("February"), 28)
test(month_days("September"), 30)
test(month_days("December"), 31)

Exercise 2 - Get unevens

define a function called remove_even that returns a list after removing the even numbers from
it.

Hints: define a new list,

help(str.append)

In [ ]: num = [7, 8, 120, 25, 44, 20, 27]



def remove_even(numbers_list):
pass ## your code here

print("get_unevens", end = '\n\n')
test(remove_even(num), [7,25,27])

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 14/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

num = [7, 8, 120, 25, 44, 20, 27]
def remove_even(numbers_list):
new_numbers = []
for n in numbers_list:
if n % 2:
new_numbers.append(n)
return new_numbers

print("get_unevens", end = '\n\n')
test(remove_even(num), [7, 25, 27])

In [ ]: # MC
num = [7,8, 120, 25, 44, 20, 27]

def remove_even(number_list):
num1 = number_list.copy()
for i in number_list:
if i%2 == 0:
num1.remove(i)
return num1

print("get_unevens")
test(remove_even(num), [7,25,27])

Exercise 3 - Sum top

What is the total of the 3 highest numbers in this list?

Hints:

sorted(...)[:3]

In [ ]: numbers = [50, 23, 1, -50, 4, 400, 55, 32]



def sum_top(numbers_list):
pass ## your code here

print("sum_top", end = '\n\n')
test(sum_top(numbers), 505)

In [ ]: # MC
numbers = [50, 23, 1, -50, 4, 400, 55, 32]

def sum_top(numbers_list):
return sum(sorted(numbers_list, reverse = True)[:3])

print("sum_top", end = '\n\n')
test(sum_top(numbers), 505)

Exercise 4 - Match ends

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 15/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

Given a list of strings, return the count of strings which the string length is 2 or more and the first
and last chars of the string are the same.

In [ ]: text1 = (['aba', 'xyz', 'aa', 'x', 'bbb']) #3


text2 = (['', 'x', 'xy', 'xyx', 'xx']) #2
text3 = (['aaa', 'be', 'abc', 'hello']) #1

def match_ends(words):
counter = 0
for word in words:
pass # your code here
return counter

print('match ends', end = '\n\n')
test(match_ends(text1), 3)
test(match_ends(text2), 2)
test(match_ends(text3), 1)

In [ ]: # MC

text1 = (['aba', 'xyz', 'aa', 'x', 'bbb']) #3
text2 = (['', 'x', 'xy', 'xyx', 'xx']) #2
text3 = (['aaa', 'be', 'abc', 'hello']) #1

def match_ends(words):
counter = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
counter += 1
return counter

print('match ends', end = '\n\n')
test(match_ends(text1), 3)
test(match_ends(text2), 2)
test(match_ends(text3), 1)

In [ ]: # MC
## alternative

text1 = (['aba', 'xyz', 'aa', 'x', 'bbb'])
text2 = (['', 'x', 'xy', 'xyx', 'xx'])
text3 = (['aaa', 'be', 'abc', 'hello'])

def match_ends(words):
return len([word for word in words if (len(word) >= 2 and word.startswith(wor

print('match_ends', end = '\n\n')
test(match_ends(text1), 3)
test(match_ends(text2), 2)
test(match_ends(text3), 1)

Dictionary operations
localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 16/34
8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

A dictionary is a collection of key-value pairs. It is used to look up things and a very common used
type. Notice the { } curly brackets and the "key": "value" notation. Notice the squared bracket for
looking up.

len(dict) – Gives the total length of the dictionary. This would be equal to the number of
items in the dictionary.
dict.clear() -- Removes all elements of dictionary dict
dict.copy() – Returns a shallow copy of dictionary dict
dict.update(dict2) – Adds dictionary dict2's key-values pairs to dict
dict.fromkeys() – Create a new dictionary with keys from seq and values set to value.
dict.get(key, default=None) – For key key, returns value or default if key not in
dictionary
dict.setdefault(key, default=None) – Similar to get(), but will set dict[key]=default if
key is not already in dict
dict.items() – Returns an iterator of dict's (key, value) tuple pairs
dict.keys() – Returns iterator of dictionary dict's keys
dict.values() – Returns list of dictionary dict's values

Let's create a dictionary to go try out these functionalities.

In [ ]: # MC

prices = {"apple": 1.5, "orange": 3.25}
prices["apple"]

The dictionary has now 2 items.

In [ ]: # MC

len(prices)

We can copy and clear similar as a list.

In [ ]: # MC

prices_2 = prices
prices_3 = prices.copy()

print('before dict.clear()')
print(prices)
print(prices_2)
print(prices_3)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 17/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

prices.clear()

print('after dict.clear()')
print(prices)
print(prices_2)
print(prices_3)

In [ ]: # MC

prices = prices_3

The dictionary is mutable like a list. This is how we can update the dictionary with a new value.

In [ ]: # MC
prices = {"apple": 1.5, "orange": 3.25}

# MC - something that does not exist can be directly input

prices["banana"] = 2.30
prices

To update one dictionary with the another dictionary we can use update method. Notice this is an
in-place operation.

In [ ]: # MC

prices.update({"mango":1.22, "durian":5.60})
prices

You can initiate a dictionary with a list of keys.

In [ ]: # MC

help(dict.fromkeys)

In [ ]: # MC

keys = ["Jeremy", "Narjes", "Amin"]
dict.fromkeys(keys, [])

We can get values out of the dictionary in these 3 ways.

1. prices["orange"] -- simple. throws error when "orange" is not in the dict.


2. prices.get("orange") -- returns None or a default when "orange" is not in the dict.
3. prices.setdefault("orange", 1) -- returns None or a default when "orange" is not in the dict and
updates the dictionary with the default.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 18/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC
prices = {"apple": 1.5, "orange": 3.25}
prices["orange"]

Stay calm.

In [ ]: # MC

prices["pineapple"]

Since 'pineapple' is not in the dictionary this throws a KeyError.

In [ ]: # MC

prices.get('pineapple')

In [ ]: # MC

prices.get('pineapple', 1.0)

In [ ]: prices

In [ ]: # MC

print(prices)
print(prices.setdefault('pineapple', 1.0))
print(prices)

Let's say we have a dictionary of lists. We can use setdefault and append in one go.

In [ ]: # MC

fruits = {}

fruits.setdefault('pineapple', []).append(10)
fruits

Back to the fruit prices.

These 3 methods can be used for looping over dictionaries.

1. prices.keys() -- the keys


2. prices.values() -- the values
3. prices.items() -- the key-value pairs

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 19/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

prices.keys()

In [ ]: # MC

prices.values()

In [ ]: # MC

prices.items()

By default looping over a dictionary gives you the keys.

In [ ]: # MC

for fruit in prices:
print(fruit, prices[fruit])

The best way to loop over the key-value pairs is using prices.items()

In [ ]: # MC

for k, v in prices.items():
print(k,v)

Summary - Dictionary operations

dictionary operations

Dictionary. A collection of key-value pairs. Used to look up things. The dict class
provides many functionalities. The dict is a mutable object.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 20/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

prices = {}

prices = {"apple": 1.5, "orange":3.5, "banana":2.5}

prices["mango"] = 2.0

prices.update({"strawberry":4.0, "melon":10.0})

prices["orange"]

prices.get("pineapple", 1.0)

prices.setdefault("pineapple", 1.0)

prices.keys()

prices.values()

prices.items()

for k in price:

print(k)

for k,v in prices.items():

print(k,v)

prices.clear()

prices.copy()

prices.fromkeys(["apple", "orange", "banana"], 1.0)

len(prices)

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises - Dictionary operations

Exercise 1 - Numbers info

Given a list of numbers return a dictionary with info about the numbers. The dictionary should
contain keys "len", "max", "min", "sum", "avg" with those measures as the respective values.
Round avg to 2 decimals.

Hints: sum()/len()

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 21/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: numbers = [1, 4, 9, 16, 25]



def info(numbers):
## your code

print("numbers_info")
test(info(numbers), {'avg': 11.0, 'len': 5, 'max': 25, 'min': 1, 'sum': 55})

In [ ]: # MC

numbers = [1, 4, 9, 16, 25]

def info(numbers):
result = {"len": len(numbers),
"max": max(numbers),
"min": min(numbers),
"sum": sum(numbers),
"avg": round(sum(numbers) / len(numbers),2)}
return result

print("numbers_info")
test(info(numbers), {'avg': 11.0, 'len': 5, 'max': 25, 'min': 1, 'sum': 55})

Exercise 2 - Cart checkout

Compute the total price of this cart. By default everything cost RM 1, except when prices are
written.

Hints: dict.get(), dict.items()

In [ ]: cart = {'apple':12,


'banana': 1,
'orange': 2,
'pear':10}

prices = {'mango':12,
'apple':2.5,
'orange':5.5,
'banana':3}

In [ ]: for k in cart:


if k not in prices.keys():
print(k)

In [ ]: total=0

## your code

print("cart_checkout")
test(total, 54.0)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 22/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

total=0

for k,v in cart.items():
print(k, v)
total += v * prices.get(k, 1)

print("cart_checkout")
test(total, 54.0)

Exercise 3 - Student Marks

1. Write a function that add a mark to a student. Make sure the function can handle new
students!
2. Add 20 to Narjes, add 11 to Jeremy and add 5 to Darren
3. Compute the average mark of Jeremy. Round it to 2 decimals

Hints: dict.setdefault, sum()/len()

In [ ]: grades = {
'Amin':[12, 14, 20],
'Jeremy':[5, 12],
'Narjes':[10]
}

# 1. Write a function that adds a grade to a student.

def add_grade(student_name, grade):
## your code here

# 2. Add 20 to Narjes, add 11 to Jeremy and add 5 to Darren



## your code here

# 3. Compute the average grade of Jeremy

average_jeremy = ## your code here

# TEST
print("grade_students")
test(grades, {'Amin': [12, 14, 20], 'Jeremy': [5, 12, 11], 'Narjes': [10, 20], 'D
test(average_jeremy, 9.33)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 23/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

grades = {
'Amin':[12, 14, 20],
'Jeremy':[5, 12],
'Narjes':[10]
}

# 1. Write a function that adds a grade to a student.

def add_grade(student_name, grade):
grades.setdefault(student_name, []).append(grade)

# 2. Add 20 to Narjes, add 11 to Jeremy and add 5 to Darren



add_grade("Narjes", 20)
add_grade("Jeremy", 11)
add_grade("Darren", 5)

# 3. Compute the average grade of Jeremy

gj = grades["Jeremy"]
average_jeremy = round(sum(gj)/len(gj), 2)

# TEST
print("grade_students")
test(grades, {'Amin': [12, 14, 20], 'Jeremy': [5, 12, 11], 'Narjes': [10, 20], 'D
test(average_jeremy, 9.33)

List comprehension and lambda

List Comprehensions
A list comprehension is a compact expression to create a new list out of another list while applying
filters and expressions. All in one line!

We have a list of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

We want to create a new list with these numbers, but we only want the uneven numbers and we
want to square those.

This will give us: 1, 9, 25, 49, 81

Let's try.

In [ ]: numbers = [1,2,3,4,5,6,7,8,9,10]

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 24/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

newlist = [n for n in numbers]
newlist

Now we have a new list with the same items.

Let's filter so that we keep the uneven numbers.

In [ ]: numbers = [1,2,3,4,5,6,7,8,9,10]


ans =[]

for n in numbers:
if n%2!=0:
ans.append(n)

In [ ]: # MC

newlist = [n for n in numbers if n%2!=0]
newlist

Now let's square those odd numbers

In [ ]: # MC

newlist = [n**2 for n in numbers if n%2!=0]
newlist

Done! All in one line.

So what did we do here?

We looped over the numbers, applied a filter and applied an expression on each item n.

iterable: numbers
element: n
condition: n%2!=0
expression: n**2

[ expression(element) for element in iterable if condition(element) ]

Lambda
The lambda is a nameless function.

Sometimes your want to define a function for a single time usage. In that case a lambda function is
more elegant than predefining a whole named function, that you will only use once.

Lambda functions are mainly used in combination with the functions sort(), filter() and map().

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 25/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: tuples = [(40,1,1), (1,2,3,4), (1000,2), (-1,5,5,5,5,55,5,5)]

Let's say we want to sort this list of tuples based in the total value of the tuple.

In [ ]: # MC

sorted(tuples)

By default it is sorted based on the first value.

In [ ]: # MC

help(sorted)

In [ ]: def total(item):


return sum(item)

sorted(tuples, key=total)

The nameless lambda function enables us to do this in a more clean way.

In [ ]: # MC

sorted(tuples, key=lambda x: sum(x))

We can also filter based on the last item, which is indicated by [-1]

In [ ]: # MC

sorted(tuples, key=lambda x: x[-1])

The filter() function applies a filter function on the list. The outcome of the filter function is a
boolean.

Let's filter out the even numbers.

In [ ]: # MC

numbers = [1,2,3,4,5]
filter(lambda x: x%2==0, numbers)

We got a filter object. To see the result we need to cast it to list.

In [ ]: # MC

list(filter(lambda x: x%2==0, numbers))

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 26/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

And now apply a mapping using map() to take the power of 3

In [ ]: # MC

def power_2(x):
return x**2

list(map(lambda x: x**2, numbers))

In [ ]: # MC

list(map(lambda x: x**2, filter(lambda x: x%2==0, numbers)))

Summary - List comprehension and lambda

list comprehension

A compact way to process all or part of the elements in a sequence and return a
list with the results.

numbers = [1,2,3,4,5]

newlist = [n for n in numbers]

newlist = [n for n in numbers if n%2==0]

newlist = [n**2 for n in numbers if n%2==0]

lambda

An anonymous inline function consisting of a single expression which is evaluated


when the function is called. The syntax to create a lambda function is lambda
[parameters]: expression

sorted(numbers, key=lambda x: sum(x))

filter(lambda x: x%2==0, numbers)

map(lambda x: x**2, numbers)

RUN ME
Please run the below code snippet. It is required for running tests for your solution.

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 27/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: def test(got, expected):


max_width_print = min([int(79/3),
len(repr(got)) + 10,
len(repr(expected)) + 10])

if got == expected:
prefix = ' OK '.center(max_width_print, '=')
else:
prefix = ' FAIL '.center(max_width_print, '=')
print(('%s\n got: %s \nexpected: %s' % (prefix, repr(got), repr(expected)
end = '\n\n')

In [ ]: test('a', 'ab')


test('a', 'a')

Exercises - List comprehension and lambda

Exercise 1 - Negative ints

Write a list comprehension to create a new list that contains only the negative numbers as integers
and store it as newlist.

In [ ]: numbers = [34.6, -203.4, 44.9, -68.3, -12.2, 44.6, 12.7]



newlist = ## your code

# TEST
print("negative_ints")
test(newlist, [-203, -68, -12])

In [ ]: # MC

numbers = [34.6, -203.4, 44.9, -68.3, -12.2, 44.6, 12.7]

newlist = [int(n) for n in numbers if n < 0]

# TEST
print("negative_ints")
test(newlist, [-203, -68, -12])

Exercise 2 - Power odds

Write one line of Python that takes this list and makes a new list that only has the odd elements
and power it with 3.

Hints: x**3

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 28/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: numbers = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

result = ##

print("power_odds")
test(result, [1, 729, 15625, 117649, 531441])

In [ ]: # MC

numbers = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

result = [n**3 for n in numbers if n%2!=0]

print("power_odds")
test(result, [1, 729, 15625, 117649, 531441])

Exercise 3 - Word lengths

Write a list comprehsion to determine the length of each word except 'the' and store as
word_lengths

In [ ]: sentence = "the quick brown fox jumps over the lazy dog"

## your code
word_lengths = ## your code

# TEST
print()
test(word_lengths, [5, 5, 3, 5, 4, 4, 3])

In [ ]: # MC

sentence = "the quick brown fox jumps over the lazy dog"

words = sentence.split()
word_lengths = [len(w) for w in words if w != "the"]

# TEST
print("words_lengths")
test(word_lengths, [5, 5, 3, 5, 4, 4, 3])

Exercise 4 - Celsius to Fahrenheit

Convert the following list of temperatures in Celsius to Fahrenheit. Round the results to 2 decimals.

Fahrenheit = 9/5*Celsius + 32

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 29/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: celsius = [43.6, 67.3, 43.6, 56.4]


fahrenheit = ## your code here

# TEST
print("celsius_to_fahrenheit")
test(fahrenheit, [110.48, 153.14, 110.48, 133.52])

In [ ]: # MC

celsius = [43.6, 67.3, 43.6, 56.4]
fahrenheit = [round((9/5)*c + 32, 2) for c in celsius]

# TEST
print("celsius_to_fahrenheit")
test(fahrenheit, [110.48, 153.14, 110.48, 133.52])

Exercise 5 - Square odds lambda

Write a lambda function that squares number for all odd numbers from 1 to 100

Hints: filter(), map(), list()

In [ ]: numbers = list(range(1,100+1))



answer = ## your code


# TEST
print("square_odds_lambda")
test_answer = [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841
test(answer, test_answer)

In [ ]: # MC

numbers = list(range(1,100+1))

answer = list(map(lambda x: x**2,filter(lambda x: x%2!=0, numbers)))


# TEST
print("square_odds_lambda")
test_answer = [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841
test(answer, test_answer)

Exercise 6 - Square odds

Write a list comprehension that square number for all odd numbers from 1 to 100

Hints: range(1,100)

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 30/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: answer = ## your code here



# TEST
test(answer, [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841,
961, 1089, 1225, 1369, 1521, 1681, 1849, 2025, 2209, 2401, 2601, 28
3025, 3249, 3481, 3721, 3969, 4225, 4489, 4761, 5041, 5329, 5625, 5
7225, 7569, 7921, 8281, 8649, 9025, 9409, 9801])

In [ ]: # MC

answer = [n**2 for n in range(1,101) if n%2!=0]

# TEST
test(answer, [1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841,
961, 1089, 1225, 1369, 1521, 1681, 1849, 2025, 2209, 2401, 2601, 28
3025, 3249, 3481, 3721, 3969, 4225, 4489, 4761, 5041, 5329, 5625, 5
7225, 7569, 7921, 8281, 8649, 9025, 9409, 9801])

Exercise 7 - Extract A names lambda

Write a lambda function to extract names that begin with 'A'

In [ ]: names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']


answer =

# TEST
test(answer, ['Anne', 'Amy'])

In [ ]: # MC

names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
answer = list(filter(lambda x: x.startswith("A"), names))

# TEST
print("extract_a_names_lambda")
test(answer, ['Anne', 'Amy'])

Exercise 8 - Extract B names lc

Write a list comprehension to extract the names that begin with 'B'

In [ ]: names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']


answer = ## your code here

# TEST
print("extract_b_names_lc")
test(answer, ['Bob', 'Barbara'])

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 31/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
answer = [n for n in names if n.startswith("B")]

# TEST
print("extract_b_names_lc")
test(answer, ['Bob', 'Barbara'])

Exercise 9 - Sort last

Given a list of non-empty tuples, return a list sorted in increasing


order by the last element in each
tuple.

e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]

Hint: use a custom key= function to extract the last element form each tuple.

In [ ]: #output: [(2, 1), (3, 2), (1, 3)]


list1 = [(1, 3), (3, 2), (2, 1)]

#output: [(3, 1), (1, 2), (2, 3)]
list2 = [(2, 3), (1, 2), (3, 1)]

#output: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
list3 = [(1, 7), (1, 3), (3, 4, 5), (2, 2)]

def sort_last(tuples):
## your code here


# TEST
test(sort_last(list1), [(2, 1), (3, 2), (1, 3)])
test(sort_last(list2), [(3, 1), (1, 2), (2, 3)])
test(sort_last(list3), [(2, 2), (1, 3), (3, 4, 5), (1, 7)])

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 32/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

In [ ]: # MC

#output: [(2, 1), (3, 2), (1, 3)]
list1 = [(1, 3), (3, 2), (2, 1)]

#output: [(3, 1), (1, 2), (2, 3)]
list2 = [(2, 3), (1, 2), (3, 1)]

#output: [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
list3 = [(1, 7), (1, 3), (3, 4, 5), (2, 2)]

def sort_last(tuples):
return sorted(tuples, key=lambda x: x[-1])

# TEST
print("sort_last")
test(sort_last(list1), [(2, 1), (3, 2), (1, 3)])
test(sort_last(list2), [(3, 1), (1, 2), (2, 3)])
test(sort_last(list3), [(2, 2), (1, 3), (3, 4, 5), (1, 7)])

Exercise 10 - Coordinates

Convert the following for loop code to list comprehension

Hint: double list comprehension

In [ ]: coords = []
for x in range(4):
for y in range(2):
coordinate = (x, y)
coords.append(coordinate)
print(coords)

In [ ]: coords_lc = ## your code here



# TEST
print("coordinates")
test(coords_lc, [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)])

In [ ]: # MC

coords_lc = [(x,y) for x in range(4) for y in range(2)]

# TEST
print("coordinates")
test(coords_lc, [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)])

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 33/34


8/22/22, 9:19 PM Python_Day2_MC - Jupyter Notebook

localhost:8890/notebooks/2022/22Aug/PRJ63504 Capstone (Python)/CADS/Python for Analytics (Basic)/MC/Day 2/Python_Day2_MC.ipynb 34/34

You might also like