Introduction To Python Programming - Part 1
A Primer on Python programming for beginners
Python is a popular, high-level, general-purpose programming language. Guido van Rossum invented it in 1991, and the Python Software Foundation continued to advance it.
Because of its syntax, which was developed with code readability in mind, programmers may be able to express their ideas in fewer lines of code.
Python is a computer language that facilitates rapid development and more efficient system integration.
Python 2 and Python 3 are the two most popular versions. Both are different.
Beginning with Python programming:
Finding an Interpreter:
- Using an Integrated Development Environment ( IDLE)
Before we start Python programming, we need an interpreter to convert and execute our programs.
Windows: The Python software downloaded from http://python.org comes with IDLE (Integrated Development Environment), one of many free interpreters that can be used to run Python scripts.
Linux: Python is preinstalled on many Linux distributions, including Fedora and Ubuntu. Type "python" in the terminal emulator to see what version of Python you're using. When it launches, the interpreter should print the version number.
Python 2.7 is typically preinstalled on macOS. Python 3 must be manually installed from http://python.org.
Open the command line and enter Python --version to see if Python is installed on your computer. This will display the Python version that is currently installed on your computer.
If any other message apart from that shows, then Python is not installed on your device. You’ll have to install Python manually from python.org.
Follow my other article, where I walk you through the process of installing Python on a Windows computer.
Python installation guide for Windows 11
- Using a code editor
Alternatively to an IDLE or an interpreter (command line), Python codes can also be written in a code editor.
Code editors are applications used for writing, editing, and executing Python code. Examples are Visual studio code, Pycharm, Sublime text, Atom, etc.
For the remainder of this tutorial, I will use Visual Studio Code as my code editor for Python programming. You can easily download and install Visual Studio Code on their official website.
Fig. 1: Official website of visual studio code
Writing our first program:
Step 1: After downloading the VSC editor, open the code editor and click on the three dashes at the top left.
Fig. 1b: A diagram displaying the VSC editor's user interface
Step 2: Click on the file and then the new file to name your first Python file.
Fig. 1c: A diagram displaying the VSC editor's user interface
Step 3: You can use any naming scheme for your file, but make sure it ends with .Py extension.
Fig. 1d: A diagram displaying the VSC editor's user interface
Step 4: Just type in the following code after naming your Python file: The codes shown below will be explained later in this tutorial
Fig. 1e: Python codes written in the Visual Studio code editor
Line 1: print(‘hello, world’)
Line 2: print(‘Python is awesome and simple’)
Line 3: print(‘My name is Akinnimi Stefan’)
NOTE: I used single quotes ' ' here; you can also use double quotes " ". But for consistency and code readability, Choose one and follow it religiously.
To print something on the console, the print() function is used.
Step 5: Executing or interpreting the code
After typing in the codes, the next step is for you to click on the Run Python File button in the upper-right corner of the editor.
Fig. 1f: Executing our first Python program
Step 6: A Terminal will open (similar to the command line on a Windows or Mac OS laptop), your program will be executed, and the results will be shown as seen below.
Fig. 1g: A diagram showing the output of the first Python program
Output:
The output of Line 1 - Hello World.
The output of Line 2 - Python is awesome and simple.
The output of line 3 - My name is Akinnimi Stefan
Congratulations!
Your first Python program has been created and run successfully.
Python Syntax
Syntax means rules that govern the use of a particular programming language.
The print Program
The print() function prints the specified message to the screen or other standard output device.
Fig. 2a: Python codes written in the visual studio code editor
Python Indentation
Indentation is the term used to describe the spaces at the start of a code line.
Whereas in other programming languages, the indentation in code is for readability only, the indentation in Python is significant.
A block of code is identified in Python through indentation.
Fig. 2b: A diagram showing the indentation (four spaces or two tabs).
Fig. 2c: Output of a successfully written Python code with indentation
NOTE: If you omit the indentation, Python will throw an error as seen in the image below.
Fig. 2d: Visual Studio Code showing an error because of the absence of a proper indentation
Fig. 2e: Output of an executed Python program without proper indentation
As a programmer, you can choose the number of spaces; the most usual use is four, although there must be at least one.
Continuation to Part Two of the Series