Hey peeps! Welcome to an exciting tutorial on Python in which you will learn about the Python reverse list. We are on a series of deep learning phases where Python is under our observation. In the last tutorial, we saw the variables in Python and practised many codes in the TensorFlow. Today, we are interested to practice many interesting methods in an easy way. These are the built-in functions and you do not have to be an expert in the programming to perform them in TensorFlow. All you need is to read this tutorial and have the TensorFlow working fine. Before going deep into the topic, it is important to have a look at the list of content that you will learn today:
How do you define the python reverse list?
Why we should know about the python reverse list or keywords?
How do you practice the codes of Python reverse list in TensorFlow?
What is the core difference between some related keywords that seems to be work relatively?
How do you run some loops in the Python while working on the TensorFlow?
All these concepts will be cleared by discussing them in detail and while using the codes, you must keep in mind, there are more than one way to run the program in your own way but the one that we have defined in this lecture are the precise one and these are enough to understand the concept that we want to share with you. So work smarter then to word harder.
We know that there are some rules that have to be followed when you are naming your variables in python. Usually, these rules are applied to almost all the present programming languages but we are specifying Python because we want to connect this discussion with the Python reverse list. There are some restricted words that cannot be used as variable names. These are pre-defined in each language. You can define it in the following way:
"The Python reverse list (also known as keywords) is the complete list of pre-defined functions that are already stored in Python and these particular names are not allowed to be used as the name of variables while coding."
If you go into the detail, there are hundreds of reverse words in Python but this will be out of the scope of this course. For your information, we have added some very common keywords that you have to learn and practice to keep in mind. IT WILL HELP YOU A LOT WHEN YOU WILL WORK WITH THE COMPLEX AND LONG CODES. In Python, the following table is useful to understand what kinds of names are not allowed.
Python Reserve List |
|
True |
del |
false |
def |
not |
elif |
as |
class |
if |
break |
return |
None |
else |
with |
for |
lambda |
except |
yield |
The list does not end here, but I think it is enough to understand what types of variable names must not be used when you are practising deep learning with the help of TensorFlow (in our case).
Once you have read about the reverse list, you must practice it for the practice. Here, we are using some of the variables from the list given above. To practice it on TensorFlow, you have to follow the steps given next:
Open your Anaconda navigator to use TensorFlow.
Navigate to the environments and start the Jupyter lab.
The screen here will show you the cells where you can write the codes.
Start writing the codes using the keywords from the list mentioned in the above table.
The declaration of a function is done by using the “def” keyword. There is no need to tell the compiler the type of variable. After that, you can use this variable in other operations. Have a look at the code.
def welcome(name):
print (f"{name}, Welcome to theEngineeringProjects")
welcome ("Student")
The output of this program is given next:
Hence, you can see that we have declared the variable “welcome” and then provided the string in which we are using the function that we specified for ourselves. In the next line, we simply provide the value of the variable for that given string. You can simply change the value of a function in the last line to change the name with the same string.
This is a different type of keyword than in the previous case. The value “false” is declared by the compiler if we are providing information that is universally wrong. The best example in this regard is the one in which we are trying to equilibrate two different values of numbers.
The compiler is intelligent enough to clarify that the command given by you is not right.
This is the other simple keyword that is shown by the compiler itself, and if you are familiar with the keyword “false” given above, then it is obvious that if a universal truth or condition is fulfilled, the compiler will provide the answer in the form of the “true” keyword, as can be seen in the next image:
Another thing to notice here is, the string, or other data types may also be used with teh equality operation but as we are mentioning again and again, the Python is the case sensitive therefore, if the alphabets are the same but the case is different, the result will be contrary to the one given above:
The “L” of the second name is capitalized and therefore, the compiler is recognizing it as a different letter. It is the reason we are getting the “false” keyword as result.
As you can see, del is the short form of the “Delete” operation, and therefore, you can use this to delete any specific entry, value, or object from the list or the code. Here is an example of how to do so:
subjects = ['Physics', 'Chemistry', 'Biology']
print(subjects)
#applying the delete option
del subjects[1]
print (subjects)
When we put this code in our cells, you will get the results as expected:
At the start of the code, we declared an array of named subjects. You will learn more about these concepts in detail, but for now, just keep in mind that we have a group of sciences in our array, the index of which starts from 0. So, when you delete the first entry that is under the second name, you will get the list with only two entries.
Here is another keyword that is used in Python for your convenience. You can think of the “None” keyword as a blank space or an empty container. Take the example of the case when the user does not put the information required on a survey that is necessary to answer, then he/she gets the error from the website that this is a required field and therefore, the user has to put the information in it to move forward. This is best understood by the code given next:
Here, you can see the error message that appears when the required field is empty and the user wants to proceed.
The “if” is a special keyword in Python, and you cannot name it as a variable because it is against the rules. This is the name of a loop, and you must know that loops will be discussed in detail when you proceed in this series; therefore, we will not explain too much about this keyword. But for now, have a look at the code given next to get an idea about the workings of this reverse word
number = 300
if number >= 18:
print("You are eligible to become an Engineer")
Output of this code is given as:
Here is an interesting keyword that uses words in a very useful way. We have seen the error indication in different websites and other platforms where the user input the data and if it is not according to the rules of the input data then the screen shows the warning about the error. It can be done with the help of the raise keyword in Python. Let us take the case in mind where you have to provide your name in the form, and if the user provides the numbers rather than the name, then it will throw an error at you.
name = 123
if not type(name) is str:
raise TypeError("Only strings are allowed.")
So, it is clear that you have to put the name in the form of a combination of alphabets. By looking at this program deeply, we have seen the following points:
It is important to specify the type of content that you want to allow.
The “if statement” is also used in this program so that we can use more than one keyword in one line.
You can also change the type of content by specifying it in the first line.
The name of the variable does not matter.
By deleting the word “not” in the second line, you can invert the whole program.
In advanced programs, you can add more than one condition to apply all the necessary information so that any illegal way to type the name can be detected and the error may be shown.
The return keyword looks like the print function in the code, but both of them are not the same. The return function combines two or more results, and then control is given to the print function, which displays the results on the screen. Here is the program to do so.
def sum(x, y,z):
return x + y + z
print (sum(55,7,34))
The TensorFlow output is as follows:
Here, we have written a program that calculates the sum of three numbers. The start is done by using the define function, and here, we define a function that defines the pattern. This pattern will be used in the future. In the second step, the “return" function specifies the way the three components will work. In the third and last step, the values are put into the print function so that it may show us the result. This can be done in another way if we initialize another variable in which the result of summation is stored, and then we put that particular variable into the print function. The second way is more practical, but it occupies more space.
In addition to this, you must know that you can specify any number of elements in the formula in the first step. If the pattern contains more than three elements and you do not have a large number of elements to test, you can enter "0" in place of the extra elements, but you must follow the pattern and cannot enter fewer than the specified number of elements.
Here is the last keyword to explain in this lecture. It is the combination of two words, “else” and "if,” and it is used in the loops. You will practice a lot about the loops in the coming sessions; therefore, I am not explaining it much. But for now, you must know that when you want to add more than two conditions to a program, you use this keyword. In the language of C++, we use the else if the keyword for the same purpose. So, have a look at this program:
So, the user with knowledge of artificial intelligence is more likely to get the skills of deep learning, and we have made this program to show you this.
So, it was a helpful tutorial to learn a lot about the Python reverse list. These are the keywords that are pre-defined in Python, so it is not advisable to name your variable exactly like these. If you do not follow this rule, the compiler will be confused between your defined variable and the keyword saved in it, and therefore, it will throw the error. You must know that there are more words in the list, but for now, it is enough to understand and practice the words mentioned here.