Hey, peep! This is a connected tutorial from the previous one where we saw the detail of numeric data types. This time, we are moving forward with the other data types in Python. We are understanding all these concepts with the help of examples and practising the little but understandable codes in TensorFlow. Different types of operations are also performed on these data types so that you may have an idea of why we are differentiating all these data types and how we can categorize all of them into different groups. Keep in mind that all of these concepts are for deep learning, and we want to make sure that we will not face any problems in the complex work of deep learning; therefore, we are moving slowly and steadily in Python. So, have a look at the content you are learning in this tutorial, and after that, we’ll start the introduction.
What are the strings in Python?
How do you declare the string in different ways while working in Python?
What are escape sequences in Python?
How can you use the triple quotation in Python and why it is useful?
Each concept will be discussed with the help of simple and easy codes and the description of each of them is discussed in detail in this lecture. This is a connected part of the lecture that was discussed in the last lecture and other data types will be mentioned in the next lecture.
A string is nothing but the combination of different alphabets in a specific sequence. In programming, the concepts are a little bit different from those in the real world. The way we speak in English is said to be "string" in the programming language, and it is an important data type in any programming language for non-programmers, anything that comes on the screen must be easy to understand, and string is the best way to print the message on the screen. Let us define the string in simple words.
"The string is the sequence of characters or alphabets that specify the message on the screen and it is denoted by 'str' in Python."
We always say that python is simpler and easier than other programming languages, and it is true for the concept of string as well. In Python, the string can be denoted by using single or double quotation marks and the usage of commas is according to the choice of the programmer. In other words, you can use the single or double inverted commas around the alphabet to represent the string. Moreover, you must know that string has no limited length same as in the case of integers. The only thing that limits the length of the string is the memory space of the system you are using. Have a look at the syntax of the string while you are working on Python.
First of all, you have to look at the syntax of the string. The syntax of all the data types is the same; therefore, we are mentioning just this one, and after that, you will have an idea of how to implement the other data types. We have mentioned in the previous lectures that you just need the name of the variable and then the value of the variable in the form of any data type you want, so the syntax is given as
string = "I am learning Python for Deep learning."
The name may be anything, but it is important to use inverted commas (either single or double). TensorFlow will make this clearer, but there is a short procedure to get TensorFlow up and running.
Search for the “Anaconda Navigator” on your PC.
In the environment section, click on the Jupyter lab and launch TensorFlow.
The new tab will open in your browser with the name “Local host.”
Go to the new cell and start coding there.
Now, you have to write the following code in the new cell and run the program to get the output.
string="I am learning at TheEngineeringProjects"
print(string)
a=" "
print(a)
b='Python is best for Deep learning'
print(b)
The output of this code is given as:
From the code and output, we can conclude with the following point:
The name of the variable may be anything.
We can use single or double inverted commas for the string and the result will be the same.
A string may be an empty space so we can say that length of the string is zero to positive infinity.
The output of each print function is always shown in the next line in normal conditions.
So, the best way to show any message to non-programmers is in the form of a string.
In most high-level programming languages, there are certain words that are chosen to be used for a special task in Python with the help of their special sequence. These are known as the escape sequence. These are defined as:
"The escape sequence in Python is the combination of characters that, when used inside a string or character, does not show itself but does the specific task according to its functionality."
It is important to notice that these are important concepts in Python because it is very difficult and in some cases, impossible to do the same task without using the escape sequence. Now, have a look at some of these in the next section and you will get the detail of some important escape sequences.
As you can see, in the previous code, we made a space between two lines with the help of an empty string. But, what if we want to print a new line in the message? For this, we use a special operator in the string message and it is used in places when the output is long or there is the need for more than one line to represent the message more clearly. The operator is a backslash with an “n” that can be used at any place in the text. Have a look at one example to do so.
print('The backslash with "n" is used to \nprint a new line')
The output of this single-line program is interesting.
It is important to notice that if you want to work with this operator, you have to use it properly. You can not use any additional space between the text and this new line operator; otherwise, you will get an error message from the compiler.
We all use the tab on the keyboard but what if you want a space between the text? You can do so by applying the space between the text while you are printing the message but it is not the professional way. To do this with convenience, just like some other programming languages, Python has a special operator. Same as we use the n in the new line operator, if you use the t with the backslash, you can print the space between the text that is equal to eight space bars.
print('The backslash with "t" is used to \tprint a tab space in the line')
Let’s see what we have in the output of this code.
Same as these operators, we also have some other commands that do the work similar to this and the syntax of all of these is the same. For the convenience of the reader, we have made a table that contains all the information about these operators. Have a look at the table and after that, we will run all of these at once in our code in TensorFlow.
Name |
Representation |
Description |
New line |
\n |
It creates a new line in the text even if you are using it in the middle of the line. |
Tab space |
\t |
It is used for the space tab between the text. |
Bullet |
\a |
For the bullets in the text, we use this operator. |
Delete space |
\b |
To delete the space in the text, we use this operator. In this way, the text obtained only removes the space from the place where this operator is being used and the other text remains the same. |
Ignore the line |
\r |
By using the working of this operator, the text before the operator is deleted, and you will get the text after this operator only in the output. |
Arrow |
\v |
If you want to show a small arrow in the text, you will use this operator. |
To test each of the commands discussed before, we are rushing towards the TensorFlow, where we are using all of these in a similar ways to show the difference between all of these. Keep in mind, the syntax of each of them is the same. These are not the proper functions but are the pre-defined commands that are used less commonly in Python. Here is the homework task for you. You have to look at the code we are giving below and without cheating will guess the output.
print('We use \ncups for tea')
print('The tab create \teight spaces in the text')
print('\aBullets can be made using this operator in Python')
print('If you want to delete the \bspace, you can use the operator in Python')
print('This part will be ignore \ronly this will be printed on the screen')
print('\vThis small arrow looks cute')
Once you have guessed the output, now check this in your compiler, and then match the output with the one that is given next in the image.
The last arrow can also be used as the bullet in the text you want to show on the screen. Another task for you is use the different text and practice the code and string with your own message. It is the simplest and interesting task that you must try on your TensorFlow.
Here is another way to indicate that you want to declare the string. This is a less common way to represent the string, but if you are studying the string, you must know this. You can use double and single inverted commas around the text at the same time, and this will not cause any errors. Let me tell you about the workings of this kind of string. After that, I’ll show you how you can use it for different purposes.
print('''Deep learning is the subclass of the artificial intelligance''')
So, we are using three quotation marks around our text and getting the same output as for the single and double inverted commas. Here is the output:
The advantage of using this method is, you do not need any new line operators to start the text on a new line, but you will write the code as you want the output.
This is a more interesting and convenient way to write your text. Right now, you must be wondering why we are highlighting such uses and creating this hype. Yet, you must know, the aforementioned ways of writing the codes will help you a lot when you will go to the complex and long codes for the deep learning. One of the application of this way to declare the string is in this lecture. We can use this way to declare the string and can perform all the escape sequence command by declaring a single string in different lines so that we may not have to write “print command” every time when we are working with a new escape sequence.
Hence, we have read a lot about strings today. It was an interesting lecture where we saw what strings are and how we can use them in our coding in different ways. We say the representation, working, and the escape sequence between the string. The syntax of each case was clarified with the help of examples in TensorFlow. You will get information about more data types in the next lecture.
Hey fellow! Welcome to the next episode of the Python series, where we are learning the basics of Python to implement them in deep learning. In the previous lecture, our focus was on string data types. With the practical implementation of TensorFlow, many interesting points were discussed in depth. I hope you completed the home task that I assigned you during that lecture. Today, we are moving forward with the next data type, which is a sequence. You will know the different sub-groups of this data type as well in the next lecture, but today, the focus will be totally on the list because, once you understand them well, other data types of the sequence will be at your fingertips. Yet before starting, it's time to look at the content that you will learn today:
What is a sequence?
How do you classify the sequence into different types?
How do you understand the list?
What are some characteristics of this list that make it useful and different from the others?
The workings and characteristics of the list are interesting, and these will be well understood when we start our TensorFlow for different examples taken from our daily lives. We hope you have a strong grip on the integers, strings, and floats because, in our example, we will use them and will try to make changes in the list to show the difference in detail. So, without using your time, I am going to start the learning phase in Python.
In programming languages, we use a lot of items, objects, classes, and related concepts, and when we talk about the sequence, that is a data type that provides us with the concept of a collection of things. These are useful concepts that allow us to work with them in a useful way. We define the sequence in the following way:
"The sequence in Python is a generic term that defines the ordered set of items in such a way that any of the items can be easily referred to."
Here, the word “ordered set” is important to notice. We’ll talk about it in detail in just a bit, but before that, let me tell you that the string is also considered to be the sequence because it contains the sequence of the characters. We have read a lot about the string, and therefore, we know that the ordered sequence of the characters, or alphabets, is called the string. Yet, I had a lot of data to tell you about these concepts; therefore, I have discussed them separately. There are two natures of the sequence that are listed below:
Homogeneous sequence
Heterogeneous sequence
The difference between these two is that a homogeneous sequence contains a group of items of the same kind.
Homogeneous Sequence |
Heterogeneous Sequence |
A homogeneous sequence contains a group of items of the same kind. |
A heterogeneous sequence contains a group of items of different kinds. |
{"Apple", "Banana", "Cherry"} |
[Cup, knife, banana] |
It is important to know this difference because, on the basis of it, you will get the further types of the sequence.
The collection of the data can be represented in different ways, and on the basis of these types, we can divide the sequence into six major types. All of these are important to understand because we use the sequence often while programming and when dealing with complex subjects such as deep learning (in which we are interested), and the sequence plays a vital role in organizing the data in an understandable way. So, have a look at the types of sequences:
String
List
Tuple
Byte sequence
Byte array
Range objects
Out of them, the strings have been discussed in detail in the previous lecture. So, we are skipping that for now. You will be familiar with the remaining ones in depth, and things will become clearer to you with the help of examples. Thus, have a look at them:
In some other programming languages, such as C++ and C#, there is the concept of arrays that we study a lot. Yet, in the Python programming language, this concept is not used; instead, lists are used. The list is indicated by the square brackets just like the arrays, but it is a little bit different from them. The list is the type of sequence that contains an ordered group of different items. More information about the list can be found in the table below:
Name |
Attribute |
Representation |
Square brackets |
Nature |
Heterogenous sequence |
Example |
[‘cups’, ‘eggs’, ‘yolk’, ‘tea’] |
Here, it is important to notice the representation of the list with the square bracket because the bracket is the only way to differentiate it from the other types of sequence that you will learn in the coming lectures. The operation of the list will be explained in simple steps in a moment, but first, let us open the Python workspace for practical implementation. For this, you simply have to follow the steps again that we always mention:
Search for the Anaconda Navigator on your personal computer.
Go to the environment section and look for the “Jupyter lab” or “Jupyter notebook." For this tutorial, we are using the Jupyter lab.
Go to the new cell and get ready for the coding.
The list's information does not end with the table mentioned above. Certain characteristics make the lists useful, and you will learn about them in the following section.
One of the most significant features of Python is its mutable nature. To know the meaning of the line we have just said, you must know that when we declare a list, we define the size of that particular list by mentioning the number or directly feeding the elements in it. The list's advantage is that the programmer can change the elements at any time to meet the needs of the situation. In addition to this, if you want to change the individual value of the list, you can do so easily. Not only this, but you can also change the order of the elements while working on the code. It means if you want to swipe or change the position of elements 1 and 3, you can do so easily. In short, you can say that, while working with the list, all the controls are in your hands.
MyIntList=[1,56,8,12,56,90,3,67]
print('The homogenous integer list is : ', MyIntList)
MyFloatList=[1.2,56.2,8.2,12.2,56.2,90.2,3.2,67.2]
print('The homogenous float list is : ', MyFloatList)
You can see that all the elements belong to the same data type, so we are calling it the homogeneous list. In the second code, we want to do something different, so have a look at the code.
list=['eggs', 2.0, 34, 'plates']
print('The elements of the list are : ', list)
print('The element at position 2 is : ', list[2])
print('The elements before the second position are : ', list[:3])
list=[ 34,2.0, 'eggs', 'plates']
print('Now the new list is: ', list)
By looking at the code given above, we can conclude the following points:
To declare more than one type in a single list, the programmer simply has to use the accurate way to declare them in the list, and nothing special has to be done during the declaration of the list.
To get a specific element from the list, simply the position of the element is mentioned.
The position in the elements start from the 0 just like the arrays and you have to declare the element accordingly.
The order of the elements can be changed easily and to do this, the programmer have to merely change the elements according to the will with the same number. Here we have used two types of lists with the same name but by changing the order of the elements and the code is working well.
To get the elements of the list to a certain limit, there is a need of mentioning that number of position along with a colon. Keep in mind, here we specify the position 2 and all the elements before the third position are shown to us. Same can be done to get the elements after the mentioned number and it is your homework to know how can you do so.
When you compare the list to the array, you can better understand Python's dynamic nature. Let me remind you that arrays have a fixed size and cannot be changed once declared in your code. But in the case of the list, once you start working on it and need to change its size, you can increase or decrease its size by merely mentioning it in just one line. In other words, your list can grow or shrink according to your choice. To understand this you must know about a built-in function in Python.
The length is a built-in function in Python that provides the length of the functions made in the code. You simply have to input the name of the function in it and it will show you the number of elements specified in that particular function. The syntax of the length function is:
len(name)
By using this function in the list mentioned above, the number of elements can be obtained with the help of this code:
list=['eggs', 2.0, 34, 'plates']
print('The length of list is : ',len(list))
list=[34,2.0, 'eggs']
print('The new length of list is : ',len(list))
The output of the code is given next:
Keep this in mind the next time you go grocery shopping and want to buy something else; make a list for it. It will contain vegetables, meat, cloth, spices, and other things of the natural world. Because of its heterogeneous nature, the list is the same. It feels like a relief that you can make a list that contains strings, integers, and any other data types in one place. In this way, you can deal with multiple types of data at once. In arrays, this is not possible. You can also add functions to the list that contain elements of different types. You have understood it well with the previous examples in TensorFlow, but I want to show you this by mentioning a built-in function in it.
Mylist=[23,'decoration', False ]
print(Mylist)
Yet, there is a bit of difference between the keywords and loops; therefore, you can not mention the loops in the list; otherwise, you will face the same error as given next:
Here, the error is demanding that you write the code in the valid syntax. It is because the compiler is not able to understand that the for loop is an element of the list, but it demands the proper syntax for the for loop and is expecting an iterative procedure. Another thing to notice here is the color of the word “False” in the list. All the keywords, while coding, are shown in green, showing that it is a built-in function or keyword and that the compiler has understood the default functionality of that particular keyword.
Hence, it was an interesting tutorial, through which we learned a lot about the list data type. We initially understood what list data types were and why we were learning them. After that, a detailed overview of the list was discussed, through which we found the characteristics of the list. It was easy to understand as we practically performed each step of the TensorFlow with the help of different examples that were related to the general examples, so we were able to understand it well. If you have a clear understanding of the list, the following lectures will be simple to grasp because they are related concepts. So, stay tuned with us to get more information about Python.