Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learn Python Programming
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learn Python Programming

Learn Python Programming - Fourth Edition

By : Romano, Kruger
5 (1)
close
close
Learn Python Programming

Learn Python Programming

5 (1)
By: Romano, Kruger

Overview of this book

Learn Python Programming, Fourth Edition, provides a comprehensive, up-to-date introduction to Python programming, covering fundamental concepts and practical applications. This edition has been meticulously updated to include the latest features from Python versions 3.9 to 3.12, new chapters on type hinting and CLI applications, and updated examples reflecting modern Python web development practices. This Python book empowers you to take ownership of writing your software and become independent in fetching the resources you need. By the end of this book, 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. This Python book offers a clear and practical guide to mastering Python and applying it effectively in various domains, such as data science, web development, and automation.
Table of Contents (20 chapters)
close
close
18
Other Books You May Enjoy
19
Index

Mutability

The first fundamental distinction that Python makes about data is whether the value of an object can change. If the value can change, the object is called mutable, otherwise the object is called immutable.

It is important that you understand the distinction between mutable and immutable because it affects the code you write. Let us look at the following 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 is 43 (we hear you say...). Yes, it is 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 int object is created, with the 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 42 to 43—we just pointed the name age to a different location, which is the new int object whose value is 43. Let us see the IDs of the objects:

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

Notice that we call the built-in id() function to print the IDs. 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 us see the same example using a mutable object. For this example, we will use the built-in set type:

>>> numbers = set()
>>> id(numbers)
4368427136
>>> numbers
set()
>>> numbers.add(3)
>>> numbers.add(7)
>>> id(numbers)
4368427136
>>> numbers
{3, 7}

In this case, we set up an object, numbers, which represents a mathematical set. We can see its id being printed and the fact that it is empty (set()), right after creation. We then proceed to add two numbers to it: 3 and 7. We print the id again (which shows it is the same object) and its value, which now shows it contains the two numbers. So the object’s value has changed, but its id is still the same. This shows the typical behavior of a mutable object. We will explore sets in more detail later in this chapter.

Mutability is a very important concept. We will remind you about it throughout the rest of the chapter.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learn Python Programming
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon