Python Variables and Operators

Python Variables

Using variables is one of the most powerful features in Python. Use assignment statements to create variables and give them values.

From the chevron prompt in the Python interpreter:

>>> x = 12
>>> message = “You are doing a good job!”


In the code samples above, two variables were created. Now you could tell Python:

>>> print x
>>> print message
“You are doing a good job!”


You can name variables with almost whatever you want, but it is good practice to give them a name that is meaningful.

The two main rules for naming variables are:

Python Operators

You need to know how to use operators ‘+, -, *, /, and **’. The operator ‘**’ is for exponentiation.

The order of operations follows the same PEMDAS rules. For example:

>>> x = 2 + 3 * 5
>>> print x


Beware of the Division Operator

The division operator might not do what you expect.

>>> y = 1 / 2
>>> print y

Why zero? When both operands are integers, the result is an integer. In other words, the decimal part gets chopped off. To solve this you have to use float types.

>>> y = float(1) / float(2)


One other operator, that is surprising useful, is ‘%’, the modulus operator. It yields the remainder.

>>> y = 5 % 2
>>> print y


Finally, use the ‘+’ operator for concatenation. That is, joining strings together.

>>> print ‘Hello,’ + ‘ World!’
Hello, World!


Python Values – Know the Type

Python values are basic things the language works with. Python values belong to different types. Use the Python interpreter to ask for the type of a given Python value.

Below are examples of input and output from asking Python for types of various Python values. The chevron prompt “>>>” is input for Python, and the line below is output from Python.

>>> type('Cookies')
<type 'str'>

>>> type(3)
<type 'int'>

>>> type(3.0)
<type 'float'>

<type 'str'>


The first type means string. After that it’s, integer, floating number, and string. Notice that if you put quotations around a number, it becomes a string.

While it might be habit, refrain from using commas with large numbers. It will cause a semantic error, because Python would interpret it as a comma separated sequence of numbers.

Write a Simple Python Program

Download Python for free, and write a simple Python program. The examples in this blog will use Python 2. It is okay to learn either 2 or 3. They are similar, but definitely have differences.

Your First, Simple Python Program

Open a text editor and write the classic “Hello World!” program. Simply type as follows:

print 'Hello World!'


Save the file with a .py extension. Name the file

Execute the script then you will see “Hello World!” as the output.

Yes, it is a trivial program with almost zero practical use, but by definition, it is a program.

A good resource for understainding Python is Dr. Charles Severance’s free Python for Informatics book.

If you are interested in learning Python, and need to start at the basics, take a look at the Python for Everybody Specialization offered through Coursera.

Python Reserved Words to Know

This post is as a reference for Python reserved words.

Python reserved words have special meaning, and must only be used in regard to their special meaning. They include:

and not as
global assert del
while elif or
else from with
if pass break
import class raise
continue return def
lambda yield except
print exec finally
for try in

You must not use these words as variables, because they already have a special command meaning for Python.

Python understands these commands like a trained dog understands “sit”. So again, you must not use Python reserved words as variables.

Program with Python Reserved Words

You can use one of these Python reserved words to write your first Python program.

print 'Hello World!'


This, of course, is the classic ‘Hello World!’ program, written in Python.

Computer Hardware Architecture Basics

Before learning how to program computers, take a moment to understand the computer hardware architecture basics.

The Central Processing Unit

The CPU can execute commands extremely fast. The catch is, it has to be told what to do, or it won’t do anything.

The Main Memory

The main memory stores information that the CPU can retrieve extremely fast. Anything stored here will vanish when the computer is turned off.

The Secondary Memory

This stores memory but it is very slow. We need it because it can store memory even without power. Examples of this are a hard drive, USB stick, or disk drives.

The Network

The network can serve as remote secondary memory, because information is stored over the network that you can retrieve.

The Input and Output Devices

This could be a mouse, keyboard, monitor, speaker, joystick, etc. You use these to interact with your computer.