Python : Crash Course Part 12 – Methods

The most common and widely used feature of python programming for data science is methods. You will see the usage of methods quite often while you dive further into data science with python.

Lets go ahead and create a string :

In [1]: s=' hello! Welcome To Python Crash Course'
In [2]: s.lower()
Out[3]: 'hello! welcome to python crash course'

It’s so simple isn’t it ? The lower method on a string will convert all the characters to lower case. You can try the s.upper() method and see if the method changes your string to upper case. There are several methods of a string which can be found by pressing tab in jupyter notebook after the dot symbol ( s. ), you can try out various methods available.

Lets go over the split method.

In[4] : s.split()
Out[5]: ['hello!', 'Welcome', 'To', 'Python', 'Crash', 'Course']

The split functions splits the strings on the white space. If you need to split the dataset based out of ‘!’ then you can pass the ! mark within quotes and within the split function. The method will split the string where it finds the ! mark.

In [6]: s.split('!')
Out[7]: ['hello', 'Welcome To Python Crash Course']

You can try and use methods on strings, dictionaries and lists. Lets try a method on a list.

In [8]: list=[1,2,3]
In [9]: list.pop()
Out[10]: 3

The pop method removes the last component/element from the list permanently. If there is a need to pop a particular item of your choice, you have to target the items index and pass the value within the pop functional brackets.

In [11]: list
Out[12]: [1,2]

Here we come to an end of the python crash course for data science. These 12 modules / blog posts should help any beginner ( like me ) to gain fair bit of knowledge on python as we move along more complex codes within data science.

Leave a comment if you really enjoyed this crash course series.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s