Deep Dive in Machine Learning with Python

Part — III: Python essentials

Rajesh Sharma
Analytics Vidhya
7 min readOct 13, 2019

--

Welcome to the third blog of Deep Dive in Machine Learning with Python, I believe you are now comfortable with Jupyter notebook and its console. If you need a refresher on Jupyter notebook, then kindly browse my prior blog (Deep Dive in ML with Python — Part-II) where I have demonstrated how to efficiently use it for coding purposes.

As in this blogging series of ML, the primary focus will be towards practical implementation in python. Hence, in today’s blog, we will run some fascinating python code and try to understand its objects by doing several problems.

Problem-1: How to create a python object(variable) with some value?

Solution-1

In the first line of the above example, we created a python variable player and assigned value Sachin Tendulkar to it. In the next line, we just print the player variable.

Problem-2: How to identify the category or class of a variable?

Solution-2

Here, we used type function to identify the class of the player variable. So, str means that the player is a string variable.

Problem-3: How to find the total length of a variable?

Solution-3

By using the power of len(aka length) function we were able to calculate the length(aka count of total characters) of the variable player.

Problem-4.1: How to concatenate 2 string variables?

Solution-4.1.1

In the above example, we created two string objects str1 and str2. Here, str1 contains ‘Hello James, how are you? ’ and str2 holds ‘I’m good. How about you?’ We concatenated these two objects by using + operator and stored the result into a new string object concat_str.

Then, in the next cell, we just print the concat_str.

Operator + does not append space while concatenating the string objects.

(look at the example below)

Solution:4.1.2

Here, in the output, we didn’t get the space after the first question mark(i.e. before I’m).

Problem-4.2: Identify the type of concat_str variable

Solution:4.2

Similar to problem-2, here, we also used the type function to identify the class of concat_str, however, enclosed within a print statement which provides the output as <class ‘str’>(means concat_str belongs to string class).

Indexing in python

Problem-5: How to access characters of a string variable via index?

Solution-5

In the above example, we created a string variable name with value Python. The length of the name variable is 6 that means its index lies from 0 to 5(0th position index stores the first character P, 1st position index stores the second character y and so on).

Problem-6: How to create a sequence of numbers from 0 to the length of a variable?

Solution-6

In the first cell of the above example, we used the range function to define a starting and ending point of a range. Then, in the second cell, we ran a for loop to print the numbers that come in this range.

Problem-7: How to obtain an element or character of a python string along with its index?

Solution-7

This is an easy one and similar to the previous problem, however, here we also used the print statement to display the element of a variable at a particular index position.

Problem-8: How to fetch the first 5 characters of a string?

Solution-8.1

For the above example, the concat_str variable has been used and [0:5] means display the elements of indexes 0, 1, 2, 3 and 4. Thus, Hello appeared as an output(refer to solution:4.1.2 if you want to see what concat_str contains)

When we access the index of a string like [0:5], then, number after : is not included in the index range.

Refer to another below example:

Solution-8.1.2

Here, new_val contains a string and in the second cell, the first 10 elements of that string have been fetched.

Index allocation in new_val

As an output, we got This is ne because only the first 10 index elements(means from 0 to 9) been accessed.

Problem-9: How to obtain the last 4 characters of a string?

Solution-9

In step-1, we print the string of concat_str in reverse order(means from the end). Then, in step-2, we just took the first 4 elements based upon the index and at last step, we again reversed the order on the first 4 fetched elements.

Problem-10: How to find the index of a word from a string?

Solution-10

In the above example, the find method returns an index position of the word ‘how’ in concat_str.

Problem-11: How to replace a word from a string?

Solution-11

Here, we used the replace method to replace James in concat_str with JOJO.

Problem-12: How to convert the string in capitalized form?

Solution-12

This is fairly an easy example, here, instead of assign a string value to any variable, the str function is used to define a string. And, the capitalize() method is used to return a capitalized version of the same string.

Problem-13: How to obtain a string suitable for caseless comparisons?

Solution-13

In this example, a new approach has been used to define a string by using single and double quotes(aka ‘’ or “”). And, the casefold() method returns a string suitable for caseless comparisons.

Problem-14: How to count the number of occurrences of a specific word in a string?

Solution-14

In the above example, the count method has been used to find out the total number of occurrences of ? in concat_str.

Problem-15: How to convert a string as a centered string?

Solution-15

Here, 25 represents the desired width of a new string with David JOJO in the center of a string.

Problem-16: How to print every character of a string in upper case?

Solution-16

In the above example, a for loop will iterate on every character of player string. And, Upper() method will convert every character in uppercase.

“+” is used to add an extra newline(“\n”) with every character.

Problem-17: How to verify whether the string is of digit type?

Solution-17

In this example, False means that ‘Digit 123’ is not a digit type. Refer to few below examples as well:

Solution-17.1

Problem-18: How to get the user input at runtime?

Step-1: Use input() function to accept the user input at runtime(refer to the below image):

Solution-18.1

Step-2: User writes its input in the text box(refer to the below image):

Solution-18.2

Step-2: Once the user is finished with its input, the output gets displayed(refer to the below image):

Solution-18.3

Hurray, we come to the end of this blog, to summarize, we covered how to work with python strings by solving some problems.

If you want to download the Jupyter Notebook of this blog, then kindly access below GitHub repository:

https://github.com/Rajesh-ML-Engg/Deep_Dive_in_ML_Python

In the next blog, we continue to work with other objects like Lists so stay tuned…..

Thank you and happy learning!!!!

Blog4: Guide to Python Lists

--

--

Rajesh Sharma
Analytics Vidhya

It can be messy, it can be unstructured but it always speaks, we only need to understand its language!!