Arithmetic Operators
Regarding the operators we can use in Python, we have the following.
- Addition
- 4+3 = 7
- Subtraction
- 4-3 = 1
- Multiplication
- 4*3 = 12
- Division
- 4/3 = 1.3333333
- Exponentiation
- 4**3 =64
- Floor division
- 4//3 =1
- Remainder of division
- 4%3 =1
The order of precedence is
- What's between
() ***,/,//,%whichever appears first+,-whichever appears first
Opening the console let's do some tests
>>> 5+4**2
21
>>> 18//2*(5+2)*2
126
>>> 18//2*(5+2)*2+4
130
>>> 567**333
87732117446070003715053516180852867561733510490298952637287497431563317374084143995465490560216107876808702873487868667862271552903064662596725150619327746079148174413688662023190539128122361705889382443530458156182022076736458993036481642794482475236289311330701329291855996885404789485786551483458396449973434920640205792038734126774527885373180925993193311014948558974951069075316968365528706238117357359458909312301720409726208656326771195846753582287854877372147618520442083048255420664761304358432564719724043959812977568801553053999616645045035274573820396436155101285704381990481895392992363684440625177127180036028294946917143399860454006028454837777640777962785150665504496193057912004099984540159030535471388553223000248429007559122198684670105604831537145651461714172578435020765490665307159386361128416692210916174057769498026339495380358225940846121826164215336097323274422272026998129778089404958995287
>>> 347.04**33.7
4.081222933285245e+85
>>> 49**(1/2) # this is a square root
7.0
>>> 49**(1/3) # cubic root
3.6593057100229713
>>> pow(5,2) # Python built-in function that calculates power
25
In Python, the limit of a number is the size of the memory, which is why it's recognized in the scientific computing field.
There are other ways to do mathematical calculations in Python using the math library, but we'll see that later.
Exercise 1​
Read an integer number and show its predecessor and successor. The solution is in antecessor_sucessor.py
Exercise 2​
Read a number and show its double, triple, and square root. The solution is in dobro_triplo_raiz.py
Exercise 3​
Read two grades from a student and calculate their average. Pay attention to the order of precedence. The solution is in media.py
Exercise 4​
Read a value in meters and convert it to centimeters and millimeters. I want to be able to read 2.3 meters for example. The solution is in converter.py
Exercise 5​
Read an integer number and print its multiplication table in the best way you can.
Tip:
/nline break The solution is in tabuada.py
Exercise 6​
Read the value of how much 1 dollar costs at the moment and another value in reais that you have and see how many dollars you can buy. As the first challenge and leaving your comfort zone, if you can, try to use only two decimal places. Search on the internet for the solution before seeing the result. The solution is in dollar.py
Exercise 7​
Knowing that 1 liter of paint covers 2 square meters, read the height and width of a wall and tell me how many liters of paint I'll need. The solution is in tinta.py
Exercise 8​
Read the price of a product and show its value with an 8% discount. The solution is in desconto.py
Exercise 9​
Read a person's salary and show it with a 17% increase. The solution is in salario.py