Learning Python – Comments and Math in your Apps!

January 9, 2017 at 10:32 pm

In this post we will examine two great features we can begin using in our amazing Python applications. The first is the ability to use the pound sign (#) in order to add comments to our code.

This of course makes our code more “self documenting”. This is a fancy term for us being able to figure out what the hell the code is supposed to do if we wrote it a long time ago and forgot! It is also nice to have this done in the event we ship our code off to a peer that needs to understand what we are up to.

# This is an entire line of code that is not going to run in this application - it is just here for our documentation purposes.
 # Anything after the # is ignored by Python!
 print "Here is the print command at work that we used in an earlier lesson."
print "And here is another cool way to use it!" # This will not print even though it is in the same line of code!

Let me save this masterpiece as ex2.ps and run it! Today I am using the Python download for Windows because I cannot find my MacBook! 🙂

C:\Users\terry> python C:\Users\terry\OneDrive\Blog\Python\ex2.ps
Here is the print command at work that we used in an earlier lesson.
And here is another cool way to use it!

C:\Users\terry>

Now let’s turn our attention to math – here are the math operators we have at our disposal!

+     plus
-     minus
/     slash
*     asterisk
%     percent
<     less-than
>     greater-than
<=   less-than-equal
>=   greater-than-equal

Now it is time for me to create ex3.ps and have some fun with math!

print "This is how much I like to tip when the service in the US at a restaurant is poor and the tab is $100 - ", 100 * %15
print "If the service is good - ", 100 * %18
print "If the service is awesome - ", 100 * %20
print "My age is shown below!" 
print 40 + 7

Now it is time to run it!

C:\Users\terry>python C:\Users\terry\OneDrive\Blog\Python\ex3.ps
This is how much I like to tip when the service in the US at a restaurant is poor and the tab is $100 - 15.0
If the service is good - 18.0
If the service is awesome - 20.0
My age is shown below!
47

C:\Users\terry>
I hope you will return for some more Python fun soon!