Book Image

Python Fundamentals

By : Ryan Marvin, Mark Nganga, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Nganga, Amos Omondi

Overview of this book

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.
Table of Contents (12 chapters)
Python Fundamentals
Preface

Imports and Import Statements


There are several ways we can import and use the resources defined in a module. Taking the calculator module we defined previously, you have already seen one way to go about it, which is importing the whole module by using the import keyword and then calling a resource inside it by using the dot (.) notation, like so:

>>> import calculator
>>> calculator.add(8, 9)
17

Another way of accessing a module's resources is to use the from…import… syntax:

>>> from calculator import *
>>> add(8, 9)
17

Note that the * in the preceding example tells Python to import everything in the module named calculator. We can then use any resource defined in the calculator module by referring to the resource name (in our case, the add function) directly. You should note that using * is not a good practice. You should always strive to import exactly what you need.

What if you want to only import a few resources from the module? In that case, you can name...