ebook img

The Python Workshop: Write Python code to solve challenging real-world problems, 2nd Edition PDF

600 Pages·2022·69.869 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Python Workshop: Write Python code to solve challenging real-world problems, 2nd Edition

The Python Workshop Write Python code to solve challenging real-world problems Corey Wade Mario Corchero Jiménez Andrew Bird Dr. Lau Cher Han Graham Lee BIRMINGHAM—MUMBAI The Python Workshop Copyright © 2022 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Group Product Manager: Gebin George Publishing Product Manager: Kunal Sawant Senior Editor: Rohit Singh Technical Editor: Pradeep Sahu Copy Editor: Safis Editing Project Coordinator: Deeksha Thakkar Proofreader: Safis Editing Indexer: Subalakshmi Govindhan Production Designer: Vijay Kamble Business Development Executive: Debadrita Chatterjee Marketing Coordinator: Sonakshi Bubbar First published: November 2019 Second edition: October 2022 Production reference: 1271022 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80461-061-9 www.packt.com Contributors About the authors Corey Wade, MS Mathematics, MFA Writing & Consciousness, is the director and founder of Berkeley Coding Academy where he teaches Python and data science to teenagers from around the world. A regular contributor to Towards Data Science, Corey has also authored Hands-On Gradient Boosting with XGBoost and scikit-learn. Corey has taught math and programming for 18 years at the Independent Study program of Berkeley High School. When not teaching or building machine learning models in Python, Corey reads poetry and studies the stars. You can visit his website at berkeleycodingacademy.com. Mario Corchero Jiménez is a senior software developer at Bloomberg. He leads the Python infrastructure team, enabling the company to work effectively with Python and building company-wide libraries and tools. His professional experience is mainly in C++ and Python and he has contributed patches to multiple Python open source projects. He is a PSF fellow, having received the PSF Q3 2018 Community Award, and the vice president of Python España (Spain’s Python association). He has also served as the chairperson of PyLondinium, PyConES17, and PyCon Charlas at PyCon 2018. Mario is passionate about the Python community, open source and inner source. Andrew Bird is the data and analytics manager at Vesparum Capital. He leads the software and data science teams at Vesparum, overseeing full-stack web development in Django and React. He is an Australian actuary (FIAA, CERA) who has previously worked with Deloitte Consulting in financial services. Andrew also currently works as a full-stack developer for Draftable Pvt. Ltd. He voluntarily manages the ongoing development of the donation portal for the Effective Altruism Australia website. Dr. Lau Cher Han is a chief data scientist and is currently the CEO of LEAD, an institution that provides programs on data science, full-stack web development, and digital marketing. Well-versed in programming languages such as JavaScript, Python, C#, and so on, he is experienced in MEAN Stack, ASP.NET, and Python Django web frameworks. He is multilingual and speaks English, Chinese, and Bahasa fluently. His knowledge of Chinese even includes its dialects of Hokkien, Teochew, and Cantonese. Graham Lee is an experienced programmer and writer. He has written several books, including Professional Cocoa Application Security, Test-Driven iOS Development, APPropriate Behaviour, and APPosite Concerns. He is a developer who’s been programming for long enough to want to start telling other people about the mistakes he’s made in the hope that they’ll avoid repeating them. In his case, this means having worked for about 12 years as a professional. His first programming experience can hardly be called professional at all, as it was in BASIC on a Dragon 32 microcomputer. About the reviewer Jeyapriya Udayanakumar is currently an associate professor at Stella Maris College, Chennai, India, and she has been pursuing her passion for teaching for more than 20 years. She has an interest in computer programming and believes that programming should be taught to students as an art. She has guided many student projects and been a part of some of them in real time. She has headed the department of computer science for 7 years and has been the dean of academic affairs for 3 years. She is known for her teaching skills and competency, has a penchant for clarity in terms of gauging goals and learning outcomes, and can connect with learners and assess their needs with ease. She has completed a master’s degree in computer applications. Table of Contents Preface xix 1 Python Fundamentals – Math, Strings, Conditionals, and Loops 1 Overview 1 Docstrings 17 Introduction 1 Activity 2 – finding the area of a triangle 17 Technical requirements 2 Strings – concatenation, Opening a Jupyter Notebook 3 methods, and input() 18 String syntax 18 Python as a calculator 4 Exercise 7 – string error syntax 19 Standard math operations 4 Escape characters 20 Basic math operations 5 Multiline strings 21 Order of operations 7 The print() function 21 Exercise 1 – getting to know Exercise 8 – displaying strings 21 the order of operations 7 String operations and concatenation 23 Python concept – spacing 8 Exercise 9 – string concatenation 23 Number types – integers and floats 8 Exercise 2 – integer and float types 9 String interpolation 24 Complex number types 10 Comma separators 24 Errors in Python 10 f-strings 24 Variable assignment 11 The len() function 25 Exercise 3 – assigning variables 11 String methods 25 Casting – changing types 12 Exercise 10 – implementing string methods 26 Activity 1 – assigning values to variables 13 Casting 27 Variable names 13 Exercise 11 – types and casting 27 Exercise 4 – naming variables 14 The input() function 28 Multiple variables 15 Exercise 12 – using the input() function 29 Exercise 5 – assigning multiple variables 15 Activity 3 – using the input() Comments 16 function to rate your day 30 Exercise 6 – comments in Python 16 viii Table of Contents String indexing and slicing 30 if else 41 Indexing 30 Exercise 17 – using the if-else syntax 41 The elif statement 42 Slicing 32 Loops 43 Strings and their methods 33 while loops 43 Booleans and conditionals 34 The break keyword 45 Booleans 34 Activity 4 – finding the least common Exercise 13 – Boolean variables 34 multiple (LCM) 46 Logical operators 35 Programs 46 Comparison operators 36 Exercise 18 – calculating perfect squares 47 Exercise 14 – comparison operators 37 Exercise 19 – real estate offer 48 Comparing strings 39 for loops 50 Exercise 15 – practicing comparing strings 39 Exercise 20 – using for loops 50 Conditionals 39 The continue keyword 53 The if syntax 39 Activity 5 – building conversational Indentation 40 bots using Python 55 Exercise 16 – using the if syntax 40 Summary 56 2 Python Data Structures 57 Overview 57 Activity 6 – using a nested list to store employee data 67 Introduction 57 Technical requirements 59 Matrix operations 68 The power of lists 59 Exercise 27 – implementing matrix operations (addition and subtraction) 69 Exercise 21 – working with Python lists 60 Matrix multiplication operations 70 List methods 61 Exercise 28 – implementing matrix Exercise 22 – basic list operations 61 operations (multiplication) 71 Accessing an item from a list 62 Dictionary keys and values 72 Exercise 23 – accessing an item from Exercise 29 – using a dictionary to store shopping list data 62 a movie record 73 Adding an item to a list 63 Activity 7 – storing company employee Exercise 24 – adding items to our shopping list 64 table data using a list and a dictionary 75 Exercise 25 – looping through a list 65 Dictionary methods 76 Matrices as nested lists 65 Exercise 26 – using a nested list to store Exercise 30 – accessing a dictionary data from a matrix 66 using dictionary methods 76 Table of Contents ix Tuples 77 A survey of sets 80 Exercise 31 – exploring tuple properties Exercise 33 – using sets in Python 81 in a dance genre list 78 Set operations 82 Zipping and unzipping dictionaries Exercise 34 – implementing set operations 83 and lists using zip() 79 Choosing types 85 Exercise 32 – using the zip() method to manipulate dictionaries 79 Summary 87 3 Executing Python – Programs, Algorithms, and Functions 89 Overview 89 Exercise 43 – defining and calling a function in the shell 107 Introduction 89 Exercise 44 – defining and calling Technical requirements 90 a function in a Python script 108 Python scripts and modules 90 Exercise 45 – importing and calling Exercise 35 – writing and executing the function from the shell 109 our first script 91 Positional arguments 109 Python function example 92 Keyword arguments 110 Exercise 36 – writing and importing Exercise 46 – defining a function our first module 92 with keyword arguments 110 Shebangs in Ubuntu 93 Exercise 47 – defining a function Docstrings 94 with positional and keyword arguments 110 Exercise 37 – adding a docstring to Exercise 48 – using **kwargs 111 my_module.py 94 Activity 9 – formatting customer names 112 Importing libraries 95 Iterative functions 113 Exercise 38 – finding the system date 96 Exercise 49 – a simple function The if __name__ == ‘__main__’ statement 97 with a for loop 114 Activity 8 – what’s the time? 98 Exiting early 114 Python algorithms 98 Exercise 50 – exiting the function during Exercise 39 – finding the maximum number 99 the for loop 114 Time complexity 100 Activity 10 – the Fibonacci function with an iteration 115 Sorting algorithms 101 Exercise 40 – using bubble sort in Python 103 Recursive functions 116 Searching algorithms 104 A terminating case 117 Exercise 41 – linear search in Python 104 Exercise 51 – recursive countdown 118 Exercise 42 – binary search in Python 106 Exercise 52 – factorials with iteration Basic functions 107 and recursion 118 Activity 11 – the Fibonacci function

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.