1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to the Python Language

Introduction to the Python Interpreter

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
37m
Students
92
Ratings
4.3/5
starstarstarstarstar-half
Description

Python is more than just a programming language. It includes additional components which all work together as a holistic system. This course provides an introduction to some of these different components. Including the programming language itself. With the goal of introducing you to the Python programming language as a means of creating and controlling objects.

Learning Objectives

Upon completing this course you’ll be familiar with the basic concepts of language syntax, the python interpreter, and code blocks.

Intended Audience

This course was designed for first-time developers wanting to learn Python. Existing developers may want to skip.

Prerequisites

This is an introductory course and doesn’t require any prior programming knowledge. However, conceptual knowledge of Python objects is recommended.

Transcript

In this lesson, we’re going to use Python in interactive mode which will enable us to run Python code one line at a time and immediately see the results. 

Previous lessons talked about the Python runtime. Let’s recap the term: Python runtime. Recall that Python is just an application that knows how to read Python code and interpret those instructions into actions. That holistic process is the Python runtime. So, when we talk about the Python runtime, we’re talking about the different components which exist inside the Python application.

Typically when running the Python application you’ll specify the Python code file that you want to run. However, by not providing a code file, Python opens up into an interactive console. 

The interactive console provides a command line environment which enables us to send code to the interpreter - one line of code at a time.

Using Python’s interactive console is not the most efficient way to develop code because the code that we enter isn’t saved anywhere. The code runs when entered, remains in the console’s history, and once the console is closed, everything is gone. 

So, while it’s not great as a means of developing Python code, it does have its use cases; one use case is to help well when first learning Python. 

For this lesson, don’t worry about focusing on how everything functions. We haven’t covered any of the syntax yet. The goal of this lesson is to demonstrate how the Python language is used to create and control objects. 

Over the coming lessons you’ll be learning about the Python syntax. I want this to serve as a real world example of using Python code. Then we can build on this over the coming lessons.

The interactive console reads Python code, interprets the code’s instructions and returns the results. That implies that everything that I’m about to type into the console is valid Python code. 

So, let’s see what this interactive console can do. 

First, we can perform some basic operations on objects. Operations such as addition, subtraction, multiplication, and division. 

Let’s start by adding two integers. Notice that typing 1 + 2 returns the results of 3. 

Okay, let’s pause. I’ve spent the entire course so far talking about how everything in Python is an object; then I show how to add two numbers. Where are the objects that I’ve been talking about non-stop?

In previous content, we talked about some of Python’s built-in data types. We talked about the integer type, which Python refers to as an int. We talked about floats, which are numbers with decimal places. We also talked about strings which are used for text. 

These types and a few others are so common that Python incorporated them directly into its language syntax. When the interpreter reads this code, it recognizes that these are integers and it creates objects for each. Then it adds the two objects together. 

How does the interpreter know that these should be integers? Python considers whole numbers to be integers, unless we specify otherwise. 

Don’t worry if that doesn't make sense just yet.

Let’s try something slightly more complex? Let’s do some math with integers and floats. Notice we have a mix of integers and floats; because of the decimal places Python knows that these are floats. Running this returns the results with a decimal place which hints that the results are a float.

Notice that the code we enter returns the results and displays them just below the entered code. Remember, everything in Python is an object. That includes these results. In these examples we have the resulting object returned and displayed. And then that object can be removed from memory. 

Picture it like this: The interpreter reads this line of code. It detects two numbers without decimal places, so it assumes that they’re integers. It takes these 2 numbers and stores them inside an integer object. It determines which operation to perform, and then it calls the code responsible for handling that operation. 

That code may or may not return an object. It depends on which operator is used. In this case the add operator does return an integer object containing the number 3. 

Oftentimes we want to be able to interact with different objects later-on in our code. Imagine that we want to take the results of this addition and use it to perform some further math. In that case we need to be able to reference the returned object. Python provides a mechanism for referencing objects so that we can use the object throughout our code. 

This mechanism is called name binding. Name binding is the process of linking an object to a name. The way that we link a name to an object is by using the assignment operator. Which is a single equal sign.

