A4: Python Essentials (Practice Exercises)

Junaid Qazi, PhD
4 min readJan 29, 2020

This article is a part of “Data Science from Scratch — Can I to I Can” series.

Click here for the previous article/lecture on “A3: Python Essentials (Part-2): Operators, conditional statements, loops, list comprehension, functions, lambda expression, map & filter”.

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.)

  1. How to compute 5 to the power of 3?
  2. Write python code toe get remainder if 9 is divided by 2. Is 9 even?
  3. Convert the string s = 'i am in Alberta' into upper case and into a list.
  4. Convert the string s = 'My name is #Qazi!' into lower case and split at # into a list.
  5. We have two variables, province = "Alberta" and population = 4.1 . Print "The total population of Alberta is 4.1 million" using format()along with print() statement. (hint: Create place holders in print statement and pass the initialized variables to the format to fill the place holders.)
  6. Can you grab Canada from the given nested list: a = ['Alberta',2,[3,4],[5,['Canada',7,['hello']],10,15],1,7]? Use indexing.
  7. Create a list of the name of provinces (keys) from the given dictionary: d = {'BC':'Victoria','AB':'Edmonton', 'ON':'Toronto', 'QC':'Quebec City'}.
  8. From the given nested dictionary, grab word Montreal. d = {'k1':'AB','k2':[1,{'BC':['SK','ON','Vancouver',{'Calgary':[1,2,3,'Montreal']}]}]}. Little tricky!
  9. What is the main difference between a tuple and a list?
  10. An element can appear multiple times in a set ? True/False?
  11. 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 return some_domain.com)
  12. Can you write a function that tells you if there is a word Calgary in the provided string? How to deal if the word city is capitalized?
  13. Write a function to return square of numbers from 1 to 5, use range() method to generate numbers.
  14. Write lambda expression for the function in question 13, use map() and range().
  15. Filter the words that starts with 'c' from the given list veg_list = ['tomato','cabbage','meat','chicken','goat'] . Use lambda expression and filter(). For the given list, your out put will be['cabbage','chicken']
  16. 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)

--

--

Junaid Qazi, PhD

We offer professional development, corporate training, consulting, curriculum and content development in Data Science, Machine Learning and Blockchain.