Saturday, April 1, 2017

Start Python programming: explanation for the first program

We saw how server-side scripting using Python is done, but I think you didn't understand details of the code of Python.

The code was like this:
print("<h1>Welcome to my website</h1>")
amount = 100.0
if amount < 100:
  discount = amount * 0.05
elif amount < 500:
  discount = amount * 0.10
else:
  discount = amount * 0.15
print ("<p>Your amount: ",amount,"</p>")
print ("<p>Discount: ",discount, "</p>")
print ("<p>Net payable: ",amount-discount,"</p>")

print("") is, as explained in the last post, used to display something written in-between these --> " ". So print("<h1>Welcome to my website</h1>") works just to display "<h1>Welcome to my website</h1>" in the browser.

"amount = 100.0" is initialization of a variable. "amount" is a variable defined to store a value, which is intended to be used to store how much amount of money is used.  But, before being initialized, variables store initial value like "null", which is called undefined value (that can be considered as a garbage...it can't be used productively). So the "amount" value is initialized with the value "100" in the code, namely, "amount = 100.0". Now the "amount" variable doesn't have an undefined value (garbage) inside...it has "100" inside the variable.

A variable is like a small box. This box can have only one thing (a value) inside but the thing inside always can be exchanged to another thing (another value). 

But there are some types of variables in Python like "Integer", "String" and so on. But you don't need to explicitly write what the type of the variable is in Python.

For example, in Java, you need to explicitly write what the type is for all variables like:
int age = 10;
"int" is indicating the "age" variable is an integer-type-variable. 
But in Python, you can write this way:
age = 10;
"int" doesn't need to be written because Python itself will automatically check what the variable type is, 
which means "variable type doesn't need to be written explicitly".

The next is if-statement. 

if amount < 100:
  discount = amount * 0.05
elif amount < 500:
  discount = amount * 0.10
else:
  discount = amount * 0.15 

The statement says "if amount < 100:", which means "if amount variable is smaller than 100, the following will be executed." And "the following" is "discount = amount * 0.05" according to the statement. 

if amount < 100:
  discount = amount * 0.05

"discount = amount * 0.05" means "Caculate amount * 0.05 at first and then assign the result value to the discount variable". So the discount variable would have a value that is the payment amount discounted with 0.05 (5%). 

elif amount < 500:
  discount = amount * 0.10

"elif" in the if-statement means "else if". This "elif"-part is executed only if the first condition (if amount < 100:) is not satisfied AND if the amount variable is smaller than 500. So if amount is 99, the discount rate is 0.05 (5%) because it satisfies the first condition. But if the amount is more than 100 but less than 500, the discount rate is 0.10 (10%).

else:
  discount = amount * 0.15 

The code under "else"-condition is executed for every condition that doesn't satisfies "if amount < 100:" and "elif amount < 500:", e.g., amount = 600, amount = 700, or amount = 99999999 (every number that doesn't satisfy other conditions above). 

But, for example,

if amount < 100:
  discount = amount * 0.05
elif amount < 500:
  discount = amount * 0.10
else:
  discount = amount * 0.15
elif amount < 600:
  discount = amount * 0.10

in this case, "elif amount < 600:" and the following code will be ignored because "elif amount < 600:" is written after "else:"-condition is written. Codes are executed from upside to downside, so there is nothing left to be checked after "else"-condition is checked. (Every number that doesn't satisfy the first and second is treated under else-condition, so nothing is left for the fourth condition "elif amount < 600:".)