Here’s an example: imagine that we want to store the number for pi so that we can use it later. If we type the number 3.1415 into the Python interpreter, it will create a float object with this value stored inside. Now, since we want to use it later, we need a reference to this object. We need to bind this object to a name. In this case we’ve called it pi. 

Notice when I run this, it doesn’t display the object. What this did was create a name binding. This pi binding is now a stand-in for our float object. Which means we can treat this name as if it’s a float object. 

Checking the type of the name binding shows that the object is indeed a float. If we display the results of this binding, we can see the value of 3.1415. 

So, Python can bind a name to any object and that name can be used as a stand-in. Some languages refer to this concept of name binding as variables. For example, I might say that this pi variable is equal to 3.1415. I’ll likely use these two terms interchangeably from here on out. So, make a mental note that the term variable is shorthand for name binding.

So, integers and floats are such commonly used types that they’re integrated into Python’s language syntax. This allows us to create integers and floats in code by simply typing a number.

Let's explore how Python has built strings into the language syntax. 

A string is just a sequence of characters. Typing “Hello, World!” reflects the same text back to us. When the interpreter reads this it recognizes based on the quotes that this is a string. So it creates a string object in memory with this text. Python allows strings to be defined by either a pair of single or double quotes. 

Let’s bind this string object to a name so that we can interact with it. We’ll name the binding: greeting. 

Recall that objects are made of attributes and methods. Attributes define meaningful aspects of a type. Methods implement meaningful behaviors of a type. 

Now recall that bound names serve as a stand-in for an object. Since this name binding references a string object, we can call any of the methods which exist for strings. 

Looking at this list of methods which exist for string objects shows that there’s a lot we can do with text. Let’s call some of these methods. 

We can make this text lowercase by calling the lower method. Notice it returns the same text, although lowercase.

It’s similar when using the upper method to return uppercase text. 

Some methods expect us to provide input for specific parameters. For example the replace method finds specific text and replaces it. So we could find the text “world” and replace it with “universe” resulting in “Hello, Universe!”

At the start I said that we can perform operations on objects. We saw that in action when we added two numbers. The numbers are each an object, the plus sign is an operator. It feels intuitive to add numbers. But, that’s only kind of what we actually did. What we really did was add two objects that happened to be integers. 

The interpreter is the glue between our code and objects. It has methods built into objects that know what to do for each operation. So when the interpreter reads 1 + 2 it uses some built-in methods to add these two objects. What this means for us is that we can use operators on types other than numbers. 

For example, we can add two strings together to form one string. We can also repeat a string by using the multiple operator. This is probably less intuitive than using operators with numbers. However, this is why it’s so valuable to understand Python objects. Because they can be closely integrated with the features built into the language such as operators.

There’s another key data type that’s built into the language, it’s called a boolean; similar to integers being named int, and strings being named str, booleans are named bool. 

A boolean is something that represents one of two options, true or false. Booleans are useful in programming when you want to make decisions. Just as there are operators for adding, multiplying, etc, there are operators which help to determine if something is true or false.

For example we might want to compare two objects. In this case the interpreter can determine if one number is larger than another. If it is, it will return True. Otherwise it’ll return False. 

Determining if something is true or false is the basis for decision making in code. Decisions such as:

  • Is this date in the future?
  • Is this score high enough?
  • Is this user authenticated?

Being able to determine if something is true or false plays a large part in programming. And so do integers, floats, strings, and a few others that have yet to be introduced.

In the coming lessons, we’ll go through the syntax more in depth. For now here are your key takeaways:

  • Python provides a lot of commonly required types built into the runtime.
  • Common types such strings, integers, floats, and booleans are built directly into Python’s language syntax. 
  • Binding a name to an object enables us to establish a reference to an object.
    • Bound names are stand-ins for the object they reference.
  • The interpreter is the glue between code and objects.
    • Which enables objects to determine how they interact with operators.

That’s going to wrap up this lesson, thanks for watching, and I’ll see you in the next lesson.

About the Author
Students
94627
Labs
26
Courses
44
Learning Paths
56

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics