Search This Blog

Wednesday, 15 December 2021

What are variables in python and How to use them

 

Python Variables

free amp templates

Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type. 

Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.It is recomended to use lowercase letters for variable name. Rahul and rahul both are two different variables.

Variable Assignment

Think of a variable as a name attached to a particular object. In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign (=):

>>>n=300

This is read or interpreted as “n is assigned the value 300.” Once this is done, n can be used in a statement or expression, and its value will be substituted:

>>> print(n)
300

Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without the need for print(), so can a variable:

>>>n
300

Later, if you change the value of n and use it again, the new value will be substituted instead

>>> n = 1000
>>> print(n)
1000

>>> n
1000

Python also allows chained assignment, which makes it possible to assign the same value to several variables simultaneously:

>>> a = b = c = 300
>>> print(a, b, c)
300 300 300

The chained assignment above assigns 300 to the variables a, b, and c simultaneously.

Variable Types in Python

In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type

Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type:
>>> var = 23.5
>>> print(var)
23.5

>>> var = "Now I'm a string"
>>> print(var)
Now I'm a string

Object References

What is actually happening when you make a variable assignment? This is an important question in Python, because the answer differs some what from what you’d find in many other programming languages. 

Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a specific type or class. (This point will be reiterated many times over the course of these tutorials.)

Consider this code:
>>> print(300)
    300

When presented with the statement print(300), the interpreter does the following:

  • Creates an integer object 
  • Gives it the value 300
  • Displays it to the console

You can see that an integer object is created using the built-in type() function:

>>>type(300)
< class 'int'>

A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object. 
For example:
>>>n = 300

This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.

The following code verifies that n points to an integer object:

>>> print(n)
300
>>> type(n)
< class 'int'>

Now consider the following statement:

m=n

What happens when it is executed? Python does not create another object. It simply creates a new symbolic name or reference, m, which points to the same object that n points to.

Next, suppose you do this:

>>>m=400

Now Python creates a new integer object with the value 400, and m becomes a reference to it.

Lastly, suppose this statement is executed next:

>>>n="foo"

Now Python creates a string object with the value "foo" and makes n reference that.

There is no longer any reference to the integer object 300. It is orphaned, and there is no way to access it. 

When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory so it can be used for something else. In computer lingo, this process is referred to as garbage collection.

Variable Names

The examples you have seen so far have used short, terse variable names like m and n. But variable names can be more verbose. In fact, it is usually beneficial if they are because it makes the purpose of the variable more evident at first glance. 

Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.
  • Note: One of the additions to Python 3 was full Unicode support, which allows for Unicode characters in a variable name as well. You will learn about Unicode in greater depth in a future tutorial.

For example, all of the following are valid variable names:

>>> name = "Bob"
>>> Age = 54
>>> has_W2 = True
>>> print(name, Age, has_W2)
Bob 54 True

But this one is not, because a variable name can’t begin with a digit:

>>> 1099_filed = False
SyntaxError: invalid token

Note that case is significant. Lowercase and uppercase letters are not the same. Use of the underscore character is significant as well. Each of the following defines a different variable:

>>> age = 1
>>> Age = 2
>>> aGe = 3
>>> AGE = 4
>>> a_g_e = 5
>>> _age = 6
>>> age_ = 7
>>> _AGE_ = 8
>>> print(age, Age, aGe, AGE, a_g_e, _age, age_, _AGE_)
1 2 3 4 5 6 7 8

There is nothing stopping you from creating two different variables in the same program called age and Age, or for that matter agE. But it is probably ill-advised. It would certainly be likely to confuse anyone trying to read your code, and even you yourself, after you’d been away from it awhile.

First Program in Python

 

Python Hello, World

free web maker

Python is easy to learn and code and can be execute with python interpreter. We can also use Python interactive shell to test python code immediately. A simple hello world example is given below. Write below code in a file and save with .py extension. Python source file has .pyextension. 

