Python Fundamentals
eBook - ePub

Python Fundamentals

A practical guide for learning Python, complete with real-world projects for you to explore

Ryan Marvin, Mark Ng'ang'a, Amos Omondi

Partager le livre
  1. 324 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Python Fundamentals

A practical guide for learning Python, complete with real-world projects for you to explore

Ryan Marvin, Mark Ng'ang'a, Amos Omondi

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

With an interesting mix of theory and practicals, explore Python and its features, and progress from beginner to being skilled in this popular scripting language

Key Features

  • A comprehensive introduction to the world of Python programming
  • Paves an easy-to-follow path for you to navigate through concepts
  • Filled with over 90 practical exercises and activities to reinforce your learning

Book Description

After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions.

As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation.

By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.

What you will learn

  • Use control statements
  • Manipulate primitive and non-primitive data structures
  • Use loops to iterate over objects or data for accurate results
  • Write encapsulated and succinct Python functions
  • Build Python classes using object-oriented programming
  • Manipulate files on the file system (open, read, write, and delete)

Who this book is for

Python Fundamentals is great for anyone who wants to start using Python to build anything from simple command-line programs to web applications. Prior knowledge of Python isn't required.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Python Fundamentals est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Python Fundamentals par Ryan Marvin, Mark Ng'ang'a, Amos Omondi en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781789809947
Édition
1
Sous-sujet
Programming

Object-Oriented Programming

Learning Objectives

By the end of this chapter, you will be able to:
  • Explain different OOP concepts and the importance of OOP
  • Instantiate a class
  • Describe how to define instance methods and pass arguments to them
  • Declare class attributes and class methods
  • Describe how to override methods
  • Implement multiple inheritance
This lesson introduces object-oriented programming as implemented in Python. We also cover classes and methods, as well as overriding methods and inheritance.

Introduction

A programming paradigm is a style of reasoning about programming problems. Problems, in general, can often be solved in multiple ways; for example, to calculate the sum of 2 and 3, you can use a calculator, you can use your fingers, you can use a tally mark, and so on. Similarly, in programming, you can solve problems in different ways.
At the beginning of this book, we mentioned that Python is multi-paradigm, as it supports solving problems in a functional, imperative, procedural, and object-oriented way. In this chapter, we will be diving into object-oriented programming in Python.

A First Look at OOP

Object-oriented Programming (OOP) is a programming paradigm based on the concept of objects. Objects can be thought of as capsules of properties and procedures/methods. In an interview with Rolling Stone magazine, Steve Jobs, co-founder of Apple, once explained OOP in the following way:
"Objects are like people. They're living, breathing things that have knowledge inside them about how to do things and have memory inside them so that they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction
"
Steve Jobs; Rolling Stone; June 16, 1994
An example of an object you can consider is a car. A car has multiple different attributes. It has a number of doors, a color, and a transmission type (for example, manual or automatic). A car, regardless of the type, also has specific behaviors: it can start, accelerate, decelerate, and change gears. Regardless of how these behaviors are implemented, the only thing we, the users of the car, care about, is that the aforementioned behaviors, such as acceleration, actually work.
In OOP, reasoning about data as objects allows us to abstract the actual code and think more about the attributes of the data and the operations around the data. OOP offers the following advantages:
  • It makes code reusable.
  • It makes it easier to design software as you can model it in terms of real-world objects.
  • It makes it easier to test, debug, and maintain.
  • The data is secure due to abstraction and data hiding.
With the benefits it confers, OOP is a powerful tool in a programmer's tool box.
In the next section, we'll be looking at how OOP is used in Python.

OOP in Python

Classes are a fundamental building block of object-oriented programming. They can be likened to blueprints for an object, as they define what properties and methods/behaviors an object should have.
For example, when building a house, you'd follow a blueprint that tells you things such as how many rooms the house has, where the rooms are positioned relative to one another, or how the plumbing and electrical circuitry is laid out. In OOP, this building blueprint would be the class, while the house would be the instance/object.
In the earlier lessons, we mentioned that everything in Python is an object. Every data type and data structure you've encountered thus far, from lists and strings to integers, functions, and others, are objects. This is why when we run the type function on any object, it will have the following output:
>>> type([1, 2, 3])
<class 'list'>
>>> type("foobar")
<class 'str'>
>>> type({"a": 1, "b": 2})
<class 'dict'>
>>> def func(): return True
...
>>> type(func)
<class 'function'>
>>>
You'll note that calling the type function on each object prints out that it is an instance of a specific class. Lists are instances of the list class, strings are instances of the str class, dictionaries are instances of the dict class, and so on and so forth.
Each class is a blueprint that defines what behaviors and attributes objects will contain and how they'll behave; for example, all of the lists that you create will have the lists.append() method, which allows you to add elements to the list.
Here, we are creating an instance of the list class and printing out the append and remove methods. It tells us that they are methods of the list object we've instantiated:
>>> l = [1, 2, 3, 4, 5] # create a list object
>>> print(l.append)
<built-in method append of list object at 0x10dd36a08>
>>> print(l.remove)
<built-in method remove of list object at 0x10dd36a08>
>>>

Note

In this chapter, we will use the terms instance and object synonymously.

Defining a Class in Python

In our example, we'll be creating the blueprint for a person. Compared to most languages, the syntax for defining a simple class is very minimal in Python.

Exercise 31: Creating a Class

In this exercise, we will create our first class, called Person. The steps are as follows:
  1. Declare the class using the Python keyword class, followed by the class name Person. In the block, we have the Python keyword pass, which is used as a placeholder for...

Table des matiĂšres