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

Defining Modules


Following the definition of a module that was given earlier, you can now see that you have in fact been working with modules all along. Any valid .py file that you have created in this book is more or less a valid module.

In this section, though, we are going to be a bit more deliberate in how we create modules so that you can see how to define and import resources.

To recap, a valid Python module is any .py file containing valid Python code. This code could be variable definitions, functions, classes, methods, and so on. We are going to practice with a simple module that contains just one function.

Let's go ahead and create our first module.

Exercise 46: Creating Modules

In this exercise, we will create a module named calculator:

Module names should follow normal Python variable naming conventions. These include the following:

  • They should follow snake_case (lower_case_with_underscore). Some good module names would be module and another_module. The following are examples of bad...