Python Tutorial 03 : basic arithmetic operator and create the first Python Function

From Python Tutorial 01, we have installed the Spyder application. Spyder application is the IDE for Python.

Now let us to start creating the first application.

 

But before hand, let us go through the basic arithmetic operator that Python use. Some of them appears only available in Python.

 

[vtftable ]
Operator;;;Example;;;Result;;;Meaning;nn;
+;;;43+3;;;46;;;addition;nn;
-;;;43-3;;;40;;;subtraction;nn;
*;;;43 * 3;;;129;;;Multiplication;nn;
/;;;43/3;;;14.3333;;;Divide;nn;
**;;;43 ** 3;;;79507;;;43 to power 3;nn;
//;;;43//3;;;14;;;Same is divide, but only take the integer part and disregard the remainder;nn;
%;;;43%3;;;1;;;Remainder value;nn;
[/vtftable]

 

Basic Arithmetic Operators in Python

Our first Python Function

Okay! Let’s try to create the first Python Function – area_triangle.

Area_Triangle is a function that calculate the area of a triangle. It accepts to input arguments:

  • Base &
  • Height

Following is the formula for calculating the area size of a triangle:

0.5 * Base * Height

 

Following is the steps:

  1. New a Python document in Spyder.New Python File
  2. Spyder will create a new file automatically.
    Blank new Python fileYou will find that the first 7 lines have something by default. The first link is the code setting and the 2nd to the 7th line is the document info.We will have look into the this in later tutorial. Now, let’s focus on creating new functions
  3. Following is my function
    My first Python function - area_triangle

Explanation:

Line 1 – 7:

Was generated by Spyder. So leave it at the moment

Line 8:

“#%%” – This is a cell separator. It is a IPython function. IPython allow us to divide the entire program into different cells using “#%%” function. A cell simply  a session on the file.

An Python file and have many cells and they are separated by the “#%%” operator.

 

Line 10:

This is where my new function is

  • “Def” Statement is to define the function Area_triangle that accept two variables “Base” and “Height” and calculate the area.
  • All indented statements immediate go under the Def Statement will be considered as a statement belongs to the function.This program is adding 4 spaces at the beginning of each line as indentation instead of using a tab. This is because using a tab may cause some issue to Python.

Line 11 – Line 20 : 

“”” <documentation > “””

I have documented this function by writing down what is this function for, how does it work etc. It is mainly for our own future references.

 

Line 21 – 23:

Calculate the area and using the print function to print the results.

Note: Python variable name is case sensitive. This means a variable “Area” is separate from the variable “area”.

 

Run the new function

Now we have finished writing the function. Following is the steps to run the function.

  1. Right click the cell and click “Run cell” from the menuRun first Python program
  2. This will load the program in the Python automatically.
  3. We will see it from the IPython consoleIPython Console - new function loaded
  4. To run the function, type “area_triangle(<base>,<height>” in the Console Following image is an exampleRun my first Python Function

Let’s try to see the help function for this newly created function.

  1. Click the cell again.
  2. Press <Ctrl +I> from the keyboard

OR

  1. Go to the help windowPython help window
  2. Change the source to “Console” and pick the object from the drop-down list.
    Change python help source to "Console"
    Change python help source to “Console”

    Pick the object from the drop down list - Python Help window
    Pick the object from the drop down list
  3. Python automatically populated information regarding to the new function, including input arguments and the documents I typed in between the two  ” “”” ” in line 11 and line 20
    Help document for my new Python function

Leave a Comment