A4: Python Essentials (Practice Exercises)
This article is a part of “Data Science from Scratch — Can I to I Can” series.
Hello guys,
It’s time to test your understanding of Python basics. Create a new jupyter notebook and try to solve the given tasks, if given, please follow the instructions.
Good luck!
Exercises:
(Solutions are provided at the end.)
- How to compute
5
to the power of3
? - Write python code toe get remainder if
9
is divided by2
. Is9
even? - Convert the string
s = 'i am in Alberta'
into upper case and into a list. - Convert the string
s = 'My name is #Qazi!'
into lower case and split at#
into a list. - We have two variables,
province = "Alberta"
andpopulation = 4.1
. Print"The total population of Alberta is 4.1 million"
usingformat()
along withprint()
statement. (hint: Create place holders in print statement and pass the initialized variables to the format to fill the place holders.) - Can you grab
Canada
from the given nested list:a = ['Alberta',2,[3,4],[5,['Canada',7,['hello']],10,15],1,7]
? Use indexing. - Create a list of the name of provinces (keys) from the given dictionary:
d = {'BC':'Victoria','AB':'Edmonton', 'ON':'Toronto', 'QC':'Quebec City'}
. - From the given nested dictionary, grab word
Montreal
.d = {'k1':'AB','k2':[1,{'BC':['SK','ON','Vancouver',{'Calgary':[1,2,3,'Montreal']}]}]}
. Little tricky! - What is the main difference between a
tuple
and alist
? - An element can appear multiple times in a set ?
True/False?
- You got email addresses, write a function that splits the website domain from the email
user_name@some_domain.ca
. (In other words, calling function with email as a parameter should returnsome_domain.com
) - Can you write a function that tells you if there is a word
Calgary
in the provided string? How to deal if the wordcity
is capitalized? - Write a function to return square of numbers from 1 to 5, use
range()
method to generate numbers. - Write
lambda expression
for the function in question 13, usemap()
andrange().
- Filter the words that starts with
'c'
from the given listveg_list = ['tomato','cabbage','meat','chicken','goat'] .
Uselambda expression
andfilter()
. For the given list, your out put will be['cabbage','chicken']
- You are a loyal customer and the store is offering discounts to their loyal customers:
- between 0 & 50 CAD, you get 10 % discounts,
- between 51 & 100 CAD, you get 20 % discounts,
- more than 100 CAD, you get 40 % discounts,
- Its Monday, and you are spending more than 50 CAD, you get a cherry on top! 10 CAD OFF
write a function to calculate how much the customer will pay for any spending. Tricky? You need to develop the logic! Take a pen and a notebook and do the calculations if needed!
Exercises overview — video lecture:
Solutions — video lecture:
Solutions:
1:
2:
3:
4:
5:
6:
7:
8:
9: Tuple is immutable.
10: True
11:
12:
13:
14:
15:
16: Try to write the function yourself. Get help from the videos.
See you in the next lecture, Good Luck!
Please click here for the next article/lecture (A5: NumPy (Part-1): Arrays, random module, array methods & attributes) in this series, thanks!
Note: This complete course is available on Udemy and SkillShare. It is encouraged to acknowledged the efforts and buy this course from these platforms. Stay tuned, a book is also in draft!
About Dr. Junaid Qazi:
Dr. Junaid Qazi is a Subject Matter Specialist, Data Science & Machine Learning Consultant. He is a Professional Development Coach, Mentor, Author, and Invited Speaker. He can be reached for consulting projects and/or professional development training via LinkedIn(https://www.linkedin.com/in/jqazi/) or through his company website(www.scienceacademy.ca)