Lesson 02 - Introduction to Python

The following topics are discussed in this notebook:

  • Values, Types, and Lists
  • Expressions and Statements
  • Variables
  • Function
  • Objects
  • Errors
  • Comments

This notebook is intended to provide you with a quick introduction to some of the concepts and terminology that you will encounter in this course. We will not explore any of these concepts in depth at this moment, but will discuss each of them more thoroughly in future lessons.

Values, Types, and Lists

A value is anything that represents a piece of information. That information could be numerical, a piece of text, or something more complicated. The following are all values in Python:

7, 8.214, 'cat', 'dog', 'Hello world!'

We can ask Python to display a value to the screen using the print() function. In the cell below, we print three of the values mentioned above.

In [1]:
print(7)
print(8.214)
print('Hello World!')
7
8.214
Hello World!

Every value has a type that describes what kind of information is represented by the value. There are many different types that a value can have, but the three most basic types in Python are integers, floating-point numbers (or floats), and strings.

  • An integer is value that represents a whole number.
  • A float is a value that represents a decimal number.
  • A string is a value that represents a piece of text.

We can use the type() function to check the type of a value.

In [2]:
print("The type of the value 7 is ", type(7))
print("The type of the value 8.214 is ", type(8.214))
print("The type of the value 'Hello World!' is ", type('Hello World!'))
The type of the value 7 is  <class 'int'>
The type of the value 8.214 is  <class 'float'>
The type of the value 'Hello World!' is  <class 'str'>

Notice that Python abbreviates the types as int, float, and str.

A list is a collection of values that have been grouped together and arranged in a specific order. A list is defined, and displayed, using brackets: []. In the cell below, we create and display a list containing the integer values 1, 4, 6, and 3 (in that order).

In [3]:
print([1, 4, 6, 3])
[1, 4, 6, 3]

Expressions and Statements

An expression is a sequence of characters that can be evaluated or reduced down to a single value. Here are three examples of Python expressions:

2*(5 + 8), sum([1, 4, 6, 3]), 'Mary' + 'ville'

You might be able to guess what values each of these expressions will evaluate as. To check your intuition, we will print the value of each of these expressions below.

In [4]:
print(2*(5 + 8))
print(sum([1, 4, 6, 3]))
print('Mary' + 'ville')
26
14
Maryville

If it wasn't immediately clear to you how these expressions would be evaluated, don't worry. You'll learn everything you need to understand these expressions early in this course.

A statement is a unit of code containing an instruction. Statements often contain expressions. An example of a statement is shown below.

print(2*(5 + 8))

This statement instructs Python to print the value of the expression 2*(5 + 8), which evaluates to the value 26.

Variables

A variable can be thought of as a container or storage location for a piece of information. Every variable has two features: a value and a name.

  • The value of a variable is the information that is stored within the variable. This could be a single number, a list of numbers, a string of characters, or some more complex type of information.
  • The name of a variable is sequence of characters that is used to identify the variable. We use the name of a variable in statements to access its contents.

We define variables using the assignment operator =. A statement used to define a variable is referred to as an assignment statement.

In the cell below, we use two assignment statements to define variables named x and y. We then include a statement to print the value of the expression x + y. This will display the sum of the numerical values that we stored in the variables x and y.

In [5]:
x = 42
y = 37

print(x + y)
79

Functions

A function is a named collection of statements that (often, but not always) accepts some input, performs some set of instructions, and (often, but not always) returns some output.

The inputs provided to a function (if it has any) are called arguments. The output that is provided by the function (if it has one) is called the return value of the function. When we use a function within a statement, we say that we are calling the function.

We have seen two examples of functions already in this lesson: the print() function and the sum() function. We will now use both of these in an example.

In [6]:
total = sum([1, 4, 6, 3])
print(total)
14

The cell above consists of two statements. In the first statement, we provided the list [1, 4, 6, 3] as an argument to the sum() function. The sum function ran several pre-written statements (that we do not see) to calculate the sum of the values stored in the list. It then returned (or output) the calculated sum as a value to be used in an assignment statement, storing it in a variable named total.

In the second statement, the variable total was provided as an argument to the print() function, which caused the value 14 to be displayed the to screen.

In the cell below, we use a function named len() to determine the length, or number of characters, in a string value.

In [7]:
count = len('Hello World!')
print(count)
12

Python has many built-in functions such as sum(), len(), and print(). We also have the ability to create our own user-defined functions. We will discuss this in depth in a later lesson.

Objects

You will occasionally see the term object used to refer to a value, variable, or function. We will discuss exactly what this term means later in the course, but for now you just just consider this as an umbrella term used to refer to values, variables, and functions.

Errors

There will be times when you write a line of code that Python does not understand, or that represents an instruction that Python is unable to complete. In this situations, Python will generate an error. When an error is encountered, Python will display an error message that intended to help you identify the cause of the error.

We will now provide a couple examples of statements that produce errors.

In [8]:
a = 5
b = 0
print(a / b)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-8-dfcedec16dfc> in <module>
      1 a = 5
      2 b = 0
----> 3 print(a / b)

ZeroDivisionError: division by zero

In the cell above, we asked Python to display the value of 5 divided by 0. Division by zero is, of course, a calculate that Python is unable to perform, and the statement results in an error. The last line of an error message is often the most important. This line will let you know which of several possible error types was encountered, and will also give a description of the error. In this case, the error type was ZeroDivisionError.

Notice on the third line of the error message, there is an arrow that points to the line where the error was encountered. This can be useful for helping you to debug your code.

Let consider another example.

In [9]:
Python is cool!
  File "<ipython-input-9-91fc9ad54969>", line 1
    Python is cool!
                  ^
SyntaxError: invalid syntax

The type of the error we encountered is SyntaxError. A syntax error occurs when Python encounters a statement that it does not understand, or is not able to parse. The line in the cell above does not represent a valid Python statement, and Python does not know what to do with it.

Comments

A comment is a piece of text contained within a program that does not represent executable code, and is intended to be ignored by the interpreter. Comments in Python are created using the pound sign, #. Anything that appears on a line following this symbol will be treated as a comment, and will be ignored by the Python interpreter.

Comments are primarily used to document code. They can provide descriptions of the intended purpose of a piece of code, making it easier for someone reading the code to understand.

In the cell below, we have recreated a previous example, but have added comments to document the code.

In [10]:
# In this example, we will define two variables
# named x and y, and will display their sum. 

x = 42  # Store the value 42 in the variable x
y = 37  # Store the value 37 in the variable y

print(x + y)  # Print the sum of x and y. 
79

Comments are also often used to temporarily "turn off" a line of code that the programming does not want to execute, but does not wish to delete. This can be useful in debugging a program that produces an error. The code below contains three print statements, one of which produces an error. We have used a comment to disable the statement that results in an error.

In [11]:
print(13 / 2)
#print(5 / 0)
print(27 / 3)
6.5
9.0