In Day 02, you introduced yourself with print and simple variable. You can write as much as you can to tell the world about you. The key thing is to use variable to reuse it every time you need. More in the future I don’t know when, the lesson perhaps is more complicated with the use of variable I guess. Don’t mind that! This is not the time to look into complex stuffs.
In this post, I’m going to introduce math in Python, and will do some maths with you. First, you don’t need to understand why interpreter works to calculate your math. What you need in this post is to memorize math symbols which are supported in Python, like any of programming languages. Look at the below list:
- + plus
- - minus
- / slash
- * asterisk
- % percent
- < less-than
- > greater-than
- <= less-than-equal
- >= greater-than-equal
It’s not new to you if you are familiar with general math. Let’s practice just a little bit by telling the world that you have two bank accounts. The first account has 10,000 USD and the other has 7,500 USD. Now let’s tell the world the total amount of money you have.
You can quickly write something like
print 10000 + 7500
That’s not wrong but you are required to tell the world about the amount in each account. Remember the variable you used in the previous lesson.
acc_1st = 10000
acc_2nd = 7500
print acc_1st + acc_2nd
When you need to use the value you declare in your variable, make sure you don’t use quotation marks. If the output is 17500 then you are done the math with Python. Just practice a little more and pray to see the sentence “Totally I have 17500 USD” from the output.
acc_1st = 10000
acc_2nd = 7500
print "Totally I have", acc_1st + acc_2nd, "USD"
What about % (percent)? Is that the percentage you often see in math books, for example: I’ve finished 50% of my assignment. It means if the total exercises in my assignment is 20 so I’ve finished 10 exercises. Alright % (percent) in Python is not that percentage. It is just the remaining value when you divide among numbers. For example the remaining value of 5 divide by 2 is 1. If I give 5 pencils to 2 people equally, then I have 1 pencil remaining. Let’s practice this case that I have 10,000 pencils. I need to give them equally to 74 people. After then how many remaining pencils do I have? Make sure each person has the same number of pencils.
no_of_pencil = 10000
no_of_people = 74
print no_of_pencil % no_of_people
If the result is 10 then you are done. Each person shall have 135 pencils and the remaining is 10. Test by multiplying 135 by 74 then plus 10, with Python and see the result.
print 74 * 135 + 10
More advance, what is the output if I use the following code:
no_of_pencil = 10000
no_of_people = 74
print 10 - no_of_pencil % no_of_people
Or below:
no_of_pencil = 10000
no_of_people = 74
own = 10
print own - no_of_pencil % no_of_people
If you guess correctly then you are done the basic math in Python. Now have a look at each result of the two below code snippers:
print 100 > 99
and
print 100 < 99
The first result is True and the second result is False. The interpreter just concludes whether your statement is correct. Obviously 100 is greater than 99 then the interpreter tells that you are correct (True). Vice versa, when you tell the interpreter that 100 is less than 99, it tells you that you are wrong (False). The use of less-than-equal and greater-than-equal are similar. Let’s practice just a little bit more:
a = 100
b = 90
print "Is a greater than b?",a > b
The output shall enlighten more about the use of comparison. Can you guess output of the following code snippet:
no_of_pencil = 10000
no_of_people = 74
own = 10
print own >= no_of_pencil % no_of_people
If you are correct I believe you are not familiar with the use of math and basic variable in Python.
If you like to type more Python code, here is what I extracted from the beginning of the lesson 04. Before executing the code, guess in your brain what the output will be.
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."
You might be curious the following lines of code:
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven</span>
Don’t worry. It’s just a math. You declare that you have 100 cars and only have 30 drivers. It means the number of cars that cannot be driven is 70. That’s how you do the math for
cars_not_driven = cars - drivers
Of course you can write
cars_not_driven = 70
However in programming people tend to flexibly use variables and maths as much as possible because they want the interpreter (computer) to help them calculate. Moreover, they will call (reuse) the value later. We will look into such a case in the future.
The lesson in Day 03 is finished now.