Python Getting Started
Python is a powerful and easy-to-learn programming language. This guide will help you get started with Python by covering installation, running Python programs, and using the command line.
01. Python Installation
Many PCs and Macs come with Python pre-installed. To check if Python is installed on your system, follow these steps:
Check Python on Linux or Mac
Open the terminal and type:
python --version
If Python is not installed, you can download it for free from the official website: python.org
02. Python Quick Run
Python is an interpreted programming language, meaning you write Python (.py) files in a text editor and execute them using the Python interpreter.
Running a Python File
To run a Python file, use the command line:
C:\Users\Admin>python firstprogram.py
Where firstprogram.py
is the name of your Python file.
Creating Your First Python File
Let's create a simple Python file named firstprogram.py
in any text editor and add the following code:
Example:
firstprogram.py
print("Hello, World!")
Save your file → open the command line(cmd) → navigate to the directory where you saved your file → run:
python firstprogram.py
Output:
Hello, World!
Execution:
03. Python Version
To check the Python version in the editor or on your system, you can use the sys
module:
Example
import sys
print(sys.version)
Output:
3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)]
Execution:
04. The Python Command Line
Sometimes, it's quicker to test short Python scripts directly in the command line instead of creating a file. Python allows you to run code interactively in the command line.
Running Python in Command Line
Open the command line and type:
python
Or, if the python
command does not work, try:
py
Example
Once in the Python command line, you can type and execute Python code:
>>> print("Hello, World!")
Hello, World!
Exiting the Python Command Line
When you're done, type the following to exit:
exit()
Commands Execution:
Conclusion
In this tutorial, we covered how to install Python, run Python scripts and execute Python code in the command line.
Comments
Post a Comment