We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Chapter 1:
Getting Started with Python
Page + 1 backlink
1.1 Setting Up Your Environment
Before running our first program in Python, first we'll need to install the recent
version of Python in our computer. Next, we'll need to install a text editor to work
with our Python programs. Text editors such as Visual Studio Code can recognize
Python code and highlight sections as we write, making it easier for us to
understand out code structure.
Python Versions
As of writing, the current version of Python is 3.12, but every code found in this
course should run using Python 3.9 or later.
Running Snippets of Python Code
We can run Python's interpreter using our operating system's terminal window. The
interpreter lets us try Python code without having to save and run an entire program.
To start, just open the terminal window and type the python command. In this
course, code snippets on the terminal will be shown like this:
>>> print('Hello Python! ')
Hello Python!
The three angle brackets >>> prompt, or the Python prompt, indicates that we
should use the terminal window. After the prompt is where we type in and execute
Python code by pressing ENTER.
About VS Code
Visual Studio Code (VS Code) is a powerful text editor that we will use for creating
our Python programs. It can be installed on all modern operating systems, and it
supports most programming languages, including Python.
1.2 Python on Different Operating Systems
Python is a cross-platform programming language, which means it runs on all the
major operating systems. Any Python program we write should be able to run on any
modern computer that has Python installed.Python on Windows
Windows doesn't usually come with Python, so we'll need to install it and VS Code.
Installing Python. To check if Python is installed in our system, we open command
prompt or powershell. In the terminal window, enter python in lowercase. If we get
the Python prompt >>> in response, Python is installed in our system. But, if we see
an error message telling us that python is not a recognized command, or Microsoft
Store opens, Python is not yet installed.
If Python is not yet installed, or if we see a version earlier than Python 3.9, then we
need to download the latest version installer for Python from the official site:
https: //python.org.
After downloading the, run the installer. Make sure to select the option Add Python
to PATH, which will make it easier to configure our system correctly.
Running Python in a Terminal Session. To start running Python code, open a new
terminal window and enter python in lowercase. We should see a Python prompt
>>>, which means Windows has found the version of Python we just installed
:\> python
Python 3.x.x (...) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", “credits” or "license" for more information
>>>
Enter the following line in our Python session:
>>> print('Hello Python! ')
Hello Python!
>>>
We should see the output Hello Python! Anytime we want to run a snippet of
Python code, open a terminal window and start a Python session. To close the
session, press CTRL-Z and then press ENTER, or enter the exit() command.
Installing VS Code. We can download an installer for VS Code at
https: //code.visualstudio.com. Click the Download for Windows button and
run the installer.
Python on Linux (Ubuntu)
Linux systems are designed for programming, so Python is already installed on most
Linux computers.
Checking Your Version of Python. Open a terminal window and enter python3 in
lowercase. If Python is installed, the command starts the Python interpreter. We
should see output indicating which version of Python is installed and the Python
prompt >>> where we can start entering Python code:
$ python3
Python 3.10.4 (...) [GCC 11.2.6] on linuxType "help", "copyright", "credits" or "license" for more information.
>>>
This output indicates that Python 3.10.4 is currently the default version of Python
installed on the computer. To close the session, press CTRL-D and then press
ENTER, or enter the exit() command. The codes used in our course will need
Python 3.9 or later, so make sure to install the latest version.
Installing VS Code. On Ubuntu Linux, we can install VS Code from the Ubuntu
Software Center. Open Ubuntu Software then search for vscode. Click the app
called Visual Studio Code (sometimes called code ), and then click Install. Once it's
installed, search the system for VS Code and launch the app.
1.3 Running a Hello World Program
Now that we have the necessary software, we are almost ready to start
programming in Python.
Installing the Python Extension for VS Code
Before running our first program, we need to install the Python extension for VS
Code. This extension adds support for writing, editing, and running Python
programs.
To install this extension, press CTRL-SHIFT-X or click the Extensions menu on the
left side of the screen. Enter python in the search box and click the Python
extension. Click Install and install any additional tools that your system needs to
complete the installation,
Ce
Running hello.py
Before writing our first program, make a folder called python_projects on the
desktop for our projects. It's best to use lowercase letters and underscores for
spaces in file and folder names, because Python uses these naming conventions.
To start, open VS Code and close the Get Started tab if it's still open. Make a new
file by clicking File P New File or pressing CTRLN. Save the file as hello.py in thepython_projects folder. The extension .py tells VS Code that the file is written in
Python, and tells it how to run the program and highlight the text in a helpful way.
After saving our file, enter the following line in the editor then save the file:
int
Open the in-app terminal of VS Code by clicking View > Terminal or pressing CTRL-
~ . This will open a terminal at the bottom of the editor. In here, we navigate to the
location of the file using commands. To run our program, we type in the command
python hello. py
We will be running most of our programs directly in VS Code. However, sometimes
it's useful to run programs from an external terminal instead. For example, we might
want to run an existing program without opening it for editing.
We can do this on any system with Python installed if we know how to access the
directory where the program is stored. We use the cd command, for change
directory, to navigate through the filesystem in a command window. The command
1s,, for list, shows all the files that exist in the current directory. Ensuring that we
have the Python program in the current directory, we can now run the program. For
example:1.4 Troubleshooting
Here are general solutions for any programming problem:
* When a program contains a significant error, Python displays a traceback,
which is an error report. Python looks through the file and tries to identify the
problem. Check the traceback as it might give a clue as to what issue is
preventing the program from running. For example:
essen a um Caen sy
PICeN soe O ON cme sae) eae ae
ste nee aE)
SyntaxError: unterminated string literal (detected at line 1)
PS C:\Users\ADMIN\Desktop\python_projects> ff
* Step away from the computer, take a short break, and then try again. Remember
that syntax is very important in programming, so something as simple as
mismatched quotation marks or mismatched parentheses can prevent a
program from running properly.
Python Learn Python in One Day and Learn It Well. Python For Beginners With Hands-On Project. (Learn Coding Fast With Hands-On Project Book 1) by LCF Publishing Jamie Chan (Publishing, LCF) (Z-Li