Lesson 2 - IDLE and the first Python console application
In the previous lesson, Introduction to the Python language, we talked about the Python language itself. In today's lesson, we're going to focus on IDLE. We'll show you how to use it and program a simple console application.
IDLE stands for Integrated DeveLopment Environment and it comes with Python. In a nutshell, it's an application that lets us write code, and then use it to run, test and debug our applications.
Of course, we'll have to start by installing Python. You can download it on the official site - https://www.python.org/downloads/. Keep in mind that we're going to work with Python 3.
Download Python 3.X from the Download section and run the installer.
Once you run the file, the installation wizard will execute. Check the "Add Python 3.X to PATH" which will register the interpreter to your command line and make running your future projects easier. Aside from that, we'll get by with the express installation - click "Install Now".
Backup and version control
Programmers usually need a tool that provides version control and backup of their work. We can't rely on the fact that we could simply save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about this sort of situation right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers. You can download Dropbox at https://www.dropbox.com.
You can also use a tool called GIT for the same purposes, but its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.
Now, let's run IDLE. Since the installation didn't add a shortcut to our Desktop, enter the "Start" menu and type "IDLE". Then, click on the search result with the right mouse button and choose "Pin to Taskbar".
You can now start IDLE by clicking on the Taskbar icon. The first run might take some time.
You should now see the Python Shell window. It's a terminal through which we can send commands to the Python interpreter. It also displays the outputs.
As tradition instructs, we'll make the first program most people make when they learn a new language - Hello World. This is a program that displays "Hello world" or some similar text.
Let's call a function that prints out text, as you may have expected, it's
print(). Some functions require input parameters. In this
print() requires the value to be printed as a parameter. We
specify function parameters in the parentheses after the function name. In
Python 3, we always write parentheses (even if the function doesn't require any
parameters). This is a major difference when compared to the older Python 2.
Write the following into IDLE and press enter:
In programming, texts are called strings, like a string of characters, and are written in quotation marks. Python sees the quotation marks and interprets it as text. Without the quotes, the text could easily be misinterpreted with other language commands. We can use both double or single quotes.
The result should look like this:
By the way, you can use the Python console as an advanced calculator. Try entering math problems like:
10 * 20
Console application 200
Creating a project
Ok, this was interesting but we'd like to create actual programs instead of sending commands through the console. Click on "File" in the IDLE application menu and select "New File".
A new Window will appear. Now, let's write the Hello world program again. This time, in a separate project file.
#!/usr/bin/python3 print("Hello ICT.social") print("We'll learn more in the next lesson!")
The first line is optional but it's better to introduce it. It is designed to help run the script on some systems since it specifies the Python version.
Let's save our script by pressing CTRL + S or (File -> Save) from the application menu. If you decided to use Dropbox, create a new folder in your Dropbox folder (C:\uses\your_username\Dropbox), e.g. python. We'll name our first program "hello.py".
You can run the program by pressing F5 or (Run -> Run Module) from the application menu.
Console application Hello ICT.social We'll learn more in the next lesson!
Congratulations, you have just become a programmer That will be all for today. In the next lesson, Variables, type system and type conversions in Python, we'll look at the basic data types and create a simple calculator.
Today's project is attached as a file at the end of the article. You can always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find a mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything
1 messages from 1 displayed.