Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Hey learners! Welcome to another deep learning tutorial, in which we are beginning the practical implementation of Python on the TensorFlow library. We installed and checked TensorFlow in detail while we were in the previous lecture, and today we are going to use it for our practice. We have checked the presence of a perfectly installed library of TensorFlow in our tutorials and seen the basic structure of this library. As a result, we will skip the details and jump right into learning Python. In this tutorial, the main focus will be on Python instead of learning the workings of TensorFlow. You have to remember one thing: all the discussion will be from the point of view of deep learning, and it is not a general tutorial in which you will learn to develop apps or have a discussion about the details of Python; we will learn all the basics in detail, and after that, you will see Python in the field of deep learning. But first of all, we are showing you the list of content that you will learn in this lecture:

  • How do you introduce the syntax of Python?

  • What is the syntax error, and how do you define the other types of errors?

  • How can you execute the instructions of Python in TensorFlow?

  • How do we print the message in different ways while using Python?

  • What is an indentation in Python, and why is it important?

  • How can you use comments in Python?

These all concepts will be cleared as we are going to discuss all of them one after the other with the practical implementation and no step will be missing so let us discuss the detail of each of them.

Syntax of Python

As we all know, the syntax is the most important thing that you must know, even if you are typing the simplest program in any programming language. All these programming languages are recognized by their syntax, and even a single mistake with a semicolon matters a lot. When talking about Python, we have mentioned many times that it is a simple and easy programming language that is easy to understand. Before going into detail, I want to show you the definition of the syntax, and after that, we will link this to Python.

"In programming languages, the syntax is the set of predefined rules that communicate with the computer and tell it how to read the code."

 This is the combination of alphabets, numbers, and symbols in a specific way and the programmer always has to follow these rules otherwise the computer will throw errors. Most of the time, the compiler shows the suggestions if the code is slightly different from the one that must be followed. But it is not true all the time. There are two types of errors:

  • Syntax error

  • Logical error

  • Runtime error

The details of each of them are given next:

Syntax Error in Python

In the compiler, syntax errors occur when the programmer does not follow the syntax of the language exact, and the compiler is not able to understand the exact operation. Therefore, it shows the error and is not able to perform the required function. As the instructions are pre-defined, they show the list of errors and the possible solutions to the problem. This solution may be in the form of an instruction, a statement, or any other clear indication of where the error is present and how you deal with it. 

Examples of Syntax error in Python

There are many ways to show you the exact information, but I am choosing the basic way to do so because many beginner-level programmers are also there and they need the information from scratch. The discussion above will be clearer with the help of the example given below.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Here you can see that the compiler does not know the correct spelling of the command given to it, but the point to observe is that you get the suggestions and other statements to solve the error.

Other Types of Errors

The other types, which are the logical and runtime errors, are important to discuss here because, in this way, we can differentiate them better. When talking about logical error, you have to be very clear about your instructions and have to provide the best path to the compiler so that it may solve your code and present the right output. If there is a mistake in the logic, or, in other words, if you are telling it to compare illogical things or do a task that is not possible in real-time, it will not accept it and will do exactly as per your instructions, but the required results will not be obtained. This situation worsens when the programmers' concepts for solving the given problem are unclear, making it difficult to identify the error in the code.

Contrary to this situation, we observe another type of error that is a run-time error. A pure run-time error is one in which the logic and syntax are completely correct but you still do not get the desired results because the program does not receive the error during compilation but is unable to retrieve the required information from the code at runtime. The difference between logical and runtime errors is that in logical errors, the program is not compiled well because of illogical or incomplete information, whereas in runtime errors, the compiler is able to compile or gather all the information but the program is not complete because of the missing information that is to be gathered from the other piece of code. At this time, I do not want to discuss more these two error types more; my focus is on the syntax error. Truss, have a look at the table below that describes all these errors at a glance.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Name of the Error

Short Description

Possibility

Complexity to Identify

Example

Syntax error

Errors occur due to incomplete or incorrect syntax.

It happens mostly if the programmers have little practice typing or are beginners.

The compiler provides suggestions in the errors, making them easy to identify. 

