Learn Python Programming
eBook - ePub

Learn Python Programming

An in-depth introduction to the fundamentals of Python, 3rd Edition

Fabrizio Romano, Heinrich Kruger

Share book
  1. 552 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn Python Programming

An in-depth introduction to the fundamentals of Python, 3rd Edition

Fabrizio Romano, Heinrich Kruger

Book details
Book preview
Table of contents
Citations

About This Book

Get up and running with Python 3.9 through concise tutorials and practical projects in this fully updated third edition.

Purchase of the print or Kindle book includes a free eBook in PDF format.

Key Features

  • 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

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

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

Who this book is 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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learn Python Programming an online PDF/ePUB?
Yes, you can access Learn Python Programming by Fabrizio Romano, Heinrich Kruger in PDF and/or ePUB format, as well as other popular books in Informatica & Linguaggi di programmazione. We have over one million books available in our catalogue for you to explore.

Information

Year
2023
ISBN
9781801815529

2

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

Table of contents