Creating Your First Program in Python
Welcome to codeswithpankaj.com! If you're just starting with Python, this guide will help you set up Python, write your first program, and run it in multiple environments. Let's get started!
Step 1: Installation of Python
Download Python
Visit the official Python website: https://www.python.org.
Click on the Download Python button for your operating system (Windows, macOS, or Linux).
Install Python
Run the downloaded installer.
Ensure you check the box that says "Add Python to PATH" before clicking "Install Now."
Follow the on-screen instructions to complete the installation.
Verify Installation
Open a terminal or command prompt.
Type the following command and press Enter:
You should see the Python version displayed (e.g.,
Python 3.x.x
).
Step 2: Creating and Running a Python Program
Write Your First Program Open a text editor (e.g., Notepad on Windows) and type the following code:
Save the File Save the file with the
.py
extension, for example,hello.py
.Run the Program
Open a terminal or command prompt.
Navigate to the folder where you saved
hello.py
using thecd
command.Type the following command and press Enter:
Output:
Step 3: Writing a Python Program using the Command-line Window
Open a terminal or command prompt.
Type
python
and press Enter to enter the interactive mode.In the Python shell, type:
Press Enter, and you'll see:
This is a quick way to test Python code without creating a file.
Step 4: Using Python IDLE
Open Python IDLE
After installation, search for IDLE in your system and open it.
Write Your Code
In the IDLE shell, type:
Press Enter, and you'll see:
Create a Script in IDLE
Click on File > New File to open the editor window.
Type the following code:
Save the file with a
.py
extension (e.g.,script.py
).
Run the Script
Press
F5
or go to Run > Run Module.Output in the IDLE shell:
Step 5: Interactive Python
Python's interactive mode allows you to test snippets of code quickly.
Open a terminal or command prompt.
Type
python
to enter interactive mode.You can directly type Python commands and see immediate results. Example:
Output:
Conclusion
Congratulations! You've written and executed your first Python program using multiple methods. Python's simplicity and flexibility make it an excellent choice for beginners and professionals alike. Keep practicing, and stay tuned to codeswithpankaj.com for more tutorials!
Example Summary
Environment
Code
Output
Command Prompt/Terminal
print("Hello, World!")
Hello, World!
Python IDLE Shell
print("Learning Python is fun!")
Learning Python is fun!
Interactive Mode
name = "Pankaj"; print(f"Hello, {name}!")
Hello, Pankaj!
Happy Coding! 🚀
Last updated