Menu Close
Python Variables

Python Variables

In this tutorial, we are going to learn about Python variables and also will we 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 Python variables, don’t worry at the end of this article you are able to deal with python variables.

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

What is Python variables ?

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 single line.

Example

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

You can aslo assign same value to the multiple variable into 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 python print statement. Python print is a built-in function.

x = 'Programming Funda'
print(x)

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

Example

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

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

Example

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

Python Global Variables:

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

Example

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

printNow()

Python local variables:

Variable that are created inside a function are know 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 Python variables and how can use Python variables. I hope you don’t have any confusion regarding the Python variable and it uses.
Variables are the most important part of any programming language. Sometimes you want to use the variable value on 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 your who wants to learn Python from an easy way.

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply