Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

Hey learners! Welcome to the new lecture on deep learning, where we are using TensorFlow to learn it with the help of Python. Previously, we worked on the syntax of Python, and now it's time to discuss the variables in detail. There are some variables that you will learn about as well as get hands-on experience within TensorFlow. These are important concepts that will help you throughout your coding career. If you are new to programming, this is a crucial concept for you, and if you know it already, you can use this tutorial to polish your concepts. We will move forward after looking at the list of content for this lecture:

  • What are the variables in Python?

  • How do you assign the value to the name of the variable in Python?

  • What are some rules to define the name of the variables?

  • How do you declare or initialize the variables in Python?

  • Can you get the type of variable that is declared before?

  • What is the difference between statically and dynamically typed programs?

  • What is the purpose of re-declaration in Python?

Introduction to Variables in Python

In the previous lecture, we mentioned the name string, and the difference between the string and character is just the amount of storage these two occupy. The reason these are important to understand is to use an accurate way to store our data. This will be clearer when you see the introduction of the variables in Python:

"In Python, the variables are the containers that store the information in them and are defined as the name given to the location in the memory."

We all know that when a program is saved in the compiler, it allocates a specific amount of memory. Variables in Python work differently than variables in other programming languages such as C++ and C#. In these languages, the programmer has to define the type of the variable and allocate the specified amount of memory required to store that variable. Still, in Python, the program statically types. The other type of program in this regard is dynamically typed programming. For your convenience, I have made a comparison between these two types:

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

Sr#

Statistically Typed Programs

Dynamically Typed Programs

1

The checking in the code for the error is done before running the program. 

The checking for the error is ignored at the compile time and is done in the run time. 

2

The type of the object is known by the variables. 

The variable does not know the type of object but the object knows itself. 

3

At compile time, the “Unsafe operation” is rejected. 

The “Unsafe” operation is rejected at the runtime. 

4

Example: C++, C

Example: Python, PHP


So, Python is easy to understand, and you do not have to work hard for the declaration of variables of a different kind; most of the time, the declaration is super easy. You will see this when we work on the examples of the variables in just a bit. 

Assigning the Values to the Variables

When declaring the variables, you have to be very careful about the data you are placing in different containers. The reason behind this is, there are hundreds or more than it variables in the code when we start professional coding in Python. In such cases, it is important to memorize the name and working of the variables instantly. It is not advisable to name the variable without following any logic to memorise the exact information about that particular variable. The list of the variable is shown on the side of the TensorFlow but the working must be shown with the name of your variable.

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

For example, if you are declaring the variable that calculates the sum of numbers then the name of that particular variable must be “sum” or “addition”, or any other word that describes the function otherwise if you are naming it as “x”, or “var1” then you have to think for some moment that why you had initialized these variables. This rule is also beneficial to the case when the code is read by the other person or if you are sharing your code with the other person to work further on it.

Here, you must know, the code that we are practising is tiny and these are easy to understand to make the points clear to the beginners as well but at a professional level, the coding is different and you have to make sure that you are writing a clear, clear, and easy to understand code as the code in such cases are too long and contain many concepts in a single line sometimes.

Rules For the Variables in Python 

For the declaration of the variables in the Python programming language, you have to follow certain rules, and for the convenience of the reader, we have made the points, and there is no need to memorize them; you just have to read them first.

  • The name of the variable must always start with the alphabet or the underscore character; otherwise, it is against the rules. 

  • The variable name does not start with the number. You can use the number in between the alphabets of the variable name, but not at the beginning. 

  • Symbols are not allowed to be used in the variable name. In other words, you can use only alphabets (A to z, in which the case of the alphabet does not matter), numbers from 0 to 9, and the underscore only. As a result, you can't use symbols like @, #, and $.

  • Variables name is case sensitive. This can be understood with the example that the variables Type, Type, and Type are totally different, and these are the names of three variables. 

  • The declaration of the variable may be done with the help of a single alphabet as well such as x, j, y, etc. But it can't be a single number such as 1, 2 3, and so on.

