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

Working with the Python Interactive Shell


We are going to be writing our first program through the Python interactive shell. Before we begin, ensure that you have Python installed on your machine.

Exercise 1: Checking our Python Installation

To check whether Python is installed properly, perform the following steps:

  1. Open the command prompt.

  2. Type in the command python and press Enter. This should open the Python interpreter, and you should see a message containing the Python version you installed.

    Observe the output. It should be similar to what's shown in the following screenshot:

    Figure 1.1: Checking the Python installation

Once the interactive shell opens, on the first line, you should see the Python version information. This includes its major version and the release date. As you can see from the preceding screenshot, it shows us that we're using Python 3, minor version 6, which was released on December 19, 2017. We can also see information on the system the interactive shell is running on. On the second line, we can see a few examples of commands we can write, and finally the prompt to enter a command >>> on the third line.

The Python interactive shell can be thought of as just any other shell that interfaces with the operating system (for example, Bash or CMD), but in this case, it interfaces with the Python interpreter. Through it, we can execute Python instructions. It presents a command-line interface.

Exercise 2: Working with the Python Interpreter

In this exercise, we will learn how to work with the Python interpreter:

  1. At the command prompt, enter the following command to run the popular Hello World! program:

    >>> print("Hello World!")
    Hello World
    >>>

    Calling print logs the passed message to the standard output.

  2. Anything you type into the shell is echoed back. Type "Echo" in the shell to check this:

    >>> "Echo" 
    'Echo'
    >>> 1234 
    1234 
    >>> 
  3. You can even do some calculations (note that the order of operations is observed). Carry out a few mathematical operations in the terminal, like this:

    >>> 9 + 3 
    12 
    >>> 100 * 5 
    20 
    >>> -6 + 5 * 3 
    9 
    >>> 
  4. Exit the shell by running the exit command:

    >>> exit

When you exit the Python interactive shell and relaunch it, you will notice that any variables you had defined or commands you had run in the previous session are gone. Therefore, we can't reuse them. Another way to run a Python program is by running code that has been saved in a file. This allows us to run many instructions at a time and also reuse them, as we'll see later on in the book. In the next section, we'll take a look at how to do this.

Activity 1: Working with the Python Shell

In this activity, we will perform some basic operations using the Python interactive shell.

The steps are as follows:

  1. Open the terminal.

  2. Open the interactive shell.

  3. Print Happy birthday or any message to the standard output.

  4. Run the operation 17 + 35 * 2. The result should be 87.

  5. Print the numbers 1 to 9 on a single row.

  6. Exit the shell.

Note

Solution for this activity can be found at page 276.