Learning Python is fun

Welcome to ‘learning python is fun - for beginners’

Previous: Installing Python

Next: Variables

Running Python

Lesson: 3

open your computer's command line / terminal
and enter python --version to check that python is installed and working on your computer

> _
command Prompt
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

> _
command Prompt
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

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:

  • Read: Python reads 5 + 5
  • Evaluate: Python evaluates this input and decides it is a summation operation of 2 numbers
  • Print: it prints out what was evaluated
  • Loop: it’s ready for the following input

Math Operators

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:

OperatorNameExample
+Addition1 + 2 + 3 + 4
Subtraction50 - 55.55
*Multiplication4 * 4 * 4
/Division9 / 3
%Modulo7 % 2
//Floor division11 // 2
**Exponential2 ** 8
_Previous Result1 + 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
>>>

Commands History

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.

click to run python

Lesson Completed 👏

Great job! 🍺🍺“🎉“🎉

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.

Previous: Installing Python

Next: Variables