Lesson 1.2: IDLE and Python Shell

In the previous lesson, you have learned how to install the Python distribution from python.org. This installed several different apps on your computer including IDLE, the Python Integrated Development and Learning Environment. In this section, you learn how to start IDLE and how to use one of its many tools, the Python Shell.

Starting IDLE

  1. On Windows, press the Windows key and start typing “idle”. Click the “IDLE (Python 3.7 32-bit)” app.
  2. On Mac, open Finder and click Applications. Double-click Python 3.x and then double-click the IDLE icon.
  3. On Linux, open a terminal window and enter idle3. You may also be able to click Applications at the top of the screen. Then click Programming and IDLE 3.

Please make sure that you are running IDLE for Python 3!

Using Python Shell

We know that IDLE is an integrated environment with many different tools bundled together. When you start IDLE, the window that opens is the Python Shell tool.

Any instruction you type into Python Shell is sent one-line-at-a-time to the Python interpreter and then to CPU. And, whatever the result of the instruction is, it is sent from CPU through the interpreter back to the shell, which then displays it. Let’s try a few math expressions and see what happens.

It seems that the Python shell works as a calculator! Awesome!

Try a few math expressions on your own; try more complicated expressions like 3 + 7 * 2 or (3 – 8) * (-5). Has Python make any mistakes? What happens when you add extra spaces?

Python operators, values, and expressions

The +, , *, and / are called (arithmetic) operators. Whole numbers (like 234 or -87) are called integers, or integer values, or simply ints. Numbers with a decimal point (like 61.4 or -3.1415) are called floating point values or simply floats. Python supports many other operators and many other types of values. We will discuss them later.

Values and operators can be combined into expressions. The result of an expression is also a value. That’s the value displayed in blue in the Python shell.

Syntax errors

Have you made any typos and got an error message? Try for example 2 +. You should get a so called syntax error because 2 + is not an expression. When you get an error like this, you can fix the expression and retype it again.

[cws-ad1]

Variables and statements

When you want to use a value later, you can store it in a variable. You can think of variable as a container that holds exactly one value. When you write container1 = 5, we say that a value 5 is assigned to a variable container1. The value being assigned can be a simple value (like 2 or -74.6) or a result of an expression (like 2 * 3 – 25). Look at the examples below and try it for yourself in your Python shell.

As we already mentioned, variable x can hold only one value. At first, it was 5. Later, it got replaced by a new value -2. We say that a new value overwrote the old one.

x = 5 is called the assignment statement and = is called the assignment operator. Notice that, unlike expressions, statements do not return values.

Statements don’t return a value so Python shell does not show anything. So how can we tell if a statement succeeded? It’s actually quite simple – a statement was successful if there is no error message.

[cws-ad1]

Exercises

  1. Restart IDLE; i.e. close the Python Shell window and start IDLE again.
  2. Type x1 into the Python shell. What happened? Is it expected?
  3. Type x1 = 5 then x1 again. What happened? Is it expected?
  4. In the Python shell, calculate 5! (read 5 factorial). Remember that 5! = 1 * 2 * 3 * 4 * 5. Calculate 6! and 7! Did you reuse the previously calculated values?
  5. Use 3 variables – saras_age, my_age, and age_difference. Assign 12 to saras_age, assign your age to variable my_age. Then assign the difference between the two variables to age_difference.

Conclusion

Congratulations! You have completed another lesson! From now on, you can use Python shell instead of your regular calculator! Isn’t it cool?

[prev-next-ad1 prev=”https://codewithsara.com/python-with-sara/python-101/u1-starting-with-python/u1l1-installing-python/” next=”https://codewithsara.com/python-with-sara/python-101/u1-starting-with-python/u1l3-first-python-program/”]