Variables and data types

What is a variable ?

  • Variable is a name which is used as a pointer to data that is stored on computer memory.
  • In python, variables are called as names.
message = "Hello World"
print(message)
# Output: Hello World
my_var = "Hi"
print(my_var)
# Output: Hi
print(your_var)
# Output:
# NameError: name 'your_var' is not defined
  • In above python program we have stored the message "Hello World" in the variable message.
  • Actually it is stored in computer memory and variable message is a pointer to the data "Hello World".
  • In above program when we print "your_var" we got an error.
  • Because the the variable is not defined.
  • Instead of naming the variable as "message" we can use anything like "my_var", "your_var", etc.
  • But, it's not a good practice to give random names. We should follow some rule to name variables in python.

Note: Use variable/names only if you use it more than once

How to name a Variable?

  • variable name should not contain a space
  • variable name should starts with a letter or an underscore(_)
  • special symbols (~, !, @, #, $, etc.) are not allowed in variable name.
  • variable should be short and meaningful as per the data stored in variable.
  • If we want to use multiple words in a variable we can use underscore(_) to separate the words. Example variables "student_name", "account_number", "phone_num1", "phone_num2", etc.
  • Python is case sensitive language so, it treats the lowercase letters and upper case letters differently. For example variable "Student" is different from the variable "student".
  • We should not use python reserved variables/names as varibles though we are allowed to use it.
  • It is better practice to use lowercase letters instead of uppercase letters.

What is a datatype in python ?

  • Datatype is a classification of data which tells the computer that how the programer wants to use the data. For example the data "hello" is different from the data 255.
  • Data "hello" is of type string and 255 is of type int in python.

  • In python, we can classify the data types into two.

    1. Immutable data types
    2. Mutable data types

Immutable data types

Immutable data type is defined as once we declare the data of a datatype then it will not allow us to modify it. The following data types are immutable data types in python.

  • bool - Boolean data type

    is_number = True
    is_valid = False
    
  • int - Integer data type

    num1 = 100
    num2 = 200
    
  • float - Float data type

    num1 = 100.0
    num2 = 220.5
    
  • str - String data type

    first_name = "Anji"
    last_name = "B"
    
  • tuple - Tuple data type

    numbers = (1,2,3,4,5)
    
  • frozenset - Immutable set data type

    unique_numbers = frozenset(1,2,3,4,5)
    

Mutable data types

Mutable data typeis defined as it is a data type in which we can modify the data whenever we want. It's simply opposite to the immutable data type. The following data types are mutable data types in python.

  • list - List data type or Array data type

    data = ["Apple", 123.55, False]
    
  • set - Set data type

    numbers_set = {1,2,3,4,5}
    
  • dict - Dictionary data type [i.e key value map]

    days = {"Sun": 1, "Mon": 2, "Tues": 3}
    
  • In python we can also define our own data types as well.

  • The above mentioned data types are some of the built-in data types provided by the python.