👋 Hello, World!
Hi! if you are reading this article, then you are probably starting to dive into the amazing world of programming and computer science. That's great.
In this article, you will learn:
- How to write your first
"Hello, World!"program in Python.
- How to save your code in a Python file.
- How to run your code.
Writing this program when you are starting to learn how to code is a tradition in the developer community.
Enjoy this moment because it will definitely be part of your memories in the coming months and years when you remember your first steps.
🔹 "Hello, World!" in the Python Shell
Step 1: Start IDLE
During this article, we will work with IDLE (Python’s Integrated Development and Learning Environment), which is automatically installed when you install Python. This is where you will write and run your Python code.
The first thing that you need to do is to open IDLE. You will immediately see the screen shown below.
This is called the Python shell (interactive interpreter). It is an interactive window where you can enter lines or blocks of code and run them immediately to see their effect and output.
💡 Tip: By default, you will see a smaller font size. You can customize this in "Options > Configure IDLE".
Step 2: Display the Message
You need to tell the program that you want to display a specific message by writing the appropriate line of code.
In Python, we use
print() to do this:
- First, we write
- Then, within parentheses, we write the message or value that we want to display.
💡 Tip: The message
"Hello, World!" is surrounded by double quotation marks because it is represented as a
string, a data type that is used to represent sequences of characters in your code (for example, text).
Step 3: See the Output
You will see the following output if you write this line of code in the Python shell and press enter:
💡 Tip: You will notice that the color of the message inside
print() changes to green when you add the last quotation mark.
This occurs because IDLE assigns different colors to the different types of elements that you can write in your code (notice that
Great! You just wrote your first
"Hello, World!" program in Python.
If you want to save it in order to run it later (or just to keep it as a nice memory of your first Python program!), you will need to create a Python file, so let's see how you can do that.
🔸 "Hello, World!" in a Python File
Step 1: Create a File
To create a Python file in IDLE, you need to:
- Open the Python Shell.
- Click on
Filein the toolbar.
- Click on
💡 Tips: You can also use the keyboard shortcut
Ctrl + N.
After you click on
New File, you will immediately see a new file where you can write your code:
Step 2: Write the Code
In the new file, write this line of code to print
💡 Tip: The thick vertical black line shows where the cursor is currently at.
Step 3: Save the File
Save the new file by clicking on File > Save or by using the keyboard shortcut
Ctrl + S. You will need to write a name for your file and choose where you want to save it.
After saving the file, you will see something very similar to this in the folder that you selected:
💡 Tips: By default, line numbers will not be displayed in the file. If you would like to display them (like in the images above) go to Options > Configure IDLE > General > Check the "Show line numbers in new windows" box.
Step 4: Run the Program
Now you can run your file by clicking on Run > Run Module:
A new window will be opened and you should see the output of your program in blue:
Now your program is safely stored in a Python file and you can run it whenever you need to.
🔹 Customize Your Program
You can customize your program to make it unique. You just need to edit the Python file and change the string.
For example, you can add your name after
If you run the file, you will see the string displayed in the Python shell:
🔸 First Python Program Completed
Awesome work. You just wrote your first Python program.
Programming and Computer Science will be key for the future of humanity. By learning how to code, you can shape that future.
You'll create amazing new products and platforms, and help take us one step further towards a world where technology will be part of every single aspect of our daily lives.
To learn more about the coding uses of Python, you might like to read my article "What is Python Used For? 10+ Coding Uses for the Python Programming Language"