Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Learn Python Programming, 3rd edition
Learn Python Programming, 3rd edition

Learn Python Programming, 3rd edition: An in-depth introduction to the fundamentals of Python , Third Edition

Arrow left icon
Profile Icon Fabrizio Romano Profile Icon Heinrich Kruger
Arrow right icon
€25.99 €28.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (34 Ratings)
eBook Oct 2021 554 pages 3rd Edition
eBook
€25.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Fabrizio Romano Profile Icon Heinrich Kruger
Arrow right icon
€25.99 €28.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (34 Ratings)
eBook Oct 2021 554 pages 3rd Edition
eBook
€25.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€25.99 €28.99
Paperback
€35.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Learn Python Programming, 3rd edition

Built-In Data Types

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

– Sherlock Holmes, in The Adventure of the Copper Beeches

Everything you do with a computer is managing data. Data comes in many different shapes and flavors. It's the music you listen to, the movies you stream, the PDFs you open. Even the source of the chapter you're reading at this very moment is just a file, which is data.

Data can be simple, whether it is an integer number to represent an age, or complex, like an order placed on a website. It can be about a single object or about a collection of them. Data can even be about data—that is, metadata. This is data that describes the design of other data structures, or data that describes application data or its context. In Python, objects are our abstraction for data, and Python has an amazing variety of data structures that you can use to represent data or combine them to create your own custom data.

In this chapter, we are going to cover the following:

  • Python objects' structures
  • Mutability and immutability
  • Built-in data types: numbers, strings, dates and times, sequences, collections, and mapping types
  • The collections module
  • Enumerations

Everything is an object

Before we delve into the specifics, we want you to be very clear about objects in Python, so let's talk a little bit more about them. As we already said, everything in Python is an object. But what really happens when you type an instruction like age = 42 in a Python module?

If you go to http://pythontutor.com/, you can type that instruction into a text box and get its visual representation. Keep this website in mind; it's very useful to consolidate your understanding of what goes on behind the scenes.

So, what happens is that an object is created. It gets an id, the type is set to int (integer number), and the value to 42. A name, age, is placed in the global namespace, pointing to that object. Therefore, whenever we are in the global namespace, after the execution of that line, we can retrieve that object by simply accessing it through its name: age.

If you were to move house, you would put all the knives, forks, and spoons in a box and label it cutlery. This is exactly the same concept. Here is a screenshot of what it may look like (you may have to tweak the settings to get to the same view):

Figure 2.1: A name pointing to an object

So, for the rest of this chapter, whenever you read something such as name = some_value, think of a name placed in the namespace that is tied to the scope in which the instruction was written, with a nice arrow pointing to an object that has an id, a type, and a value. There is a little bit more to say about this mechanism, but it's much easier to talk about it using an example, so we'll come back to this later.

Mutable or immutable? That is the question

The first fundamental distinction that Python makes on data is about whether or not the value of an object can change. If the value can change, the object is called mutable, whereas if the value cannot change, the object is called immutable.

It is very important that you understand the distinction between mutable and immutable because it affects the code you write; take this example:

>>> age = 42
>>> age
42
>>> age = 43  #A
>>> age
43

In the preceding code, on line #A, have we changed the value of age? Well, no. But now it's 43 (we hear what you are saying...). Yes, it's 43, but 42 was an integer number, of the type int, which is immutable. So, what happened is really that on the first line, age is a name that is set to point to an int object, whose value is 42. When we type age = 43, what happens is that another object is created, of the type int and value 43 (also, the id will be different), and the name age is set to point to it. So, in fact, we did not change that 42 to 43—we actually just pointed age to a different location, which is the new int object whose value is 43. Let's see the same code also printing the IDs:

>>> age = 42
>>> id(age)
4377553168
>>> age = 43
>>> id(age)
4377553200

Notice that we print the IDs by calling the built-in id() function. As you can see, they are different, as expected. Bear in mind that age points to one object at a time: 42 first, then 43—never together.

If you reproduce these examples on your computer, you will notice that the IDs you get will be different. This is of course expected, as they are generated randomly by Python, and will be different every time.

