Menu Close
Python Variables

In this tutorial, we are going to learn about Python variables, and also we will learn types of data types in Python programming. In the previous tutorial, we have seen how to add Python to the windows path to run the Python programmer on the terminal. If you don’t know, then you can click here to learn.

If you don’t know about variables in Python, don’t worry at the end of this article you are able to deal with variables in Python

Before going through this tutorial, let’s see What is Variables in Python.

What is variables in Python?

In Python programming, a variable is just like a container that is able to store value to be used in the future. If you have any value and you want to use that value in the further program, then the variable is the best option to store.

Python is a dynamic type programming language that means you don’t need to specify the data type during the creation of variables. Python interpreter automatically detects the data type of variable, when you assign the value to the variable.

Let’s see types of data types in Python. Python support many types of standard data types.

Types of data type in Python :

Let’s see types of data types in Python. Python support many types of standard data types.

  • list
  • string
  • tuple
  • set
  • int
  • float
  • dictionary

Note:- If you want to check the data type of any variable, you can use type() type built-in python function.

Creating Python variables:

In Python programming, you don’t need to specify the data types during the creation of variables like c, c++, etc.
Python automatically detected data types of variables, when you assigned the values.

Example


x = 12
y = 12.23
name = 'Programming Funda'

print(x)
print(y)
print(name)

print(type(x))
print(type(y))
print(type(name))

You can change the data type of any variable.

Example


x = 23
print(type(x))
x = 'programming Funda'
print(type(x))

Variables Names:

A Python variable can have short names ( like x and y ) or more descriptive names like the name. To create Python variables, you should have followed some rules.

  • A variable name always starts with a letter and an underscore character.
  • A variable can not start with a number
  • A variable name can only contain alphanumeric characters and underscore ( A-z, 0-9, and _ ).
  • A Python variable is case sensitive which means name, Name, NAME, and naMe are different variables.

Valid Python Variable


name = 'Programming Funda'
roll_no  = 120
Score = 12300

Unvalid Python Variable

12age = 12
roll-no = 120
my score = 12300

Assign value to multiple variables:

You can provide or assign value to multiple variables in a single line.

Example

x, y, z = 12, 'Programming Funda', 1200.89

You can also assign the same value to the multiple variables into the same line.


x = y = z = 'Programming Funda'
print(x)
print(y)
print(z)

Output Variables:

To get the value or output of a variable, use a python print statement. Python print is a built-in function.


x = 'Programming Funda'
print(x)

You can use the + operator in the print function to add variables values.

Example


x = 'Programmig'
y = 'Funda'
print(x + y)

If you try to add a string with numbers, python raises an error.

Example


x = 'Programmig'
y = 12
print(x + y)

Python Global Variables:

Variables that are created outside of a function are known as Python global variables.

Example


x = 'Programming Funda'
def printNow():
	print(x)

printNow()

Python local variables:

The variable that is created inside a function are known as Python local variables:

Example


def printNow():
	x = 'Programming Funda'
	print(x)

printNow()

If you create a variable with the same name inside the function, then it will treat as a local variable and can be only used inside the function.

Example


x = 'Python Tutorial'
def printNow():
	x = 'Programming Funda'
	print(x)

printNow()
print(x)

The global keyword:

Normally, you can create a variable name inside a function, That variable is local. It is only used inside the function. To create a global variable name inside a function, use the global keyword.

Example


def printNow():
	global x
	x = 'Programming Funda'
	print(x)

printNow()
print(x)

Conclusion

So, in this tutorial you have learned variables in Python and how can use Python variables. I hope you don’t have any confusion regarding the Python variable and its uses.
Variables are the most important part of any programming language. Sometimes you want to use the variable value in different places through the program, Then variables play the most important role.

I hope this Python variables tutorial will have helped you to use variables in the Python program. If you like this tutorial pls share it with someone who wants to learn Python in an easy way.

Previous Article

Related Posts

Leave a Reply