If you are here, you have recently installed python on your machine or you want to do so. Python IDLE is one of the first software you will use to learn python. This article will discuss everything you need to know about Python IDLE to get started with it.
What Is Python IDLE?
When you install python on your Windows or Mac, IDLE comes bundled with the installation. IDLE stands for Integrated Development and Learning Environment. Looking at the name, you can guess that IDLE helps you get started with learning python. If you are a beginner, it can be one of the best tools for you to learn python.
Python IDLE as an Interactive Python Interpreter
IDLE can work as an interactive interpreter. You can write python commands line by line and it will execute them to show the results. You can also call the interactive python interpreter a python shell. A python shell is designed to read the python command, evaluate the statement, print the result, and repeat the same process until you exit.
If you want to learn about variables, assignments, mathematical and bitwise operations, or operations on data structures like lists and strings in python, you can use python IDLE to execute the statements line by line.
If required, you can also write small blocks of code in python IDLE. However, I will advise you to use a python file to write blocks of code in a python program and then execute it.
Python IDLE as a File Editor
While writing blocks of code like a function or a loop, you can create a python script file with a .py extension.
All the python files have a .py extension. You can execute any number of statements using a single python file at once. With Python IDLE, you can create and modify existing python files very easily.
Python IDLE as an Integrated Development Environment
An integrated development environment(IDE) provides you with different tools and features that help you write programs in an easy manner. Python IDLE also provides different features like syntax highlighting, auto-indentation, code completion, highlighting, and refactoring the code. This article will discuss how we can use all these features in a python IDLE.
How to Install Python IDLE?
When you install Python on a Windows machine or MacOS, python IDLE is bundled with python installation. You need not install IDLE separately.
On Linux machines, you can install python IDLE using apt-get using the below steps.
First, run the following update command to update repositories.
sudo apt-get update
Here, the sudo command is used to run the statement as a superuser and you need to have an admin password to execute this command.
After the update statement, you can install python idle using the following statement.
sudo apt-get install idle
After execution of the above statement, the IDLE will be installed on your Linux machine. You can verify this by searching the app in the search bar as shown below.
IDLE ExamplesAlternatively, you can run the command “idle” in the command line terminal. After executing the command, python IDLE will be launched.
IDLE ExamplesOn a Windows machine, you can search idle in the start menu and start the app.
Getting Started with Python IDLE
Once you start the IDLE, the following screen will appear.
Python IDLE ExamplesOn the screen, you can see that the following text is written.
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
Let us type “help
” and see what happens.
The program suggests to type “help()”.Let us do that.
After typing help()
, the following screen appears.
In the help menu, you can type anything to know what it means in python. For instance, let us type the + sign in the help menu. After execution, the following output is displayed.
IDLE ExamplesSimilarly, when we type any valid literal in the help menu, the corresponding documentation will be shown to help you understand the functionalities.
To get back to the IDLE editor window from help menu, you can type “ctrl+D
”. It will take you to the home screen.
In a similar manner to help, you can type ‘copyright
’ to see who owns the copyright of the IDLE as shown below.
You can also see the credits by typing ‘credits
’ in the IDLE as shown below.
Having seen these features, let us now discuss how we can perform different operations in a python IDLE.
Using IDLE as an Interactive Python Interpreter
You can use python IDLE as an interactive interpreter and execute statements one by one. You can perform mathematical operations, comparisons, take input from user, and many more tasks. Let us discuss some of them.
Mathematical Operations in IDLE
You can add two numbers as you do in a calculator. To perform any mathematical operation, you just need to type the mathematical expression and hit the enter key. The interpreter will show the output. You can observe this in the following image.
IDLE ExamplesHere, we have performed various mathematical operations. You can observe that the output is immediately displayed on the next line in the IDLE when we hit the enter key after typing an expression.
Comparison Operations in IDLE
You can also use comparison operators to compare two numbers in the IDLE. If the expression evaluates to True
, the interpreter will print True
. Otherwise, it will display False
.
You can also combine mathematical expressions and comparison operators to produce the output.
IDLE ExamplesOperations on Variables in IDLE
Instead of performing operations directly on numbers, you can also define variables to store values.
A variable in python can be used to store any number, string, or other data type. For example, you can define two variables num1
and num2
with values of 10 and 20 as shown below.
You can print the values in the variables by simply typing the variable name in the prompt as shown below.
IDLE ExamplesNow, you can add 10 and 20 by using num1
and num2
as follows.
You can also assign the sum of num1
and num2
to another variable as shown below.
Here, we have assigned the sum of num1
and num2
to num3
. Then, we printed the value in num3
.
Print Statement in IDLE
Apart from the numerical values, you can also assign text values to variables. A text is stored as a string in python. To define a string variable, we enclose the text inside single quotes or double quotes as shown in the following image.
IDLE ExamplesHere, we have created a variable named website
and assigned it the value “Pythonforbeginners
”. You can print the value in the variable by typing the variable name in the prompt as shown above.
Instead of directly typing the variable name in the prompt, you can use the print()
function to print any value. The print()
function takes a value or a variable as its input argument and prints it in the terminal as shown below.
You can also print multiple values by separating them by a comma in the print()
function as shown below.
Here, the values are separated by space characters.
You can separate the values inside a print statement using commas by using the sep
parameter in the print()
function. The sep
parameter takes a character or a string as an input argument and separates the values passed to the print()
function using that character as shown below.
Take User Input in IDLE
You can also take user input from the user using the input()
function. The input()
function takes a string representing the user prompt as its input argument. While execution, it takes the user input until the user hits the enter key. Once the user hits enter key, the input()
function returns all the characters typed by the user in the form of a string. You can observe this in the following example.
In the above image, we have first taken the user input using the input()
function. We have also shown prompt to the user to enter his name. Once the user enters the value, the value is assigned to the variable name. We have then printed the value stored in the variable name.
Using Python IDLE as a File Editor
To execute multiple statements at a time, you will need to create a python script in a file with a .py extension.
To create a python file, You can select the “New File
” option from the File
menu in the upper right-hand corner of the IDLE. Alternatively, you can type Ctrl+N
to open a new untitled file.
After creating the untitled file, the following screen will appear.
IDLE ExamplesYou can write your code as shown below.
IDLE ExamplesIn the above code, the program asks the user to input his name and age. After that, it calculates the birth year of the user and prints the result.
The input()
function always returns a string value. Therefore, we have used the int()
function in the fourth line of the code to convert the input age into integer.
In the last print statement, we have used the str()
function to convert the integer into string so that the interpreter can perform string concatenation and print the result.
After writing the code, you will need to run the python script. For this, you first need to save the file with a .py extension. You can go to the File-> Save
option and save the file by giving a filename. After that, you can click on Run-> Run Module
to run your python script.
Once you click on the “Run module
”, the program will be executed in the IDLE and the output will be shown as given in the following image.
Other Operations on a Python IDLE
- There is no way to clear the IDLE terminal. You need to close the IDLE and restart it to clear the screen.
- You can change the font, indentation width, highlighting preferences, shortcuts, and other features by clicking on
Options-> Configure IDLE
and choosing the preferred features. - You can read the official python documentation by clicking on
Help-> Python Docs
. When you click on the Python Docs button, you will be redirected to the official documentation of the python version installed on your computer. - To stop a program execution before its completion, you can type ctrl+C.
- To close the python IDLE, you can type
ctrl+D
on your keyboard.
Conclusion
In this article, we have discussed various operations that will help you get started with python IDLE. To know more about python programming, you can enroll to this python course for beginners.
The post Python Idle: A Beginners Guide appeared first on PythonForBeginners.com.