Python has two type of numbers. First of all, there is the integer. An integer, is an element of the exact same integers number set is in standart mathematics. For example, -5, 4, 164 and -2780 are all integers.
integer = 2
integer2 = 4
type(integer) # NOTE: the type() function displays the type of the function as an output.
The output of type(integer) is always int  or  <class 'int'>Â
All decimal point and rational numbers which aren't whole numbers are floats or floating point numbers in Python. Floats are shown with decimal points (.) rather than with divsion symbols (for example: 3.5 is a floating point number where as 7/2 is a mathematical operation).
float = 2.5
float2 = 7.205
type(float) # This will ouput <class 'float'> or float
Irrational numbers such as π (Pi) and e that can have their digits after the decimal point approximated are considered floating point numbers.
Python has several mathematical operators which allow us to make the same computations as in normal mathematics. It's very easy to complete addition, subtraction, multiplication and division in Python, as the majority of the operation symbols are the same or similar in both Math and Python.
1) Addition Operation Symbol: +
addition = 2 + 2
print(addition)
The output:
4
2) Subtraction Operation Symbol: -
subtraction = 4 - 2
print(subtraction)
The output:
0
3) Multiplication Operation Symbol: *
multiplication = 2 * 2
print(multiplication)
The output:
4
4) Division Operation Symbol: /
division = 4 / 2
print(division)
The output:
2.0 #note: divisions will always output floating point numbers.
Exponents are set using two asterisks (**)
# a to the power of b: a ** b
power = 2 ** 4
# the output will be: 16
The order of operations in Python is that of standart mathematics and is the PEMDAS order.
Python has a function called the modulo, which allows for you to receive the remainder of a division. This function works with the percentage symbol (%) as shown below.
modulo = 10 % 3
print(modulo)
If we were to calculate, we know that 3 goes into 10 three times as 9. Then, the remainder is 1, which is what will be printed.
Output:
1
In cases with no remainders, the modulo will of course give back 0.
modulo = 4 % 2
print(modulo)
Output:
0
Apart from modulo, working with randomization is another important aspect in Python programming. In order to do this, you must first insert the random module at the top of the file in which you will be writing your code in.
import random # write this at the top of your file
With the random module imported, you can complete several actions which will require you to generate a random number, and this is closely related to probability in mathematics (direct yourself to the Math and Python section for more details regarding the relationship between math and Python).Â
Generating a random integer:
random_number = random.randint(0, 10) # generates a random number from 0 to 10 (including 0 and 10)
print(random_number)
Generating a float number between 0 and 1 (not including 1):
random_float = random.random()
print(random_float)
To generate a random floating point number using random.random() that is larger than the maximum value of 1, this value can be multiplied as below:
random_float2 = random.random() * 10
print(random_float2)
random.uniform can be used to generate a random floating point number between two set values. These values can be above 1.
random_float = random.uniform(0, 10)
print(random_float)
You got a summer job to work at your local fastfood restaurant. Here, you tell your boss that you're good at both math and programming. Therefore, he wishes you to create a system for the restaurant that will:
a) Calculate all total items sold for a day.
b) Calculate the per capita of all times sold for a day. (Per capita = divided by the no. of customers)
c) Calculate the spending of a single customer.
d) Calculate which gender spent more money and by how much.
To make this system, he gives you the statistics of the customers that came in today, which is as follows:
Veronica (F): 3 burgers, 2 fries, 2 sodas, 1 smoothie, 1 soft serve
James (M): 3 burgers, 5 fries, 4 sodas, 1 smoothie, 3 soft serves, 2 apple pies
Noah (M): 1 apple pie, 4 fries, 3 burgers, 1 soda, 3 smoothies
Madison (F): 2 soft serves, 2 fries, 1 soda, 1 smoothie
Shawn (M): 4 burgers, 5 fries, 4 soft serves, 2 sodas, 2 smoothies
The prices of the items are as follows:
Burger: 8 Dollars
Fries: 3 Dollars
Soda: 1.5 Dollars
Smoothie: 1.5 Dollars
Soft Serve: 2 Dollars
Apple Pie: 6 Dollars
Your boss advises you calculate the individual spending of Madison for criteria C of the system.
Write the code that will be able to calculate all of the given criteria within the system according to the data.
Please type all code on your preferred code editor
Step 1: Calculate the total number of all of the items.
Step 2: Create the appropriate variables for each of the items.
Step 3: Assign all of the variables created in step 2 with all of the corresponding values calculated in step 1.
Step 4: Create a variable called or similar to "total_sold" and assign it the sum of the values of all of the variables previously made.
Step 5: Create a variable that corresponds to the per capita items solds and assign it the value of the total sold calculated in step 4 divided by the number of customers.
Step 6: Create a variable corresponding to Madison's spending and calculate her spendings according to the data given.
Step 7: Create two different variable which correspond to the spendings of the two genders (male and female) and subtract the larger value from the smaller one to see the which gender spent more.
Please note that this is just a suggested answer and any code which completes the same function is valid
burger = 13
fries = 18
coke = 10
sprite = 8
soft_serve = 10
apple_pie = 3
customers = 5
total_sold = burger + fries + coke + sprite + soft_serve + apple_pie
print(total_sold)
average_sale = total_sold / customers
print(average_sale)
madison_order = 2 * 2 + 2 * 3 + 1.5 + 1.5
print(madison_order)
male_revenue = 10 * 8 + 14 * 3 + 7 * 2 + 7 * 1.5 + 6 * 1.5 + 3 * 6
female_revenue = madison_order + 3 * 8 + 2 * 3 + 2 * 1.5 + 1.5 + 2
print(male_revenue - female_revenue)
You have reached the end of Section 2: Math and Numbers!