Python Tutorial - Python Variables and Types

Introduction

When you create variable in python or in any other languages this means you reserve a memory location to store the value. The value stored in a variable can be accessed or updated later. The interpreter allocates memory on the basis of data type of a variable.

Python Variable Name Rules

  • The start character can be the underscore "_" or a capital or lower case letter.
  • Other characters can be letters, numbers or _
  • Case Sensitive.
  • Can be any length.
  • Can't use any Python reserved words as  a variable name.

Python Data Type

  • String.
  • Integer.
  • Float number.
  • Boolean.
  • Tuples.
  • List.
  • Dictionaries.
example:

Item_Name = "PC RAM" #A string
Item_Quantity = 10   #An integer assignment
Item_Price = 49.95   #A floating point
Item_Valid = True    #A Boolean
Item_Colors = ('Black','Blue','Red','White') #A Tuples
Item_List = ['RAM2G','RAM4G','RAM8G','RAM16G'] #A List
Item_Location = {"State":'NY', "City":'New York', "Address":'12405  ACRA'} #A Dictionaries
Print some variables in last example:

print (Item_Name,Item_Quantity,Item_Price) #print all variables

#==================================== Result ===============
('Dell Laptop', 10, 49.95)

Multiple Assignment

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

Item_Name, Item_Quantity, Item_Price  = "PC RAM", 10, 49.95
print (Item_Name,Item_Quantity,Item_Price) #print all variables

#==================================== Result ===============
('Dell Laptop', 10, 49.95)

v1 = v2 = v3 = v4 = 10
print (v1, v2, v3, v4) #print all variables

#==================================== Result ===============
(10, 10, 10, 10)
All variables in the last example are pointing to the same memory location which contain the value 10, we can print that memory location.

print id(v1)

#==================================== Result ===============
10085612








If this post was good and helpful for you, Please give it Like.
.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment