Installation and the Python shell

From Spark Makerspace
Revision as of 05:16, 15 January 2022 by Jjfs85 (talk | contribs) (Created page with "<span id="installation-and-the-python-shell"></span> = 1. Installation and the Python shell = <span id="summary"></span> == Summary == This section describes how to install...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

1. Installation and the Python shell

Summary

This section describes how to install Python3 and how to access the Python shell from the command line.

Installation

Windows

Using the Microsoft Store, search for Python 3 and install the latest release ### Mac Go download the official installer from the Python download website ### Linux Check what’s installed first

<syntaxhighlight lang="sh">$ python3 --version </syntaxhighlight> If you get a version number, then you’re good. Otherwise, follow a guide like this one

You should also install a few more packages to have a good setup

<syntaxhighlight lang="sh">$ sudo apt install -y build-essential libssl-dev libffi-dev python3-dev python3-pip</syntaxhighlight>

The Python shell

Python offers an interactive shell where you can run commands line by line and get results in realtime. Run this command to access the Python shell. The Python shell is also called a REPL. REPL stands for Read-Execute-Print-Repeat because that’s exactly what it does.

<syntaxhighlight lang="sh">$ python3</syntaxhighlight> You should see something like this now….

Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

In this shell, you can enter anything that would be a valid line of Python. For now, let’s just use it like a calculator.

<syntaxhighlight lang="py">>>> 3 * 9 27 >>> </syntaxhighlight> Mathematical operators to try out in Python

Operator Name Example
+ Addition 2 + 2
Subtraction 3 – 1
* Multiplication 5 * 3
/ Division 5 / 2
% Modulus 5 % 2
// Floor Division 5 // 2
** Exponential 5 ** 2

You can also use parentheses to control precedence

<syntaxhighlight lang="py">>>> (2 + 3) * 3 15</syntaxhighlight> Tip: You can use _ to get the previous result.

<syntaxhighlight lang="py">>>> 3 * 3 9 >>> _ + 3 12</syntaxhighlight>