1. Python Console and Mathematical Operators¶
After installation, it is possible to run the IDLE PYthon GUI (CPython) or Spyder (Anaconda) program and interact with the Python Shell or Ipython console. You can perform mathematical operations on the shell. Enter 2 + 5 and press the enter key to get the result
2+5
7
Real numbers must be typed with dots by separating the entire and decimal parts.
2.5 + 5.3
7.8
Subtraction operations can be performed in a similar manner:
5-2
3
2-5
-3
The multiplication operator is the asterisk character.
2*3
6
5*7
35
The power (or exponent) of a number is inputed as two asterisks
2**3
8
3**2
9
Division:¶
The operator / is responsible for the actual division
4/2
2.0
5/2
2.5
Two bars in a row // operate as an entire division. The entire division defines that $5/2$ would have resulted in $= 2$ and the rest $= $1$. The rest of the division can be calculated by the $\%$ operator.
5//2
2
5%2
1
The entire division can be done in whole variables and floating point variables.
5%2
1
5.0//2.0
2.0
5.0 % 2.0
1.0
2. Parêmesis levels¶
You can combine several mathematical operations into an expression in Python:
2+3/2
3.5
You must look at which operation you want to perform:
$\frac{2+3}{2}$
or
$2+\frac{3}{2}$.
The line typed above returns the second option. if the goal is the first it is necessary to use parentheses for the addition operation to be performed first:
(2+3)/2
2.5
A particularly interesting case occurs in the potentiating of negative numbers. The expression typed below corresnponede at $-\sqrt{2}$.
-2**.5
-1.4142135623730951
To perform the $\sqrt{-2}$ operation you must type as it appears below.
(-2)**.5
(8.659560562354934e-17+1.4142135623730951j)
Python 3 returns a complex variable for this oprenation.
3. Variables¶
values can be saved in computer memory for use in later operations. To access these values, you must assign a name to a variable. In Python, to use a smallpox just atrubuir a value to a variable name through the operator $=$.
a = 2
a
2
b = 2+3
b
5
a = True
a
True
b = False
b
False
type(a)
bool
type(b)
bool
3.1.2 Integers¶
a = 2+3
a
5
type(a)
int
4.1.3 Floats¶
b = 3.0
b
3.0
type(b)
float
c = a+b
c
8.0
type(c)
float
3.1.4 Strings¶
c = "hello world"
c
'hello world'
type(c)
str
4. Logic operations¶
Not
vLogic = True
not vLogic
False
vlogic = False
not vlogic
True
AND
a | b | and |
---|---|---|
True | True | True |
False | True | False |
True | False | False |
False | False | False |
True and True
True
False and True
False
True and False
False
False and False
False
OR
a | b | or |
---|---|---|
True | True | True |
False | True | True |
True | False | True |
False | False | False |
True or True
True
False or True
True
True or False
True
False or False
False
Equality
a == a
True
a == b
False
Inequality
a != a
False
a != b
True
Bigger than (>) smaller than (<)
a
5
b
3.0
a < b
False
a > b
True
a < a
False
b < b
False
Bigger or equal (>=) smaller o equal (<=)
a <= a
True
a <= 5.0
True
5.Logic onditions¶
if
else
elif
var1 = 12
if var1 % 2 == 0:
print(var1, " Even")
else:
print(var1, " Odds")
12 Even
var2 = 7
if var1 % 2 == 0:
print(var1, " Even")
else:
print(var1, " Odds")
12 Even
var3 = 12
if var3 % 2 == 0:
print (var3, " is divisible by 2")
if var3 % 3 == 0:
print (var3, " is divisible by 2 , 3 and 6")
elif var3 % 3 == 0:
print (var3, " is divisible by 3 and NOT by 2")
else:
print (var3, "is NOT divisible by 2 , 3 nether by 6")
12 is divisible by 2 12 is divisible by 2 , 3 and 6