Python Variables
Variables are identifiers of physical memory location, which is used to hold values temporarily during program execution.
Python interpreter allocates memory based on the values data type of variable, different data types like integers, decimals, characters etc can be store in these variables.
Values
Before learning about variables, you must know about values.
A value is one of the basic parts of a program like a letter or a number.
The examples of such values can be:
Value | Data Type |
---|---|
5, 9 | integers |
Hello, Ok | string (combination of letters) |
Assigning Values to Variables
Python interpreter is able to determine that what type of data are stored, so before assign a value, variables does not need to be declared.
Usually in all programming languages, equal sign “=” is used to assign values to a variable. Its assigns the values of right side operand to left side operand.
The left side operand of = operator is the name of variable, and right side operand is value.
Example:
#!/usr/bin/python
name = "Packing box" # A string
height = 10 # An integer assignment
width = 20.5 # A floating point
print name
print height
print width
Output:
Packing box
10
20.5
In the above code snippet, the variable name ‘height’ is storing a value 10 and since the value is of type integer, the variable is automatically assigned the type integer.
Another variable name ‘width’ is assigned with floating type value. Then both the values are printed or displayed using the ‘print’ statement.
Another variable name ‘width’ is assigned with floating type value. Then both the values are printed or displayed using the ‘print’ statement.
Comments
Post a Comment