Writing the “Hello, World!” Program

To write the “Hello, World!” program, let’s open up a command-line text editor such as nano and create a new file: hello.py 

Once the text file opens up in the terminal window we’ll type out our program :
print("Hello, World!")

Let’s break down the different components of the code.

1

print() is a function that tells the computer to perform an action. We know it is a function because it uses parentheses. print() tells Python to display or output whatever we put in the parentheses. By default, this will output to the current terminal window.

2

Some functions, like the print() function, are built-in functions included in Python by default. These built-in functions are always available for us to use in programs that we create. We can also define our own functions that we construct ourselves through other elements.

3

Inside the parentheses of the print() function is a sequence of characters — HelloWorld! — that is enclosed in quotation marks. Any characters that are inside of quotation marks are called a string.

4

Once we are done writing our program, save the file and we can exit notepad.

Once you exit out of notpad you’ll return to your shell or cmd.  

Running the “Hello, World!” Program

The hello.py program that you just created will cause your terminal to produce the following output:

Hello, World!

Congratulations! You have written the “Hello, World!” program in Python 3 .
Since the program ran, you can now confirm that Python 3 is properly installed and that the program is syntactically correct.

How to install python

 

HOW TO INSTALL PYTHON

site builder

Python 3 Major Version Installation (Wondows)

1

Download the latest Python 3.x version. At the time of writing this article latest version was Python 3.7.4 (July 8, 2019). Download Windows x86 – 64 executable file only as installer will automatically install 32 or 64 bit of Python according to the system configuration.

2

Open the executable file and Check the Add Python 3.7 to PATH. Then click the Install Now button. It will show the installation progress. 

3

When the installation progress is completed, you will see the Disable path length limit. Now you must be thinking what is it and whats will happen if I will disable it. The answer is clear, it will remove the limitations on MAX_PATH variable. It will allow to use long path names for the Python. We recommend you to not disable this option as it will remove any path related issues while working in Windows. Therefore click on the close button to finish the installation.

4

Now, the Python 3.7.4 is installed. You can check it either it is properly installed or not. You can do it through Command Prompt. Open the command prompt and type the following command -" python -v ". It will output the version of the Python.

5

Congratulation, you have successfully installed Python 3 version. you can read the next tutorial to Python Programming a complete guide for beginners.



Python 3 Major Version Installation (Linux)

Linux is an open source Operating System. There are many Linux based operating systems. Popular are Ubuntu, Fedora, Linux Mint, Debian. In this section you will learn how to do python installation for both Python 3 and Python 2 versions. Fedora Linux OS used for Installation of python. Most of the newer Linux based Operating system have already installed Python. You will check it is installed or not by the typing the following commands in terminal. 

For Python3
$ python3 --version

For Python2
$ python2 --version

You will see the python versions as output like in the below screenshot. But if you are not seeing then , you have to install Python . Follow the following steps for successful install.

If you are using Ubuntu 16.0 or newer version, then you can easily install Python 3.6 or Python 2.7 by typing the following commands 

$ sudo apt-get update
$ sudo apt-get install python3.7

Python 3 Major Version Installation (MacOS)

MacOs is an operating system developed by the Apple Inc. It is just like Windows Operating System and other operating system. Most of the newer versions of MacOS have pre-installed python. You can check python is installed or not by the following commands. 

python --version

Download the Python 3 or 2 new version. At the time of writing this post , Python 3.7.4 - July 8, 2019 was the newer version. Download the Mac OS X 64-bit/32-bit (https://www.python.org/downloads/mac-osx/) installer. Run the package and following the installation steps to install the python packages

After the successful installation , you can check the python version by using the same command.

python --version

how to implement YOLOv3 using Python and TensorFlow

Object Detection with YOLOv3 Introduction YOLOv3 (You Only Look Once version 3) is a real-time object detection algorithm that can detect ob...