Python is one of the most ubiquitous coding languages in the world.
It's known for being
beginner-friendly and is used in a lot of different software and scripts.
In Chapter 1, we'll have to learn the basics of Python. We'll, of course, need to have an
interactive shell (Visual Code Studio or other) to help us out with trying it out.
With some practice, we should be able to get the gist of what Python is going to be like.
We'll be learning how to give Python some basic instructions to follow along and something
we'll be working on as we go through the book.
Entering Expressions into the Interactive Shell
website
In the first example, we'll be giving the simple calculation of 2 + 2 , then once you enter it, you
can run it and it should give the output of 4 .
2 + 2 in Python is called an expression. Like with most basic kinds of programming
instructions in the language. The values in the expression are 2 and the operators are + .
Expressions with values and operators can evaluate (or as the book says, reduce) down to a
single value, that being 4 .
Errors are okay!
If there is code that the computer cannot understand, the program will crash, in which, the
computer will then give us an error code. We refer to this event as error handling. An error in
code doesn't break your computer, it just crashes (which means the program stopped
unexpectedly) and give us a message about it.
The link in the blurb will help us know what went wrong with the code.
Table of Operators
Operator Operation Example Evaluates to...
** Exponent 2 ** 3 8
% Modulus/remainder 22 % 8 6
Operator Operation Example Evaluates to...
// Integer division/floored quotient 22 // 8 2
/ Division 22 / 8 2.75
* Multiplication 3*5 15
- Subtraction 5-2 3
+ Addition 2+2 4
The order of operations (also called precedence) of Python math operators is similar to that
of mathematics
The ** operator is evaluated first; then asterisk, /, //, and % operators are evaluated next, from
left to right. So, just rememeber PEMDAS, without the P, apparently. You can use the
parentheses to override the usual precedence if you need to.
There are some examples of this at work here and with some explanation (because I'm terrible
at math).
>>>2 + 3 * 6 20`
In this operation, you'll notice that 3 times 6 will be calculated first and then + 2 and then we'll
get 20. However...
>>>(2 + 3) * 6
30
With the parenthesis, we'll be able to calculate 2 + 3 = 5 6 which would equal 30.
The parenthesis will able to calculate whatever is in it first* and then calculate outside of the
parenthesis.
So, if I were to do it like this...
>>>2 + (3 * 6) 20`
Just like our first equation. I may have to add something to this later on.
So, Python would do the hard work for us by putting it down to a single value. Here's an
example of how that would work:
( 5 - 1 ) * (( 7 + 1 ) / ( 3 - 1 ))
4 * (( 7 + 1 ) / ( 3 - 1 ))
4 * ( 8 ) / ( 3 - 1 )
4 * ( 8 ) / ( 2 )
4 * 4.0
16.0
But actuality, it would look like this
>>>(5 - 1) * ((7 + 1) / (3 - 1))
16.0
You'll notice that it 16.0 would be our single value and we wouldn't have to take a look at how
Python would do it (probably because it would have already done the hard work for us and I can
trust a computer to do the math for me.)
These rules for putting operators and values together to form expressions are a
fundamental part of Python as a programming language, just like the grammar rules that
help us communicate.
Here's that at work (I actually copy paste these lines)
>>> 5 +
File "<stdin>", line 1
5 +
^
SyntaxError: invalid syntax
>>> 42 + 5 + * 2
File "<stdin>", line 1
42 + 5 + * 2
^
SyntaxError: invalid syntax
You'll notice that even Python would even point out what's wrong with the syntax. That's pretty
cool!
The Integer, Floating-Point, and String Data Types
Remember that expression are just values combined with operators, and they always
evaluate down to a single value.
Let's get into what data types are:
The data type is a category for values, and every value belongs to exactly one data type.
Data Type Examples
Integers -3.-2.-1.0,1,2,3
Floating-point numbers -3.5,-2.1,1.2
Strings 'a','mary','Hello!'
Key tip:
Integers are just numbers, floating-points are decimals, and Strings have letters
Integers = Numbers,
Floating = Decimals,
Strings = Letters