How to Write Your First Python Program
In the previous blog, we learned how to install and verify your Python setup. In this blog, we will learn how to write your first Python Program.
The first program will use the Python built-in function Print. It is used to display output to the console or terminal. The print command is often used in Python programming to display an output.
Steps to write your first python program
1. Open a Text Editor: To write Python code, you’ll need a text editor. You can use a basic text editor like Notepad or Integrated Development Environment (IDE) like Visual Studio Code, PyCharm, or Anaconda. If you are using Notepad, it is just like writing a text document, it won’t highlight any syntax errors, etc. However, if you’re using an IDE, it will notify you when you make any error which can be useful as you progress in Python programming.
2. Write the Code: Open Notepad and enter the following simple code:
print(“Hello, OhioComputerAcademy!”)
Here we are using the print function (We will discuss more about functions in the subsequent blogs). It is a simple function that is used to show the text on the screen. In this example it will display Hello, OhioComputerAcademy!.
3. Save the File in any folder: Save the file with a .py extension (.py will let your system recognize it as a Python script), such as hello.py, in a directory of your choice. For example, I created a folder named python and saved the file there. Follow the proper naming convention to avoid any confusion.
4. Run the Python program: Now Search for cmd or PowerShell in your Windows start program. Open Command Prompt (cmd), and navigate to the hello.py file location. Use the command cd Destination folder (ex: cd python. python is my destination folder if you have multiple folders, separate each folder with / or \on Windows in the file path, ex: users/yourname/foldername) and type python hello.py or python3 hello.py. You should be able to see the output
Hello, OhioComputerAcademy!
printed on the screen. Follow the commads as shown below:
Congratulations, you have successfully executed your first program. In the next blog, we will learn programming essentials using Python.