***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Tuesday, April 27, 2021

Python - Variable

Variables are nothing
but reserved memory locations to store values. Based on the data type of a
variable, the interpreter allocates memory.



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.



Example:



counter = 100          # An integer assignment



miles   = 1000.0       # A floating point



name    = "John"       # A string



Python allows you to
assign a single value to several variables simultaneously.



a = b = c = 1



Multiple objects can be assigned to multiple variables in Python.



a,b,c =
1,2,"Ashraf"



Here, two integer
objects with values 1 and 2 are assigned to variables a and b respectively, and
one string object with the value "Ashraf" is assigned to the variable
c.



Variable Naming Rules



Rules to be followed
for naming variables::



• Python’s key words
cannot be used as a variable name.



• A variable name
cannot contain spaces.



• The first character
must be one of the letters a through z, A through Z, or an underscore



character (_).



• After the first
character you may use the letters a through z or A through Z, the digits 0



through 9, or
underscores.



• Uppercase and
lowercase characters are distinct. This means the variable name ItemsOrdered is
not the same as itemsordered.



A variable in Python
can refer to items of any type. After a variable has been assigned an item of
one type, it can be reassigned an item of a different type.

No comments:

Post a Comment