Now, let's see the same example using a mutable object. For this example, let's just use a Person object, that has a property age (don't worry about the class declaration for now—it is there only for completeness):

>>> class Person:
...     def __init__(self, age):
...         self.age = age
...
>>> fab = Person(age=42)
>>> fab.age
42
>>> id(fab)
4380878496
>>> id(fab.age)
4377553168
>>> fab.age = 25  # I wish!
>>> id(fab)  # will be the same
4380878496
>>> id(fab.age)  # will be different
4377552624

In this case, we set up an object fab whose type is Person (a custom class). On creation, the object is given the age of 42. We then print it, along with the object ID, and the ID of age as well. Notice that, even after we change age to be 25, the ID of fab stays the same (while the ID of age has changed, of course). Custom objects in Python are mutable (unless you code them not to be). Keep this concept in mind, as it's very important. We'll remind you about it throughout the rest of the chapter.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Extensively revised with richer examples, Python 3.9 syntax, and new chapters on APIs and packaging and distributing Python code
  • Discover how to think like a Python programmer
  • Learn the fundamentals of Python through real-world projects in API development, GUI programming, and data science

Description

Learn Python Programming, Third Edition is both a theoretical and practical introduction to Python, an extremely flexible and powerful programming language that can be applied to many disciplines. This book will make learning Python easy and give you a thorough understanding of the language. You'll learn how to write programs, build modern APIs, and work with data by using renowned Python data science libraries. This revised edition covers the latest updates on API management, packaging applications, and testing. There is also broader coverage of context managers and an updated data science chapter. The book empowers you to take ownership of writing your software and become independent in fetching the resources you need. You will have a clear idea of where to go and how to build on what you have learned from the book. Through examples, the book explores a wide range of applications and concludes by building real-world Python projects based on the concepts you have learned.

Who is this book for?

This book is for everyone who wants to learn Python from scratch, as well as experienced programmers looking for a reference book. Prior knowledge of basic programming concepts will help you follow along, but it’s not a prerequisite.

What you will learn

  • Get Python up and running on Windows, Mac, and Linux
  • Write elegant, reusable, and efficient code in any situation
  • Avoid common pitfalls like duplication, complicated design, and over-engineering
  • Understand when to use the functional or object-oriented approach to programming
  • Build a simple API with FastAPI and program GUI applications with Tkinter
  • Get an initial overview of more complex topics such as data persistence and cryptography
  • Fetch, clean, and manipulate data, making efficient use of Python's built-in data structures

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 29, 2021
Length: 554 pages
Edition : 3rd
Language : English
ISBN-13 : 9781801815529
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 29, 2021
Length: 554 pages
Edition : 3rd
Language : English
ISBN-13 : 9781801815529
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 109.97
Learn Python Programming, 3rd edition
€35.99
Mastering Python 2E
€37.99
Python Object-Oriented Programming
€35.99
Total 109.97 Stars icon

Table of Contents

17 Chapters
A Gentle Introduction to Python Chevron down icon Chevron up icon
Built-In Data Types Chevron down icon Chevron up icon
Conditionals and Iteration Chevron down icon Chevron up icon
Functions, the Building Blocks of Code Chevron down icon Chevron up icon
Comprehensions and Generators Chevron down icon Chevron up icon
OOP, Decorators, and Iterators Chevron down icon Chevron up icon
Exceptions and Context Managers Chevron down icon Chevron up icon
Files and Data Persistence Chevron down icon Chevron up icon
Cryptography and Tokens Chevron down icon Chevron up icon
Testing Chevron down icon Chevron up icon
Debugging and Profiling Chevron down icon Chevron up icon
GUIs and Scripting Chevron down icon Chevron up icon
Data Science in Brief Chevron down icon Chevron up icon
Introduction to API Development Chevron down icon Chevron up icon
Packaging Python Applications Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(34 Ratings)
5 star 61.8%
4 star 23.5%
3 star 5.9%
2 star 0%
1 star 8.8%
Filter icon Filter
Top Reviews

Filter reviews by




N/A Jan 30, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Feefo Verified review Feefo
H Riz Jan 15, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book leverages the art of learning programming for all from beginners to advanced level. Mostly, I like the flow of the contents given in this book. It takes you gradually to the next step and you grab lots of hands on experience by completing the tasks given in the book. It has extensive information on almost all data structures used in Python including but not limited to; data types, variables, lists, sets, dictionaries, functions, loops, conditional structures, decorators, OOP, working with files, API development, data science related packages etc. I would say this book is a must have artifact in your portfolio if you want to grow your career in programming, data science, cloud services and so on. If you have some experience working in Python 2, you can easily adopt Python 3 by using useful techniques given in this book. A perfect book which can take you from beginners to expert level because it encourages you to gain experience while working on the problem sets, labs, activities from real world.
Amazon Verified review Amazon
Vedant Khandelwal Nov 23, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book emphasizes more on the fundamentals and advancements of programming and then Python. The book seems to be a vast bank of ample information for each concept. The authors have even provided external sources if one wants to read more on a particular topic. It in a way motivates readers to dig deeper on the web and google as much sources as possible if required. The book is designed in such a way that will keep it evergreen for a really long time. In a way, you can say that it would be unaffected by at least a few Python updates.The code is available for readers to download, and the authors encourage you to play with it, expand it, change it, break it, and see things for yourself.
Amazon Verified review Amazon
Sanket Agrawal Nov 21, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book gives an in-depth knowledge of python and is a must for both beginners and advance programmers as it starts with basics of python and then slowly explores the advanced topics in python. This book provides examples, and codes, which makes it easier for beginners to learn the language. The language used in the book is really simple hence it is easy to grasp everything. I love this book and would recommend it to all the people who want to learn python. I felt this book is enough to learn python.
Amazon Verified review Amazon
Muhammad Anwar Shahid Jan 12, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book leverages the art of learning programming for all from beginners to advanced levels. Mostly, I like the flow of the contents given in this book. It takes you gradually to the next step and you grab lots of hands-on experience by completing the tasks given in the book. It has extensive information on almost all data structures used in Python including but not limited to; data types, variables, lists, sets, dictionaries, functions, loops, conditional structures, decorators, OOP, working with files, API development, data science related packages, etc. I would say this book is a must have an artifact in your portfolio if you want to grow your career in programming, data science, cloud services and so on. If you have some experience working in Python 2, you can easily adapt Python 3 by using the useful techniques given in this book. A perfect book that can take you from beginners to expert level because it encourages you to gain experience while working on the problem sets, labs, activities from the real world.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.