Thursday, September 29, 2016

Quickstart on IDLE




On your Raspberry Pi, one of the quickest ways to begin programming in Python is by using the program IDLE, a Python development environment. Let's begin exploring and understanding IDLE, so our Python projects can go more smoothly.




Opening IDLE
IDLE can be found in your menu under "Programming." You'll find two options here -- Python 2 and Python 3. Both of these are IDLE, however, Python 2 uses Python 2.7 (a more legacy version of Python), and Python 3 uses Python 3.4.




Using the Shell
Once you open IDLE (you can open Python 2 or Python 3, for this post it does not matter), you see a screen pop up that say "Python Shell" in the title bar, and list the version of Python you're using. You'll also be prompted with three arrow keys, that look like the following:

>>>

This is called a REPL (Read Evaluate Print Loop), and it's where we can type some Python commands. We can store variables here. For example, try typing the following:

>>> name = "Fluffy"
>>> "Hello " + name

What happens when you press enter? you should see it return the following:

'Hello Fluffy'

We can also use it as a basic calculator:

>>> 2 + 2

Upon hitting enter, it should spit out:

4

The shell is great for running commands like this, however, let's explore some different options for writing code in IDLE.

Hello World
To write a program we are going to want to create a file that we will run through the shell. This allows us to create a program instead of running just basic commands. To do this from IDLE, go to File > New Window. You'll see an empty page open (that looks a lot like a simplified Word Document!) and we will type in the following:

print("Hello World!")

Save your file as helloworld.py (make sure you put "py" and not "pi"!) and let's hit run! What happens?

You should be seeing your program run through the Python Shell and print 'Hello World!'

These are the basics of writing Python in IDLE. We will use this for most projects we do on this blog.



Questions? Comments? Ideas for future posts? I want to hear them! Please leave them in the comments below.

No comments:

Post a Comment