A1. Introduction to Python
How to Get Started With Python?
In this tutorial, we will walk through the process of setting up Python on your computer and then writing your first program.
Video: On how to install Python?
Python is a versatile programming language that can be used across various platforms such as
- Windows,
- macOS, and
- Linux, as well as being compatible with Java and .NET virtual machines.
Python is free and open-source, but additionally, it offers the advantage of being able to run on multiple platforms.
However, it is important to note that while many Linux and Mac systems may already have Python pre-installed, the version may not be up-to-date.
To ensure optimal performance, it is recommended to install the latest version of Python.
The Easiest Way to Run Python on vscode
Steps to install Vscode
Download and install Visual Studio Code (VSCode) from the official website: Download VsCode
Once VSCode is installed, open it and navigate to the Extensions tab on the left sidebar.
Search for and install the “Python” extension by Microsoft. Once the extension is installed, open a new file in VSCode and
Select “Python” from the language selection at the bottom right corner of the editor.
Start writing your Python code and save the file with a .py extension.
To run the code, open the terminal in VSCode by going to “View” > “Terminal” or using the shortcut (Ctrl + `).
In the terminal, navigate to the directory where your Python file is located and run it using the command “python [filename].py”
Your code will be executed in the terminal and the output will be displayed.
As shown below :
Install python Independently
- Go to the official Python website: Python Download
- Download the appropriate version for your operating system (Windows, macOS, or Linux).
- Once the download is complete, run the installer and follow the prompts to complete the installation process.
- To check if the installation was successful, open a command prompt or terminal window and type “python -V” this command will show you the version of python that you have installed on your machine.
- If the installation was successful, you should see the version number displayed in the command prompt or terminal window.
Note: Some operating systems like Linux and macOS have python pre-installed, so you may not need to install it.
You can check the version of python by running the command “python -V” in the terminal.
Once you finish the installation process, you can run Python.
How to run python in terminal/interactive mode?
Python is run in intermediate with Python shell or the interactive mode, which will allow you to write python expression or commands without writing the full code.
Steps for Interactive mode
- Open a command prompt or terminal window and type “python” or “python3” (depending on your installation) and press Enter.
- This will launch the Python interpreter and display the prompt “»>” or “….” as shown below.
- At this prompt, you can type any valid Python code and press Enter to execute it. For example, you can perform calculations, define variables, and call functions.
You can also use built-in functions such as print() and input() to interact with the user.
- To exit the Python shell, type “exit()” or use the shortcut “CTRL + Z” on Windows or “CTRL + D” on Linux or Mac.
Immediate mode is useful for testing small pieces of code, experimenting with new commands and libraries, and troubleshooting errors in your programs.
Your first Python Program
Let’s begin by writing your first python program. As the tradition goes, every programmer should first start by writing hello-world program.
A “Hello, World!” is a simple program that outputs Hello, World! on the screen. Since it’s a very simple program or a simple command, it’s often used to introduce a new programming language to beginners.
Create a file named hello-world.py in your directory. and then write the following statement:
print("Hello, World!")
Then, run the file. You will get the following output. If you are using Vscode, click on the green triangle button or type “python hello-world.py” to execute the file.
Hello, world!
And there you go, your first program in Python. As you can see, this was a pretty easy task. This is the beauty of the Python programming language.