VARIABLE TYPES:
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.
A Variable in python is created as soon as a value is assigned to it. I
t does not need any additional commands to declare a variable in python.
There are a certain rules and regulations we have to follow while writing a variable, lets take a look at the variable definition and declaration to understand how we declare a variable in python.
RULES:
Python has no additional commands to declare a variable. As soon as the value is assigned to it, the variable is declared.
1 2 |
x = 10 #variable is declared as the value 10 is assigned to it. |
There are a certain rules that we have to keep in mind while declaring a variable:
1.The variable name cannot start with a number. It can only start with a character or an underscore.
Variables in python are case sensitive.
2.They can only contain alpha-numeric characters and underscores.
3.No special characters are allowed.
Assigning Values to Variables:
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.
For example –
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
output:
100
1000.0
“John”
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously. For example −
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example −
a,b,c = 1,2,"john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "john" is assigned to the variable c.
Comments
Post a Comment