Now we have installed Python 3 successfully, we are going to make our first Python program. In order to do this, We’ll first open the Python 3 terminal session in Windows from the installed programs.
After clicking and launching the Python terminal, type the following code, which will print the string in the screen.
print ('Hello.')
Code language: Python (python)
You will get the output as shown above. Alternatively, you could run the Python shell interactively from the command line.
In order to do that, if you are in Windows, first open up the command prompt. In Windows you can find the command prompt in the menu or you search for it by typing command prompt or cmd. Then when you type python and enter you will see it looks same like the Python terminal session.
Python also ships with an IDE which is called IDLE and also stands for integrated development environment. in order to start IDLE, locate it in the Python installation folder. Pn Windows you can search for IDLE and launch it from the search results. You can also navigate to the Python folder and launch IDLE from there.
In macOS you’ll find idle in the Python folder located in the applications folder. Linux users may be able to navigate to IDLE in the menuing system if they’re using a graphical user interface. You can also launch IDLE from the command line by opening up a terminal and typing in idle3.
Once IDLE is launched it looks the same on all operating systems. When you first launch IDLE it will open a Python shell. This allows you to interact with the Python interpreter directly. Here you can type in Python commands which are executed right away and you receive immediate feedback.
Python is called an interpreter because it translates or interprets the Python language into a format that is understood by the underlying operating system and hardware.
As you can see below I typed my Python code in IDLE and when I hit enter the result is displayed in the screen.
To create a Python program in IDLE, go to the File menu and select New,which will bring up an editor. We can use this editor to type in our Python code and save it to run at a later time. I’ll go ahead and type in our Python code. In order to execute this code you will need to save it. If you forget to save IDLE remind you to save it before executing. When saving you can just type the name of the file and it will be saved automatically with the .py extension.
To execute or run the program I’m going to go to the run menu and select run module. You can also hit F5 to accomplish the same task. If we look at the other window you can see that the program was executed and the resulting output was displayed.
As you can see in the above figure, you will notice that the IDLE supports syntax highlighting which can help you visually determine if you have a syntax error and are doing something that Python just doesn’t understand.
You can also type the above code in a Notepad or any other code editor (we are going to discuss about some popular code editors and IDEs in the following sections) and save the file with .py extension. For example, I will type this code in a Notepad and save it as hello.py.
In order to run the above python file and if you are in Windows, first open up the command prompt. Then just type in ‘python’ and then follow it with the name of the Python program we want to execute. I’ve saved this hello.py Python program on c:\python_work.
I can run the program by providing a path to the file like so. I’m just going to type in Python and then I’m going to give a full path to that file and then the output is produced by running that program.
python C:\python_work\hello.py
Code language: Python (python)
Just be aware that you have to include the full name of the file including the .py extension when running it from the command line.
Let’s move on to Mac and Linux. Here the process is very similar. To get to the command line open up a terminal. On Mac the terminal is located in the utilities folder that resides in the applications folder.
Once the terminal is started, type python3 and then follow it with the name of the Python program you want to execute.
python3 hello.py
Code language: Python (python)
If you are using Python 2 then you would run,
python hello.py
Code language: Python (python)
In addition to supplying the Python file to the python3 command, you can execute the file directly by setting the execute bit on the file and specifying Python in the interpreter directive on the first line. To set the executable bit on the file run,
chmod +x hello.py
Code language: Python (python)
When we look at the contents of this file with the ‘cat’ command.
#!/usr/bin/env python3
print ('Hello')
Code language: Python (python)
To set the interpreter directive make sure the first line in your Python file looks like the first line in the above Python file.
#!/usr/bin/env python3
Code language: Python (python)
Now that the interpreter directive line is in place we can run the Python program by using relative or absolute path to the file. I can do that by running
./hello.py
Code language: Python (python)
and hitting enter one could run the file by providing the entire or full path to that file. We’ll do that by typing in
/users/w3computing/hello.py
Code language: Python (python)
Note that it’s safe to include the interpreter directive even if the program will be executed on a Windows system. Windows will simply ignore that line and execute the remaining Python code.
You must be logged in to post a comment.