Python Getting Started
Python Installation
Most modern PCs and Macs come with Python pre-installed. To verify if Python is already installed on your system, follow these steps:
On Windows:
Open the start menu.
Search for "Python."
In the Command Line (cmd.exe), run the following command:
C:\Users\Your Name>python --version
On Linux or Mac:
Open the command line (Terminal for Mac).
Run the following command:
python --version
If you discover that Python is not installed on your computer, you can easily obtain it for free from the official website: Python.org.
Python Quickstart
Python is an interpreted programming language, which means that you create Python (.py) files using a text editor and then execute these files in the Python interpreter.
To run a Python file from the command line, use the following syntax:
C:\Users\Your Name>python helloworld.py
In this example, "helloworld.py" is the name of your Python file.
Let's create our first Python file, named "helloworld.py." You can use any text editor to do this.
print("Hello, World!")
It's as simple as that. Save your file, open your command line, navigate to the directory where you saved the file, and execute the following command:
C:\Users\Your Name>python helloworld.py
The output should display:
Hello, World!
Congratulations, you've successfully written and executed your first Python program!
The Python Command Line
Sometimes, when testing a small piece of Python code, it's quicker and easier to run it directly from the command line. Python allows you to do this by opening an interactive Python session.
To start the Python command line, enter the following on the Windows, Mac, or Linux command line:
C:\Users\Your Name>python
If the "python" command doesn't work, you can try "py" instead:
C:\Users\Your Name>py
From the Python command line, you can run Python code interactively, as shown earlier with our "Hello, World!" example:
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright," "credits," or "license" for more information.
>>> print("Hello, World!")
This will display "Hello, World!" in the command line.
To exit the Python command line interface when you're done, simply type:
exit()
With this knowledge, you're ready to embark on your Python programming journey. Happy coding!
Last updated