These are the universal rules, and usually, the programmer creates his or her own code of conduct in order to ensure that he or she always follows the same route to write the code, making it easier to understand the old codes written by him.

Variable Declaration in Python vs. Other Programming Languages

Another reason why we say that Python is an easy and convenient programming language is that the declaration of different types of objects is easy in it. As we are studying the variables right now, I must tell you that in other programming languages, you have to declare the type of the variable first and then give it a name. You can set the variable's value either at the declaration or on the following line by providing the name and value. An example of the variable declaration in C++ is given below:

Int x=78;

or int x;

x=78

In contrast, when working with variables in Python, you do not need to define the type of the variable and can simply give it a name and a value. As a result, the program's work is simplified because the Python compiler is intelligent enough to recognize the type of the variable on its own and does not require the user to specify it. Here is the example that verifies the information given here:

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

Similarly, when you are declaring the string or character, you will simply declare it with the name and value without specifying the type. 

#Declaring the Character

character='Python'

print('Character is "', character,'"')

#Declaring the String

string="We want to work with deep learning through Python."

print('string is "', string,'"')

Just look at the output, and then I'll discuss the details of the code.

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

The following points are proven with this code:

  • The name of a variable may be anything, and the value determines the type of variable in Python. It can be understood with the fact that even if we name of a string is declared as a character but detail the value has double quotation marks around the values, the compiler will read it as the string and it will occupy the space in the memory accordingly. 

  • Comments in the code are totally ignored by the compiler, and these are helpful to understand the block of code defined by it. 

  • In the print function, if you want to show the values as they are, you write them in single quotation marks, and if you want to declare the value stored in the variable, the variable is written as it is. 

  • To separate the printed message and the name of the variable, we use a comma between them. 

  • Using the single "print" function, you can print multiple variables or messages. 

  • While printing more than one output on the screen, you do not have to use indentation these print functions are non-consecutive. 

  • In Python, the single quotation marks are equal to the double quotation marks but as we have seen that both of these are different in other programming languages, to illustrate the type, we have used both examples. 

The types of data will be given in the next session, where you will learn a lot about them.

Getting the Type of The Variable in Python

If you do not know the type of variable and want to get the related information for different types of operations, Python has a special pre-defined function that works in a simple way. You just have to put the value you want into the “type” function. The syntax of the “type” function is given next:

print(type(variable name))

Hence, the type of the variable will be printed on the screen. This can be best understood when you see some examples in TensorFlow. To do this, write the following code in the TensorFlow cell:

a=56

b="Deep Learning is easy with TheEngineeringProjects.com"

print(type(a))

print(type(b))

As soon as you will pop the play button, you will get the results as follow:

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

The reason why “class” words are used here is that Python is an Object-Oriented programming language, and it makes the classes perform the operations; therefore, it has presented the results in the form of classes. 

Re-declaration of Variable in Python

Once you have learned about the declaration of the variable, you might be thinking that the single value may be assigned to the single name only, but it is not true all the time. Suppose if you are using a large number of values in a program that will be used only once and you do not want to suggest single name to a single value, you can declare the variable again and this method is called the re-declaration. Have a look at the example to do so:

a=45

print("The value of the variable is", a)

a=90

print("The value of the same variable is now", a)

Types of Python Variables in TensorFlow, python variables, variables in python, python variables types, python variables tensorflow

So, you have simply masked the first value and given the same name of the variable to any other value. But in such cases, you cannot get the previous value back until you provide the first value to the name again. This is the reason why we call them "variables,” which means the non-fixed process. The values in the variable keep changing over time according to the needs of the program and the code written by the programmer.

Hence, it was the day when we learned a lot about the variables in the Python programming language. We have seen a lot of information about the variables and seen how you can introduce and work with the Python variables in different ways. We have compared the declaration of the Python variables with the other programming languages and also got information about the statistically typed programs versus the dynamically typed programs. Moreover, it was interesting to know about the type function and the declaration of the variables in Python. I hope it was an informative tutorial for you and that you will practice more to get experience coding in Python.