Spelling mistakes, using variables before their initialization, and missing the opening or closing of the brackets. 

Logical error

Occurs when unrealistic logic is applied to the code.

These errors usually occur when the concepts of the programmer are not very clear. 

Difficult to identify the mistake. 

Infinite loops, incorrect boolean operations, the wrong type of brackets.

Runtime error

While the information is incomplete in the code and the compiler does not get the required data, the program is not run, and therefore, we get the runtime error.

These errors happen due to the carelessness of the programmers. 

Usually, the instructions from the compiler are enough to identify the runtime errors. 

Demanding the information from an array with a position that is not present in that array, getting the result from the loop that is not yet been completed yet. 


Executing The Python Instructions

The Python instructions are clean to execute, and you have to be very clear about the syntax before getting started. We believe that some of the instructions are well known to you, but for the sake of practice and to prevent any gaps in learning, we are discussing all the basic information here. 

Printing the Message in Python

The first program that is obvious to practice while programming is the Hello World program, which is always practised when learning any programming language. But I want to do something different. Instead of writing the "hello world" program, I would like to write any other message. For this, you just have to follow the instructions given next:

  • Search for the installed software of “Anaconda Navigator” and run it on your PC. 

  • Go to Jupyter Lab and launch it.

  • You have to write the code in the cells given on the screen. 

  • The syntax for the printing of a message in the form of the string is given as

print(“You message”)

So, you have to follow some important rules all the time when you want to print something. 

  • Write the keyword “print” to show any message.

  • The spelling must be exactly the same; otherwise, you will get a syntax error. 

  • The text should be wrapped in parentheses. 

  • The right data type should be mentioned. (You will learn a lot about the data types in the coming lectures, so do not bother about it.) You must know that you wanted to print the string message; therefore, you have used inverted commas.

  • There is no restriction on printing the specific message between the commas; in other words, if you make a spelling mistake or make another type of statement, the message will be printed as is and the compiler will not throw an error. Yet, the syntax should always be followed strictly. 

Once you have followed all the rules, the result obtained on the screen will be like this:

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Here, you can see that the message is printed as it is in the next line, which is the output line. 

In programming, there are different ways to perform the same task, and you can choose any of them. Now, the printing of the message can also be done in another way if you do not want to print the string. You can do so with the help of variables.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

We hope it is clear to you, but if you have any ambiguity, you can learn this in our upcoming lectures. For now, just look at the fact that we have stored the message in the variable and then fed this message into the print command.

Rule of Indention in Python

While we say that coding in Python is simple, you may be surprised to learn that there is an indentation rule in Python. We all know that indention is the way of writing or typing something in with a gap at the start. Here is an example of the code without indentation.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

You can see that the syntax and all other parameters are okay, yet you have to write the blacks with indentations to identify their separations. So, we are just making these simple changes and trying to run the program. Let’s see what happens.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Important Tip 

As a programmer, you must know that you can specify the number of spaces of your choice, but the range is between one and four spaces. Moreover, if you have a bigger code than this and provide more than the required number of spaces, you will again get the error of too many spaces. So the number of spaces must be precise. Moreover, for such codes, the number of spaces must be equal. For example, if you are using more than one print function, then these two must be equally indented; otherwise, you will get the error.

Comments in Python

Most of programming languages have the "comment" option. These prove convenient and are also interesting to know about. The comments are defined as:

"The comments in the programming languages are the additional notes and instructions that are saved by the programmer itself and ignored by the compiler."

The comments start with special symbols to tell the compiler that the line after this symbol is to be ignored. All the programming languages have different symbols to do so, but in Python, the hashtag sign is used, and the line after this hashtag is totally ignored so that you may store the notes in that line.

Syntax of Python with TensorFlow, python syntax, python syntax in tensorflow, tensorflow python basics

Observe that your message is ignored fully, and the compiler has just printed the message without any error. Comments make the code more readable and easier to understand. The comments are usually italicized and have a different color than the usual code.

Therefore, we have learned a lot about the syntax of the programming language and how important it is to follow the syntax. We have seen the different types of errors and have learned the print command, indentation, and comments in detail. It was an interesting lecture and be with us for the advanced learning.