Statistics
5
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2026-02-18

AuthorMusa Arda

This book is an in-depth and activity-based introduction to the advanced level topics of Python programming. It follows a step-by-step practical approach by combining the theory of the language with hands-on coding exercises including quizzes, projects, assignments and exams. We begin by introducing the Collections module in Python. Then we cover Iterators, Generators, Date and Time Operations, Decorators and Context Managers in Python. By the end of the book, you will learn almost all of the advanced level concepts of Python in great detail by writing thousands of lines of code. All the supplementary resources (code files, quizzes, assignments, final exam etc.) are available for download at the GitHub repository. The link for the repository is provided in the book. This is the third book in Hands-On Python Series. And here is what you will find in this book Theory: In each topic, we will cover all the Theoretical Details with example coding. Coding Exercises: At the end of each chapter, we will have Coding Exercise, Quizzes. Projects: We will build projects in this book. You will learn how to apply Python concepts on real world problems. Assignments: After each project, you will have an Assignment. These assignments will let you build the project from scratch on your own. Final Exam: At the end of this book, you will have the Final Exam. It is a multiple-choice exam with 20 questions and a limited duration. The exam will let you to test your Python level. Contents 1. Introduction 2. Collections 3. Iterators 4. Generators 5. Date and Time 6. Decorators 7. Context Managers

