Welcome to this quick and easy beginners tutorial on how to use Python and get started by writing your very first line of Python code.
This tutorial requires that Python is already installed, so if you don’t have it installed yet, check out my tutorial on how to install Python on Windows, MacOS and Linux first. Its very easy to install, and shouldn’t take more than 5-10minutes. Once done, come back to this tutorial.
Launch the Python Interpreter
Once Python is installed on your computer, you should see it as an application (Python.exe) in your start menu in Windows, or in the default installed directory at “C:\Users\YOURUSERNAME\AppData\Local\Programs\Python\Python310”, unless you set a custom directory during the install process.
If you click and launch it you will see a Terminal style window appear. This is known as the Python Interpreter.
The most basic way to write code in Python is using the Python Interpreter, which is an interactive Read-Eval-Print Loop (REPL) environment. This simply means that you can launch it and start typing Python commands to it directly.
The REPL interpreter stands for:
- Reads the command you enter
- Evaluates and executes the command
- Prints the output (if any) to the console
- Loops back and repeats the process
This means that the session will continue in this manner until you instruct the interpreter to terminate by typing exit() or by closing the window.
exit()
Most of the example code in this tutorial series is presented as REPL interaction, although I will also show you a better method further down, where we use Notepad to create a .py (Python) file.
Open Windows Command line or Mac/Linux Terminal
There is also a second alternative interactive interpreter that you can use instead of the installed Python version.
In windows, this is called the Windows Command Line (CMD) and in MacOS or Linux, this is known as the Terminal.
To find and launch these interpreters, do one of the following, depending on your Operating System (OS):
- In Windows press the “Start” Button the type “CMD” and press “Enter“
- In MacOS press the “Command+Space” keys to search for the “Terminal”
- In Linux press the “CTRL+ALT+T” keys to launch the Terminal.
Now that your respective terminal is open simply type “python” to run and activate Python.
python
This tells the CMD/Terminal that you want to code in Python going forward, instead of the default system commands that these interprets are set to.
The CMD or Terminal should output a confirmation message as follows, telling you that you are now in Python editing mode.
You will notice that this is the exact message we saw when launching the Python Interpreter, showing that you can either use it or the built in CMD/Terminal in your OS, as both will give the same results.
With either the Python Interpreter open, or the CMD/Terminal open and Python activated, let’s write our first line of Python code.
How to Code in Python
Now that the Command Line on your chosen OS is running, you can start writing your first Python code within it.
Let’s start with a basic one line print syntax.
- Type: print(“Python is easy”)
- Press “Enter”
print("Python is easy")
#>> Python is easy
Amazing! You created your first line of Python code, which outputs the sentence “Python is easy”.
Let’s try another one. This time change it to “Python is lots of fun!”.
- Type: print(“Python is lots of fun!”)
- Press “Enter”
print("Python is lots of fun!")
#>> Python is lots of fun!
The sentence “Python is lots of fun!” is output in the terminal.
Let’s try one last one. This time change that to “I can write code”.
- Type: print(“I can write code”)
- Press “Enter”
print("I can write code")
#>> I can write code
Again, the sentence “I can write code” is output in the terminal.
As you can see, writing Python code is very easy to do, as you have now taken the first step in becoming a Python developer. This method is also known as “using the Python Interpreter”.
Possible reasons why your Python code isn’t working
Python, like every other scripting language, requires you to be 100% accurate in how you type the language. Even the smallest details such as spaces and quotation marks can make or break the code, so make sure you have it exactly as I do, in order for it to work.
If your code isn’t working as the above, then check that you didn’t make one of the following mistakes.
You forgot to enclose the string to be printed in quotation marks:
print(I can write code)
#>> SyntaxError: invalid syntax
You forgot either the opening or closing quotation mark:
print("I can write code)
#>> SyntaxError: EOL while scanning string literal
You used different opening and closing quotation marks:
print("I can write code')
#>> SyntaxError: EOL while scanning string literal
You forgot the parentheses ( ):
print"I can write code")
#>> SyntaxError: Missing parentheses in call to 'print'
You entered extra white space characters before the command:
print"(I can write code")
#>> SyntaxError: unexpected indent
You will encounter many of these syntax errors during your journey of learning and coding in Python, even at a professional level, as we do tend to make mistakes as human beings. The important thing to know is what it means and therefore, how to fix it.
With that said, let’s move on to how to use Notepad to better execute Python code.
How to Run Python Code from the Command Line
While the Python Interpreter method works, it’s also very limited in that it only executes code line by line, therefore you cannot run two lines of code without manually typing them both in.
In other words, you cannot write all the three sentences and execute them at the same time, using the Command Line.
In order to run multiple lines of code at the same time, we need to use a different method, by first creating a .py script file with the lines of code saved in that, then use the Command Line to call and run that script.
How to Compile and Save Python Files
To create a .py file is easy to do using a text editor such as Notepad in Windows or any similar text editor in your OS.
In Windows you can press “Start” then type “Notepad” and press enter to launch it. You can also “Right Click” anywhere on your desktop , then select “New > Text Document”.
With the text open on a blank document, let’s add in our Python lines of code, and then save it as a .py file, by doing the following:
- Type our three print syntax’s statements
- print(“Python is easy”)
- print(“Python is lots of fun!”)
- print(“I can write code”)
- Click “File>Save” or “CTRL+S” to save the file to your desktop.
- Close the file
- Rename it “mypython.py” (or any name you want), as long as it has the “.py” extension and no longer contents the “.txt” extension.
print("Python is easy")
print("Python is lots of fun!")
print("I can write code")
How to run .py file from Windows Command Line
With the .py file created and saved with our three lines of code inside it, it’s time to again go back to the Command Line terminal, but this time we are going to use it to execute the .py file we just created, instead of typing Python code directly in it.
exit() out of Python Coding mode
The first and very important step to make this work properly is to first exit out of Python coding mode by typing “exit()” in the CMD.
Unlike the default Python Interpreter this will not close the CMD or Terminal window, but rather simply exit out of Python coding mode, so that we are back in the system execution mode.
We require the system mode, as we are going to use it to run our Python script, instead of typing Python directly into the CMD.
Change Directory and Execute Python Script
Once done, you should now be back in the default system mode of your CMD or Terminal window. Now let’s use it to open and run our newly created Python script:
- Type: “cd desktop”
- Type: “python mypython.py”
“cd” means “change directory”. We are therefore changing from our “C:\Users\YOURUSERNAME” directory, to our Desktop where our .py file is saved.
Once we’ve changed and point to the location that our mypython.py script is stored, we call our saved script and run it.
cd desktop
python mypython.py
It should now print out:
#>> Python is easy
#>> Python is lots of fun!
#>> I can write code
Awesome! You just ran and executed not one, not two, but three lines of Python code with a single command!
Note that you have to use either the Windows CMD or MacOS/Linux Terminal to execute saved Python scripts. The installed Python Interpreter (Python.exe) does not have access to the system commands, therefore it cannot execute saved scripts. This is the same reason we used the “exit()” command in the CMD/Terminal, as we needed to get out of the Python editing mode first.
How to run .py file from Linux Command Line
If you are using Linux, you can do the following to run the “mypython.py” file form the terminal command line:
- Press the “CTRL+ALT+T” keys to open the terminal
- Change the directory to where the script is located using the “cd” command.
- Type “python mypython.py” in the terminal to execute the script.
Alternative method
An alternative way to run a Python script through the Linux terminal, without having to type in “python” every time, can be doing as follows:
- Type: “#!/usr/bin/env python”
- Make script Executable by typing: “chmod +x mypython.py”
- Type: “./mypython.py” to run the executable script.
It should now print out:
#>> Python is easy
#>> Python is lots of fun!
#>> I can write code
How to Edit your .py file in Notepad
The great thing about using a text editor to store and write your Python code, is that you are able to go back and add or edit the code at any time.
In order to edit your Python file, you will need to open it up in a basic text editor such as Microsoft Notepad, however, as you most likely won’t have your .py file associated with Notepad, it won’t know that it needs to open it unless you tell it to, so let’s associate Notepad with Python so that it can open the file:
- Right Click on your “mypython.py” file and select “Properties” from the popup menu
- Press the “Change” button next to “Open with”
- Choose Notepad from the list. If you don’t see it click “More Apps”
- It should be on that list. If it’s not then you need to search for it in “C:\Windows\System32\notepad.exe”
- Click “OK” to save the changes
- Now all your .py files will automatically open in Notepad when you double click them.
Download and Use Notepad++
I highly recommend actually downloading and installing Notepad++ from https://notepad-plus-plus.org/downloads/.
As the name states, it is a more advanced version of Notepad and one that I use not just for some Python coding, but also as my daily Notepad, instead of the default Microsoft one. It is able to read many coding languages, including Python, making coding inside it much better than the standard Notepad.
You can select your coding language from the top menu bar under “Language > P > Python”.
Once installed, follow the same steps as above, except you would now select “Notepad++” from the list instead of Notepad.
With this done, you can easily open your .py file and edit it to your heart’s content, save it and use the command line to run it again.
How to Use Python Conclusion
I hope you enjoyed this tutorial on how to use Python. We covered an array of skills, learning how to open the default Python Interpreter, as well as the alternative Windows Command Line (CMD) or the MacOS/Linux Terminal, to activate Python scripting.
Once Python was activated, we learnt how to code in Python, by writing our first print statements. We saw that the terminal is however limited to once syntax execution at a time. To solve this problem, we created a .py file in a notepad editor and saved it to our desktop, allowing us to add multiple lines of print statements into a single file.
We then used the CMD/Terminal to execute the saved .py file instead, allowing us to run multiple Python syntax statements at once.
This is however a tedious way of going about this, as you have to use two separate applications (Notepad & the Command Line) to get the job done. A far better method is to use an Advanced Text Code Editor, IDE (Integrated Development Environment) or Jupyter Notebook.
You can also check out my tutorial on how to install PyCharm IDE for Python so that you can get coding professionally, right away.
If you enjoyed this content and would like others to benefit from it as well, make sure to share it on your favorite social Media platform, using the share buttons below.