Saturday 14 January 2017

Learn Python!

The article is for those who have absolutely no experience in programming, but at the same time very much wants to learn the programming language of Python. It covers the basic operations of the codes as well as free online resources for the users to practice and go further into the subject.

IPython

First of all, you need a platform where you can input your codes to see how it works. When it comes to this, there is no better place to do it than IPython. It is an interactive interpreter which offers helps like tab completion, object introspection, and much more. You can either download it or try it online. Here is a link which leads you to an online interface, totally free to use.

Basic Commands

There are a lot of different commands in Python. Here are a few which coders use most frequently.

1. Basic Mathematical Operations
  • Addition―
    Input: print(1 + 2)
    Output: 3
  • Subtraction―
    Input: print(2 - 1)
    Output: 1
  • Multiplication―
    Input: print(2 * 2)
    Output: 4
  • Division―
    Input: print(2 / 2)
    Output: 1
  • Power―
    Input: print(2 ** 3)
    Output: 8
  • Remainder―
    Input: print(9 % 2)
    Output: 1
2. Basic Variables
  • Numbers―
    In[1]: Integer1 = 123
    In[2]: Integer2 = 456
    In[3]: print(Integer1 + Integer2)
    Output: 579
  • Strings―
    In[1]: Word1 = "123"
    In[2]: Word2 = "456"
    In[3]: print(Word1 + Word2)
    Output: "123456"
3. Basic Variable Conversions (Using the variables in the last section)
  • Numbers to String―
    In[1]: Converstion1 = str(Integer1)
    In[2]: Converstion2 = str(Integer2)
    In[3]: print(Converstion1 + Converstion2)
    Output: "123456"
  • String to Number―
    In[1]: Converstion3 = float(Word1)
    In[2]: Converstion4 = float(Word2)
    In[3]: print(Converstion3 + Converstion4)
    Output: 579
  • Rounding to the Nearest Interger
    Input: print(round(5.4))
    Output: 5.0
    Input: print(round(5.5))
    Output: 6.0
Example

In a coin-flipping game, you start with a capital of $100. Then, you have to flip a coin five times. If you get a head, you gain 20% of your existing money. If you get a tail, you lose 10%. Now you get 3 heads and two tails, how much do you get in the end? Let’s round the answer to the nearest integer.

You can solve this problem by a little python code:

In[1]: Gain = 1 + 0.2
In[2]: Loss =  1 - 0.1
In[3]: NumGain = 3
In[4]: NumLoss = 2
In[5]: StartCap = 100
In[6]: Outcome = Gain ** NumGain * Loss ** NumLoss * StartCap
In[7]: OutInt = round(Outcome)
in[8]: print("The final amount that I get is " + str(OutInt))

Paste the code here and you will see the output: "The final amount that I get is 140.0"

Further Study

If you are interested in learning Python, one recommended book is Python for Kids (Briggs, 2012). It guides you through the basics as you experiment with hilarious examples that feature ravenous monsters, secret agents, thieving ravens, and more.

REFERENCE: Briggs, J. R. (2012). Python for Kids: A playful introduction to programming. San Francisco, CA: No Starch Press.

No comments:

Post a Comment