Tags
No tags
ISBN: B09WVB49W8
Publisher: Kindle Edition
Publish Year: 2022
Language: 英文
Pages: 129
File Format: PDF
File Size: 649.6 KB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
HANDS-ON PYTHON with Exercises, Projects, Assignments & Final Exam ADVANCED Musa Arda
Hands-On Python with Exercises, Projects, Assignments & Final Exam: Advanced By Musa Arda Copyright © 2022 Musa Arda. All rights reserved. No portion of this book or its supplementary materials may be reproduced in any form without permission from the publisher, except as permitted by U.S. copyright law. For permissions contact: python.hands.on.book@gmail.com
Contents Preface 1. Introduction 2. Collections 3. Iterators 4. Generators 5. Date And Time 6. Decorators 7. Context Managers
Preface
About This Book This book is an in-depth and activity-based introduction to the advanced level topics of Python programming. It follows a step-by-step practical approach by combining the theory of the language with the hands-on coding exercises including quizzes, projects, assignments and exams. We begin by introducing the Collections module in Python. Then we cover Iterators, Generators, Date and Time Operations, Decorators and Context Managers in Python. By the end of the book, you will learn almost all of the advanced level concepts of Python in great detail by writing thousands of lines of code. All the supplementary resources (code files, quizzes, assignments, final exam etc.) are available for download at the GitHub repository. The link for the repository is provided in the book. This is the third book in Hands-On Python Series. And here is what you will find in this book: Theory: In each topic, we will cover all the Theoretical Details with example coding. Coding Exercises: At the end of each chapter, we will have Coding Exercise, Quizzes. Projects: We will build projects in this book. You will learn how to apply Python concepts on real world problems. Assignments: After each project, you will have an Assignment. These assignments will let you build the project from scratch on your own. Final Exam: At the end of this book, you will have the Final Exam. It is a multiple-choice exam with 20 questions and a limited duration. The exam will let you to test your Python level.
About Hands-On Python Series This is the third book in our Hands-On Python Series. And it covers the advanced level topics. So, we assume that you already know the introductory and intermediate concepts of Python programming like; Variables, Functions, Conditional Statements, Loops, Strings, Lists, Dictionaries, Tuples, Sets and Comprehensions, Exception Handling, File Operations and OOP. If you don’t feel comfortable with these topics, you are strongly recommended to finish the first two books in our series, which are the Beginner and Intermediate levels. Here, you can find them.[1] About the Author Musa Arda has Bachelor’s degree from Industrial Engineering in 2007, and he has been working as a Software Developer for more than 14 years. He uses many programming languages and platforms like; Python, C#, Java, JavaScript, SAP ABAP, SQL, React, Flutter and more. He creates online learning content and writes books to share his experience and knowledge with his students. His main purpose is to combine theory and hands-on practice in his teaching resources. That’s why there are hundreds of programming exercises, quizzes, tests, projects, exams and assignments in all of his courses and books. He is dedicated to help his students to learn programming concepts in depth via a practical and exiting way. How to Contact Us
Please feel free to get in contact with the author. To comment or ask technical questions about this book, you can send email to python.hands.on.book@gmail.com.
1. Introduction
Who Is This Book For? The goal of this book is to help students to learn Python programming language in a hands-on and project-based manner. With its unique style of combining theory and practice, this book is for: people who want to learn and practice advanced concepts in Python programming people who are already working with Python language What Can You Expect to Learn? The purpose of this book is to provide you a good introduction to the advanced topics of Python programming. In general, you will gain solid programming skills and grasp the main idea of software development. In particular, here are some highlights on what you can you expect to learn with this book. You will: learn & master advanced Python topics in a hands-on approach practice your Python knowledge with Quizzes and Coding Exercises build Real-World Project with Python and do Assignments related to these projects take the Final Exam on Python with 20 questions to assess your learning build Python applications with PyCharm and master it gain solid and profound Python Programming skills needed for a Python career
(This page has no text content)
Outline of This Book In Chapter 1, you will get the basics of the book. You will learn about this books approach to Python programming and how to get most out of it. In Chapter 2, you will learn the Collections module in Python. Collections are specialized container datatypes providing alternatives to Python’s general-purpose containers, dict, l is t, set, and tuple. You will learn; ChainMap, Counter, Deque, DefaultDict, NamedTuple, OrderedDict, UserDict, UserList, and UserString. In Chapter 3, you will learn Iterables and Iterators in Python. You will see the details of Iterator Protocol, how to loop through an Iterator, how to define custom Iterators and Infinite Iterators. Chapter 4 is on Generators in Python. Generators allow you to define Iterators more easily and efficiently. You will define custom Generators and learn the benefits of using them. In Chapter 5, you will learn all the details of Date and Time operations in Python, which are crucial for robust application development. You will learn the difference between Aware and Naive Objects and get the details of the classes in datetime module. In Chapter 6, you will meet Decorators. A very important concept in Python programming. You will learn how to define a decorator, how to chain them and how to use class syntax for creating new ones. In Chapter 7, you will learn the details of Context Managers, which are very handy tools when you need to deal with resource management in your code. You will see how to define and use a Context Manager in both class form and function form.
Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new URLs, email addresses, filenames, and file extensions. Bold Indicates new terms and important concepts to pay attention. Constant width Used for string literals and programming concepts within paragraphs. Constant width bold Used for program elements within paragraphs, such as variable or function names, data types, statements, and keywords. We will write our code in the code cells. Here is an example code cell with the cell number as 7:
Figure 1-1: A code cell example used in this book Using Code Examples You can find all the supplementary resources for the book (code files, quizzes, assignments, final exam etc.) available for download at https://github.com/musaarda/python-hands-on- book-advanced.
You & This Book This book is designed in a way that, you can learn and practice Python. At each chapter you will learn the basic concepts and how to use them with examples. Then you will have a quiz the end of the chapter. First you will try to solve the quiz questions on your own, then I will provide the solutions in detail. You will have projects after each block of core concepts. And after every project you will an assignment to test your understanding. This will be your path to learn real Python. Before we deep dive into Python, I want to give you some tips for how you can get most out of this book: Read the topics carefully before you try to solve the quizzes Try to code yourself while you are reading the concepts in the chapters Try to solve quizzes by yourself, before checking the solutions Read the quiz solutions and try to replicate them Code the projects line by line with the book Do the assignments (seriously) Do not start a new chapter before finishing and solving quiz of the previous one Repeat the topics you fail in the Final Exam Learning takes time, so give yourself enough time digest the concepts
2. Collections What are Collections Collections are specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, l is t, set, and tuple. A Container is a special-purpose object which is used to store different objects. It provides a way to access the contained objects and iterate over them. Python provides the collections module which implements container datatypes. In this chapter we will learn different classes in the collections module. You can find the PyCharm project for this chapter in the Github Repository of this book. Chapter Outline: ChainMap Counter Deque DefaultDict NamedTuple OrderedDict UserDict UserList UserString ChainMap A ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. It is often much faster than creating a new dictionary and running multiple update() calls. Syntax: class col lect ions.ChainMap(*maps)
A ChainMap groups multiple dicts or other mappings together to create a single, updateable view (list of dictionaries). If no maps are specified, a single empty dictionary is provided so that a new chain always has at least one mapping. The underlying mappings are stored in a list. That list is public and can be accessed or updated using the maps attribute. There is no other state. A ChainMap incorporates the underlying mappings by reference. So, if one of the underlying mappings gets updated, those changes will be reflected in ChainMap. All of the usual dictionary methods are supported. In addition, there is a maps attribute, a method for creating new subcontexts, and a property for accessing all but the first mapping: maps: A user updateable list of mappings. The list is ordered from first- searched to last-searched. It is the only stored state and can be modified to change which mappings are searched. The list should always contain at least one mapping. [1] : 1 # import ChainMap class from col lect ions module 2 from col lect ions import ChainMap 3 4 #– Def ining a ChainMap –# 5 numbers = {‘one’: 1 , ‘ two’: 2} 6 let ters = {‘a’ : ‘A’, ‘b’ : ‘B’} 7 8 # Def ine the ChainMap 9 chain_map = ChainMap(numbers , le t ters) 10 11 print(chain_map) [1] : ChainMap({‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’}) In cell 1, we define a ChainMap object ( chain_map) with two dictionaries. Then we print the ChainMap. As you see in the output, the result is a view of these dicts.
Accessing Keys and Values from ChainMap: We can access the keys and values of a ChainMap by using the keys() and values() methods. [2] : 1 #– Accessing Keys and Values from ChainMap –# 2 print(chain_map.keys()) 3 print(chain_map.values()) [2] : KeysView(ChainMap({‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’})) ValuesView(ChainMap({‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’})) As you see in the output of cell 2, the result of chain_map.keys() is a KeysView and the result of chain_map.values() is a ValuesView. Accessing Individual Values with Key Names: We can access individual values from a ChainMap by using the key name. This is exactly the same way what we do with regular dictionaries. [3] : 1 #– Accessing Individual Values with Key Names –# 2 print(chain_map[‘one’]) 3 print(chain_map[‘b’]) [3] : 1 B In cell 3, we access the values of the individual items in the underlying dictionaries of the ChainMap by using the key names as: chain_map[‘one’]. Adding a New Dictionary to ChainMap: ChainMap can contain any number of dictionaries in it. We use the built-in new_child() method to add new dictionaries to the ChainMap. The
new_child() method returns a new ChainMap containing a new map followed by all of the maps in the current instance. One point to note here is, the newly added dict will be placed at the beginning of the ChainMap. [4] : 1 #– Adding a New Dict ionary to ChainMap –# 2 variables = {‘x’ : 0 , ‘y’ : 1} 3 new_chain_map = chain_map.new_chi ld(var iables) 4 print(‘Old:’ , chain_map) 5 print(‘New:’ , new_chain_map) [4] : Old: ChainMap({‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’}) New: ChainMap({‘x’: 0 , ‘y’ : 1}, {‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’}) Get the List of Mappings in ChainMap: We use the maps attribute the get the list of all mappings in the ChainMap. [5] : 1 #– Get the Lis t of Mappings in ChainMap –# 2 print(chain_map.maps) [5] : [{‘one’: 1 , ‘ two’: 2}, {‘a’ : ‘A’, ‘b’ : ‘B’}] In cell 5, we get all the mappings (dictionaries) in the chain_map. As you see in the output, the maps attribute returns a l is t type object. Counter A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
Elements are counted from an iterable or initialized from another mapping (or counter). Here are some ways we create Counter objects in Python: [6] : 1 from col lect ions import Counter 2 3 # a new, empty counter 4 c1 = Counter() 5 print(c1) 6 7 # a new counter from an i terable 8 c2 = Counter(‘aabbbcddeeee’) 9 print(c2) 10 11 # a new counter from a mapping 12 c3 = Counter({‘orange’: 6 , ‘ red’ : 3 , ‘green’: 5}) 13 print(c3) 14 15 # a new counter from keyword args 16 c4 = Counter(cats=4, dogs=8) 17 print(c4) [6] : Counter() Counter({‘e’ : 4 , ‘b’ : 3 , ‘a’ : 2 , ‘d’ : 2 , ‘c’ : 1}) Counter({‘orange’: 6 , ‘green’: 5 , ‘ red’ : 3}) Counter({‘dogs’ : 8 , ‘cats’ : 4}) Counter objects have a dictionary interface except that they return a zero count for missing items instead of raising a KeyError: [7] : 1 # count of exis t ing element