Skip to content

Variables & Data Types in Python

Variables

Variables are names that given to data that we need to store and manipulate. Python is not ” statically type” i.e. no need to declare variables or their data types before using them. A variables is created the moment we first assign a value to it.

#!/usr/bin/python

age = 24          # An integer assignment
salary   = 10000.0       # A floating point
name    = "John"       # A string

print age
print salary
print name

Here, 24 , 10000.0 and “John” are the values assigned to age, salary, and name variables, respectively. This produces the following result

24
10000.0
John

Assigning different values to multiple variables

We can also declare different variables in a single line that is separated by comma (,)

name,age,salary = "John",24,10000.0 

Using same name for different data types

We can also assign  a single value for more than one variable

a= "John"
a= 10000.0

print(a)

This produce the following result −

a= 10000.0

Deleting a variable

We can delete a variable using del statement. Let’s have a look at a simple example to delete a python variable.

count = 100
 print(count)
100
del count
print(count)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'count' is not defined

Rules for creating variables is as same as in other high level language

  1. A variable name can contain small case letters (a-z), upper case letters (A-Z), numbers (0-9), and underscore (_).
  2. A variable name can’t start with a number.
  3. We can’t use reserved keywords as a variable name.
  4. A variable can’t contain only digits.
  5. A python variable name can start with an underscore or a letter.
  6. The variable names are case sensitive.
  7. There is no limit on the length of the variable name.

DataTypes in Python

Data types are declarations for variables. This determines the type and size of data associated with variables. A data type, in programming, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. For example, string is a data type that is used to classify text and an integer is a data type used to classify whole numbers.

Numeric Type

This data type represent the data which has numeric value. Numeric value can be interger, floating number or even complex numbers. These values arre defined as intfloat and complex

  • Intergers – This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be.
  • Float – This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
  • Complex Numbers – Complex number is represented by complex class.
    a = 5
    
    print("Type of a: ", type(a)) 
    
      #type() function is used to determine the type of data type
    
    b = 5.0
    
    print("\nType of b: ", type(b)) 
    
      
    
    c = 1 + 2j
    
    print("\nType of c: ", type(c)) 
    
    Output:
    
    Type of a:  <class 'int'>
    
    Type of b:  <class 'float'>
    
    Type of c:  <class 'complex'>

    Sequence Type

A sequence is an ordered collection of similar or different data types. Python has the following built-in sequence data types:

  • String: A string value is a collection of one or more characters put in single, double or triple quotes.Strings are defined either with a single quote or a double quotes.The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)
    mystring = "Hello World"
    
    print(mystring)
    
    Output:
    Hello World
  • List : A list object is an ordered collection of one or more data items, not necessarily of the same type, put in square brackets. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility. It is represented by list class
    List = []  
    
    print("Blank List: ")  
    
    print(List)  
    
        
    # Creating a List with the use of string  
     
    
    List = ['I like an apple']  
    
    print("\nList with the use of String: ")  
    
    print(List)  
    
        
    # Creating a List with the use of multiple values  
    
    List = ["I", "am", "007"]  
    
    print("\nList containing multiple values: ")  
    
    print(List[0])   
    
    print(List[2])  
    
        
    # Creating a Multi-Dimensional List By Nesting a list inside a List)  
    
    List = [['I', 'am'], ['007']]  
    
    print("\nMulti-Dimensional List: ")  
    
    print(List)  
    Output:
    
    Blank List: 
    []
    
    List with the use of String: 
    ['I like an apple']
    
    List containing multiple values: 
    I
    007
    
    Multi-Dimensional List: 
    [['I', 'am'], ['007']]
  • Tuple: A Tuple object is an ordered collection of one or more data items, not necessarily of the same type, put in parentheses. Tuples are created by placing sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of data sequence. Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple.
        
    # Creating an empty tuple  
    
    Tuple1 = ()  
    
    print("Empty Tuple: ")  
    
    print (Tuple1)  
    
        
    # Creating a Tuple with the use of Strings  
    
    Tuple1 = ('Grapes', 'Apple')  
    
    print("\nTuple with the use of String: ")  
    
    print(Tuple1)  
    
        
    # Creating a Tuple with the use of list  
    
    list1 = [1, 2, 4, 5, 6]  
    
    print("\nTuple using List: ")  
    
    print(tuple(list1))  
    
    
    # Creating a Tuple with nested tuples  
    
    Tuple1 = (0, 1, 2, 3)  
    
    Tuple2 = ('Apple', 'Mango')  
    
    Tuple3 = (Tuple1, Tuple2)  
    
    print("\nTuple with nested tuples: ")  
    
    print(Tuple3)  
    Output:
    
    Empty Tuple: 
    ()
    
    Tuple with the use of String: 
    ('Grapes', 'Apple')
    
    Tuple using List: 
    (1, 2, 4, 5, 6)
    
    
    Tuple with nested tuples: 
    ((0, 1, 2, 3), ('Apple', 'Mango')

Conclusion

Unlike popular language like C, C++, C# or Java, Python variable doesn’t require declaration. Same variable can be used for storing different types of data. It has all the data types that other programming language offers like integer, float, complex, characters and collections of them as strings, list and tuples.

Leave a Reply

Your email address will not be published. Required fields are marked *