01: Getting to Know Python

*This tutorial series will follow the Engineering 1D04 course at McMaster University. If you’re seeking help from another school and find that these tutorials can help you, please let me know!

You’ve probably come here for help with Python, a high-level object oriented programming language. Python can be extremely useful, and is a great beginner’s language, because much of it is written in plain English. Here are a few things you need to know before you get started.


IDLE

Idle is Python’s GUI (Graphical User Interface). It is a tool for writing, compiling, and saving Python code. If you don’t have IDLE, you’ll have to install it.

Once you’ve got IDLE running, there are a few things you need to know.

The Shell

The Shell is the first window you will see when opening IDLE. This is where code will run, and where Python will display errors and output from the “print” statement. You can write code here as well, but we’re going to do that in the editor. This is because the Shell does not allow us to easily edit or change code. For that, we will use the Editor. Get to the editor by navigating to File > New Window when inside IDLE.

The Editor

The Editor is where you will do most of your coding. Once you’ve opened a new window, it is best to start by naming and saving your file. To do this, navigate to file > save, or use the key command ctrl + s. Now, pick a name for your file, but make sure that the name ends in “.py”. This tells Python that it is in fact a Python code file, and not something else. Without this identifier, code highlighting will not work, and you may not be able to compile properly.


Data Types

There are many ways to store data in a computer, known as data types. These methods are here because you will often have to store a range of very different values. A few examples are:

  • Integers:  Whole numbers with no fractional value (ex. 1, -1, and 732819).
  • Floats:  Numbers that carry a fractional value, which can be zero. (ex. 3.3, Pi, and 1.0).
  • Booleans:  A binary data type whose options are either True or False.
  • Character (Char):  A letter, number, or symbol with no numerical or operational value.
  • Strings:  A collection of Chars “strung” together to create a word, sentence, or other representation. Always surrounded by quotations ( ” ” ).
  • Enumeration:  A set of named values.
  • Array:  A list of elements. Any of the above data types (and more) can be used as an element in an array.

Don’t worry too much about these for now, as Python makes it pretty easy to handle multiple data types. Let’s take a look at how these are defined inside IDLE, the Python GUI (Graphic User Interface):

myInt = 5                   # I'm an Integer! 

myFloat = 4.9               # I'm a Float! 

myBool = True               # I'm a Boolean! 

myChar = 'a'                # I'm a Char! 

myString = "Hello World!"   # I'm a String!

Let’s take a look at how using the wrong data types can cause our code to fail.


Arithmetic Operations

Python can do all the calculations that a calculator can. Simply enter the equation in IDLE using Python syntax, and print the result. Here are a few common operations, and their syntax in Python:

Operation Python Syntax Example
Multiplication * 1 * 2
Division / 2 / 3
Addition + 5 + 2
Subtraction - 2 - 1
Exponentation ** 2 ** 16

Take careful note of the exponentiation syntax!

Let’s say we’re trying to get the value of one half (0.5). This number can easily be computed by dividing the numbers 1 and 2.

int1 = 3            # Store the value 1 to int1 
int2 = 2            # Store the value 2 to int2 
ans  = int1/int2    # Divide 1/2 
print ans           # Print the result to the Shell

Let’s take a look at the output:

0

Uh oh! Zero isn’t the number we’re looking for. What happened?

Well, it turns out that Python tried to divide two integers, and therefore had to give us a result in integer form. We can look at the number we were expecting (1.5) in two parts.

We have the integer value (1)
  and the fraction value (.5).

The compiler tries to convert this number to an integer, because that is what we started with. To do so, it drops the fraction value, leaving us with only 1.

So what do we do?

Let’s try dividing two Floating point (or float) numbers. We can do this easily by adding a decimal element to our values (even if it is 0):

int1 = 3.0        # Store the value 1 to int1 
int2 = 2.0        # Store the value 2 to int2 
ans  = int1/int2  # Divide 3/2 
print ans         # Print the result

Now let’s have another look at our output:

 1.5

Much better! This is the result of dividing two floats. Python is now expecting a float as the result, and gives us the answer we’re looking for.

Errors like this are common in programming. Most operations must be done with identical data types to avoid error (there are some exceptions).
Note: It is good practice to always store numbers as floats when any arithmetic operations are being used.
 
More complicated mathematical formulas can be computed using a combination of the operators listed above. Note that Python does rely on BEDMAS for its order of operations, but it is good practice to group numbers and operations together using parentheses.

4+2*3-8+25 # = 27. Unclear, prone to errors 

(4 + (2 * 3)) - (8 + 25)# =-23. Clear, avoids errors.