Variable is used to stored data value in programming language. Data may be anything String, Integers, float etc. In python programming variable act like container to store value.
How to Declare & use Variable?
Declaring variable in python is very simple. See below example how you can declare and use variable.
x=”hello Programiz” Print(x)
In the above example we declare variable x and we used variable by printing x.
When we declare variable than it reserved some memory location based on data types. It is similar like container for storing data values. In python there is no command for declaring variable when we assign value to variable it automatically declare.
How to Redeclare variable in python
In the above example first we assign x=6 than x become integer data types and then when we assign x=”sam” in next line now x become string data types means based on value it will automatically change it’s data types.
Cast variable in python
You can also cast data types of variables in python for that you need to used some in-built casting function in python. For casting variable in Strings, Integer, Float we can use str(), int(), float() method of python. Similarly there are so many methods for casting data types.
str(3); int(3); float(3);
Get type of variable
Sometime you don’t know the data type of variable at that time you can use type() method of python. type() method simply used for getting type of variables.
x=5 y=”john” Print(type(x))
How to assign multiple value to variable?
Assigning multiple value to variable is very easy in python programming. See the below Example
a,b,c=1,2,3 print(a) print(b) print(c)
You can also assign same value to all variable.
a,b,c=1 print(a) print(b) print(c)
Constant variable in python
Constant is type of variable whose value is fixed, ones you assigned value you can’t change it later, it will show error.
Create constant.py file
Create main.py file
import constant Print(constant.PI)
Rules and naming convention of Variable
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Types of Variables
There are two types of variables in python
- Global Variable
The variable which are declare outside of function and access anywhere in code means inside or outside of function is called as Global Variable.
Global variable has a global scope in program. Create a variable outside of a function, and use it inside the function
x = "Best" def myfunc(): print("Python is " + x) myfunc()
- Local Variable
The variable which are declare inside of function and have local access in program is called as Local Variable.
def localvar(): y = "local varible" print(y) localvar()
you cannot use y variable outside of function.
Good to Know?
- String variables can declare by single or double quotes
- Variable name is case sensitive
- You can re-declare a variable in python
- Use del keyword for deleting variable in python