Starting up with Python – Part2

Here I am on the Part2 which will consist of more details whilst I am moving on to more and more video tutorials on Python. Just to remind others about past articles on Python are as follows:

3.Modules and Functions

The principle of using the function in Python looks easier. We write the function name and the parameter that will correspond with the function. Examples of the power modules are as follows:

>> pow (2,2)
>>> pow (2,2.2)
>>> bin (15)

There are many functions in Python such as abs(), hash() bin() etc.. An interesting link to study more function in Python is from the official website depending which Python version you are using.

Some inbuilt functions need to be imported before being used. Example are:

>> import math
>>> math.floor(18.7)

Functions can also be assigned to variables itself.

> math.sqrt(4)
>>> x = math.sqrt
>> x(4)


Use of strings with quotes are important. Usually, you do not have to care if its single or double quote. However, if you have this in your string he’s scary. You will notice that there is already a single quote in the phrase he’s scary. So a double quote can be used as follows “he’s scary” Escape characters can also be used in a phrase that already consists of double quotes. Example “he said: “I am scared” “ should be used as: “he said: \” I am scared\” “ to escape those unwanted quotes in the program.

Strings can also be used jointly. Example a + b where + means concatenating the strings:

>> a = "hackers"
>>> b = "Mauritius"
>>> a + b

 You might also need to concatenate a string with a number. For example, age = 10 and you want to print “your age” + age which will eventually prompt for an error. To solve this issue, we just passed the value 10 to the function str ()

>> age = str(10)
>>> print "your age is " + age
your age is 10

Another way to solve this issue is to use backtick instead of the str function.

>> age2 = 1
>>> print "your cat's age is " + `age2`
your cat's age is 1


Nitin J Mutkawoa

Blogger at | Founding member of | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author