open your computer's command line / terminal
and enter python --version to check that python is installed and working on your computer
Microsoft Windows [Version 10.0.22631.3447]
(c) Microsoft Corporation. All rights reserved.
C:\Users\yonad>python --version
Python 3.11.3
C:\Users\yonad>
checkout this guide on how to open python from any computer
you can follow this video guide if you run into issues
now that we know you have python ready, let's activate your python program by entering python
in the command line.
you will notice 3 arrows >>>
when the program is running
Microsoft Windows [Version 10.0.22631.3447]
(c) Microsoft Corporation. All rights reserved.
C:\Users\yonad>python
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
now let's test python's math capabilities, enter 5 + 5
>>> 5 + 5
Python sees our two numbers and a +
operator, it evaluates this to 10 and print the result:
>>> 5 + 5
10
>>>
What we've just witnessed is the python PERL
The Python REPL is an interactive shell that allows you to enter Python commands and directly see the results. Using the interactive shell is a great way to get your hands dirty and learn python fast because the shell will immediately give you feedback.
>>> 5 + 5
10
>>>
REPL is short for Read-Evaluate-Print-Loop:
Python is pretty good at doing math.
And it's super easy, just like in any calculator.
But python can do a heck of a lot more than just summarizing 2 numbers:
Operator | Name | Example |
---|---|---|
+ | Addition | 1 + 2 + 3 + 4 |
– | Subtraction | 50 - 55.55 |
* | Multiplication | 4 * 4 * 4 |
/ | Division | 9 / 3 |
% | Modulo | 7 % 2 |
// | Floor division | 11 // 2 |
** | Exponential | 2 ** 8 |
_ | Previous Result | 1 + 2 _ * 2 |
>>> 1 + 2 + 3 + 4
???
>>> 50 - 55.55
???
>>> 4 * 4 * 4
???
>>> 9 / 3
???
>>> 7 % 2
???
>>> 11 // 2
???
>>> 2 ** 8
???
>>>'run these yourself to get the results!'
The order in which Python processes the operators and numbers is similar to regular math.
Multiplication and division come before addition and subtraction.
Multiplication and division have the same precedence, so the order matters. Like in math, we work from left to right: 4 * 4 / 10 = 1.6
while 4 / 4 * 10 = 10.0
In order to avoid silly mistakes you can use parentheses to be sure the order is correct:
>>> 3 + 3 * 3
12
>>> (1 + 2 + 3) * 4
24
>>> 10 + 10 ** 2
110
>>> 2 / 2 * 3
3.0
>>>
Python keeps a history of your commands. You can go back and forth between previous commands by pressing up and down. Python usually keeps this history under ~/.python_history
, so it even persists between sessions.
We just did some serious math with Python, But it would be even more awesome if we could save the results of our calculations and re-use them. Python allows us to define variables that can remember our results just for that.
Next: Variables