Python Variables: These are nothing but they are known as reserved memory locations to store values. (or) assigning storage space for your variables. And these are based upon the data type of the variable, interpreter allocates the memory and decides where it can be stored. And Python is not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.
Python Variables
- Python is completely object-oriented and dynamically typed. So, you don’t need to declare variables before using them or declare their types.
- While Each and every variable in python refers to an object.
- Python variables are the storage place holders for texts and numbers. And also it must have a name so, that you will be able to find it again.
- All these variables are always assigned with an equal sign, followed by the values of the variable.
- Since white spaces & signs with special meanings in Python, as “+” and “-” are not allowed.
- And you should also remember that variable names are case sensitive.
- Although you can also use any letter, the special character “_” and every number provided by you should not start with it.
Rules for Creating Variables in Python
- name must start with a letter or the underscore character.
- And a name cannot start with a number.
- It can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- The names are case-sensitive (name, Name, and NAME are three different variables).
- The reserved words(keywords) cannot be used naming the variable.
Example:
counter=100#An integer assignment miles=2000.0#A floating point value name="freshersnow"#A string print counter print miles print name
Here 100, 2000.0 and “freshersnow” are the values which are assigned to counter, miles and name variables.
Output:
100 2000.0 freshersnow
Multiple Assignments
Multiple assignments are also known as “tuple unpacking or iterable unpacking”. While these assignments allow you to assign multiple variables at the same time in one line of code.
- Although python allows programmers to assign a single value to multiple variables simultaneously.Ex:x=y=z= 1
- While an integer value can be assigned as 1 and all the three variables are assigned to the same memory location.
- You can also assign multiple objects to multiple variables. Ex: x,y,z = 1, 2, “freshersnow”
While these two integer objects are with values 1,2 are assigned to x, y and one string value “freshersnow” is now assigned to the variable z.
Scope of Variables
The scope of variables can be defined as a portion of the program, where we can access a particular identifier. There are two types of scopes available in Python. They are 1) Global variables 2) Local Variables
1) Global Variables: Variables which are declared outside of the variables are called Global variables. Global variables can be accessed throughout all functions.
2) Local Variables: These variables are declared inside the function body having a local scope are called Local Variables. All these local variables can be accessed only inside the function in which they are declared.
Example:
total=0;#This is a global variable. def sum(arg1,arg2): total=arg1 + arg2;#Here total is a local variable. print "Inside the function local total: ", total return total; sum (10, 20); print "Outside the function global total: ", total
Output:
Inside the function local total: 30
Outside the function global total: 0