Data Types in Python (with code snippets)

·

3 min read

In Python, every value has a data type. There are various data types and in this thread we will discuss some of the important ones.

  1. Python Numbers

    Numbers are three types

    1. Integers
    2. floating point numbers
    3. complex numbers

      # integer 
      age = 30
      
      # float 
      interest_rate = 10.99
      
      # complex numbers 
      point = 10 + 20j
      
  2. List

    Python list is ordered sequence of items. We can store more than one item of different types in a list. Items in the list are accessed using index.

    
     # create a list of 3 items 
     student = ['Afiz', 'Hyderabad', 5600032]
    
     # accessing items using index. Index starts with 0
     print(student[0]) # output -> Afiz 
     print(student[1]) # output -> Hyderabad
    
     # Adding more items to list 
     student.append('India')
     print(student)
     # output: ['Afiz', 'Hyderabad', 5600032, India]  
    
     # Update list item 
     student[1] = 'Kadapa'
    
  3. Tuple

    Tuple is ordered sequence of items same as list. The difference is that tuples are immutable and list is mutable.

     # tuple is created using ()
     ip_address = ('192.1.1.0', '192.1.1.1', '192.1.1.2')
    
     # access the first item 
     print(ip_address[0]) # output -> 192.1.1.0
    
     # updating is allowed ❌
    
     # adding new itesm not allowed ❌
    
  4. Set

    Set is an unordered collection of unique items, which means duplicates are not allowed. Set is defined using {} and items in set are separated by commas. set items are not ordered

     # define set 
     prime_numbers = {2,3,5,7,11}
    
     # We can't access set items using index as it is unordered
     # However, pop method can used to pop the items. 
     print(prime_numbers.pop()) # output -> 2
     print(prime_numbers.pop()) # output -> 3
    
     # Add items to set 
     prime_numbers.add(13)
    
     print(prime_numbers) # {5, 7, 11, 13}
    

    Set items can’t be accessed using index as they are unordered.

     print(prime_numbers[1]) # output 👇 
    
     ---------------------------------------------------------------------------
     TypeError      Traceback (most recent call last)
     <ipython-input-7-13b8bce338c4> in <cell line: 1>()
     ----> 1 prime_numbers[1]
    
     TypeError: 'set' object is not subscriptable
    
  5. Dictionary

    Dictionary is an unordered collection of key-value pairs. Dictionary is defined using {} and each item being a pair of key and value. Example 👇

     # define a dictionary called student with name and city as items 
     student = {'name': 'Afiz', 'city': 'Banglore'}
    
     # accesing items: key is used to access items 
     print(student['name']) # 'Afiz'
    
     # Adding a new item 
     student['country'] = 'India' 
    
     # if the country already exists,it will updated with new value 👇
     student['city'] = 'Hyderabad'
    
     print(student) # {'name': 'Afiz', 'city': 'Hyderabad', 'country': 'India'}
    
  6. Strings

    String is a sequence of characters. In Python single, double, and triple quotes are used to define strings. Example 👇

     # defining strings 
     sentence = 'Hello World!'
    
     sentence_1 = "Hello World!" 
    
     sentence_2 = '''Hello World1''' 
    
     # There is difference between single, double and triple quote strings. 
     # single quotes comes handy when you want to have double quotes in the string.
     # like wise for double and single
    
     new_sentence = 'That is a "GREAT" news' 
     new_sentence_1 = "That's a great news"
    
     # Triple quotes are used to define multi line strings.
     big_sentence = ''' This is going to be multi line string
     second line of the string'''