Introduction to Python Data Types - Part-2

Introduction to Python Data Types - Part-2

After having discussed numeric and strings data types in my previous part of this article series on data types in python, let’s now move on to another important data type in python which is sequence data type.

Sequence Data Type:

Sequence python data type is used to store sequential data. Python generates sequences or indexes for the elements stored in this data type. The indexing in python for any sequence data type begins from zero(0); That’s why we can also call this an ordered collection of objects.

I will explain this further in a minute with examples.

Image by author

I’ve already covered string in my part-1 s a separate data type with some basic operations – I will now cover this as a sequence data type which will focus on indexing and slicing along with some more other operations performed on string data type.

Let’s now first understand these sequence data types one by one along with some basic operations on them – I will use Jupyter Notebook for my code.

String:

Strings are ordered characters type of elements that can be accessed using squared brackets [] – we can access individual elements from a string as well as extract a portion from a whole string if needed. Let’s see this here.

Example:

Image by Author

Output:


Image by Author

Now let’s understand how python assigns indices to my above mentioned string 'India’. See this in below table.


Image by Author

If you look closely at this table, It’s clearly visible that there are 5 characters in my string ‘INDIA’ So python has assigned an index to each character beginning from 0; thus my last string character ‘A’ receives a 4 index number.

We can now access these individual elements of my string using squared brackets [] - this is also called indexing, let’s see this here.

Indexing:

Coding Image by Author
Coding Image by Author

Output:


Image by Author

This can also be written like this as well.

Coding Image by Author

Output:

Image by Author

So we now understand that how the individual elements of a string can be extracted. But what if we need to extract a portion of the string. let’s say we want to extract first 2 ‘IN’ or first 3 ‘IND’ characters of the string. we can achieve this by giving a range of indexes in [] – we use colon (:) for this purpose. This is called slicing operation. See this here.

Slicing:

Coding Image by Author

Output:

Image by Author

One can easily observe here that we wanted to extract first 2 elements in our code, however it has extracted only 1 character from my string – it’s because the upper bound (the last number) is excluded in slicing operation. So I will have to mention [0:2] instead of [0:1] in order to extract my first 2 elements.

Coding Image by Author

Output:

Image by Author

It’s now working fine with 2 being as my upper bound. Similarly we can achieve other results as shown in below code.

Coding Image by Author

Output:

Image by Author

In order to extract the complete string, we don’t need to provide any value as upper and lower bound; This can be achieved simply by just providing double semicolon in squared brackets – like this [::] – python will automatically take the lower bound as 0 and the upper bund as the last character’s index +1 as you can see the in my above image of code.

Finding the index of a chercter in a string:

We’ve seen so far how we can extract individual elements and portion of a string, We can also find the their index or sequence. We can achieve this by using find() function. We just need to put a dot after the string and give the character as an input to find its index. Let’s see this here.

Image by Author

Output:

Image by Author

Counting the number of Repetition of a Character:

We can also count the number of times a character has appeared in a string. We can use count() function for this purpose. Let’s see this here.

Image by Author

Output:

Image by Author

Finding a Substring in a String:

We can also find weather a substring is available in a string or not. We can use ‘in’ operator for this purpose. This will be a True or False result in python – as this will check for you if a particular string is present or not. If that is present, the output will be True otherwise It's always False. See this here.

Coding Image by Author

Output:

Image by Author

Replacing a Substring:

A substring can also be replaced by using replace() function simply. Let’s see this here.

Coding Image by Author

Output:

Image by Author

Converting a String into Upper and Lower and Proper Cases:

This is very simple task to perform here, as the name suggests, we can use these 3 functions upper() and lower() and title() to achieve these results. See this here. I’ve also added here swap-case() function – this changes upper to lower and lower to upper cases.

Coding Image by Author

Output:

Image by author

Finding total Length and Removing Whitespaces:

So we have len() function at our service to count the length of a string – which means how many characters including spaces a given string consists. We can also remove leading ( in beginning) and trailing ( in the end) spaces form a string if thee is any by using strip() function. See this below.

Coding Image by Author

Output:

Image by Author

I think this is enough for string data type as of now – there are many more other operations that we can preform with a string data type. I’ll discuss them later. Let’s now move on to another data type which is tuple as of now – I’ll hold list for sometime here.

Tuple Data-type:

Tuple can hold multiple elements of different data types. It can contain character, numeric, complex etc., all kinds data can be held in a tuple.

How to Create a Tuple:

We use parentheses () for creating a tuple and hold the elements inside a tuple. We use comma (,) to separate elements from each other in a tuple. See this here in below example.

Coding Image by Author

Output:

Image by Author

You can see here that we have character data and numeric data and complex data as well in this tuple.

Few Operations on Tuples Data-type:

We can access individual elements from a tuple as well like we've seen above in case of a string data-type, multiple elements can also be accesses and stored in a separate variables if required. You can also check length of tuple like we did in string data type as well, and many more other operations can be performed on tuple data type. Let’s see few of them here in this below mentioned examples.

Coding Image by Author

Output:

Image by Author

Checking the Type of Data held in a Tuple:

We can use type() function for this - see this here in below example.

Coding Image by Author

Output:

Image by Author

Other Useful Operations on Tuple:

See some more useful operations in below example.

Coding Image by Author

Output:

Image by Author

Range:

Let’s now discuss range data-type; Range is essentially a function in? python that we will discus here shortly. However It can be considered as one of sequence data types, as the elements of a range function can be accessed like other sequence data types such as tuple and list.

Range function definition:

There are 3 arguments that range function can take – these are start value, stop value and step value.

Image by Author

Creating and Working with Range:

Let's start this with this below example.

Coding Image by Author

Output:

Image by Author

If you observe, It has give me output like range(0,10), which is my start and and stop values – however this was supposed to give all numbers from 0 to 9 ( excluding my upper bound value - which is 10 here). So let’s see now how we can print the list of all sequential values generated by this function.

Use of List as a function:

You can see here, we have now all the sequence numbers generated by our function range(10) only . So it is incrementing each value by 1 (as step value is 1 by default) and begins the sequence from 0 (as start value is 0 by default).

Image by Author

Output:

Image by Author

Use of Range function with all 3 Arguments:

Let's give all 3 arguments seen above in range function and see how it works.

Coding Image by Author

Output:

Image by Author

Yu can see here again, when I changed my start value from 0 (default) to a new value as 5, So it has printed my list of sequence from 5 till 9 (excluding 10)

Providing all 3 Arguments as an Input:

See this in below example.

Coding Image by Author

Output:

Image by Author

You can see here, ?Every step is a gap of 2, In other words, each number has ben increased by 2 this time instead of 1. Since we have changed our default step value from 1 to 2 this time.

Few More Examples of same Type:

In order to explain this better, let's see few more examples of using all 3 arguments of range.

Coding Image by Author

Output:

Image by Author

Reversing the Elements of Range:

If you want to reverse your above generated 10th in reverse order; you can simply use reversed() function for this purpose.

Coding Example by Author

Output:

Image by Author

So – we’ve reversed our 10th table. I hope it’s clear so far and you are enjoying learning to code. Considering the length of this article, I’ll have to end this here. I’ll continue this topic and try to wrap this up in my next article.

Thank you for your time!










































































要查看或添加评论,请登录

社区洞察

其他会员也浏览了