Nearby lessons
4 of 108Python - Writing and Running Python Programs
Introduction
Python Shell is useful for testing small programs.
For complete Python programs, you should use the Python IDLE Editor.
In this chapter, you will learn how to create, save, and run Python programs using IDLE.
When Should We Use Python IDLE?
Python IDLE is suitable for writing complete Python programs.
Use IDLE when you want to:
- Write programs with multiple lines of code.
- Save Python programs.
- Edit existing programs.
- Run complete Python files.
Open Python IDLE
Follow these steps to open Python IDLE.
- Open the Start Menu.
- Search for Python IDLE.
- Click Python IDLE.
The Python Shell window will open.
Create a New Python File
To write a new Python program:
- Click File → New File.
- Or press Ctrl + N.
A new editor window will open.
This editor is used to write Python programs.
Example 1 - Print "Hello" Multiple Times
The following program reads a number from the user and prints Hello that many times.
main.py
Enter some number: 5 Hello Hello Hello Hello Hello
Program Explanation
Line 1
input() reads a value from the keyboard.
int() converts the entered value into an integer.
Line 2
range(n) creates numbers from 0 to n - 1.
The for loop runs n times.
Line 3
The print() function displays Hello in each iteration.
Save the Python Program
After writing the program, save it before running.
You can save the file by:
- Clicking File → Save
- Or pressing Ctrl + S
Python File Extension
Every Python program must be saved with the .py extension.
Examples:
- demo.py
- sample.py
- program.py
- test.py
You can choose any file name, but the extension must always be .py.
Choose a Save Location
While saving the program:
- Select a folder.
- Enter the file name.
- Click Save.
Run the Python Program
After saving the file, run the program.
You can use either of the following methods:
- Run → Run Module
- Press F5
The F5 shortcut is the fastest way to run a program.
Program Execution
When you press F5, the Python Shell opens automatically and executes the program.
main.py
Hello Hello Hello
Example 2 - Simple Calculator
The following program reads two numbers and performs basic arithmetic operations.
main.py
Enter first number: 20 Enter second number: 10 30 10 200
Running Without Saving
If you press F5 before saving the file, Python will ask you to save it first.
Always save your program before running it.
Useful Keyboard Shortcuts
| Shortcut | Purpose |
|---|---|
| Ctrl + N | Create a new file. |
| Ctrl + S | Save the current file. |
| F5 | Run the Python program. |
Common Beginner Mistakes
- Forgetting to save the file.
- Saving the file without the
.pyextension. - Writing large programs directly in Python Shell.
- Forgetting to press F5 after writing the program.
Key Points
- Use Python IDLE for complete programs.
- Create a new file using Ctrl + N.
- Save the file using Ctrl + S.
- Always save Python files with the
.pyextension. - Run the program using F5.
- Python Shell opens automatically after running the program.
Quick Summary
| Topic | Description |
|---|---|
| Editor | Python IDLE |
| Create New File | Ctrl + N |
| Save File | Ctrl + S |
| Run Program | F5 |
| Python File Extension | .py |