chevron_left
105 points
10 5 2

Python Data Types

posted Aug 3 3 min read

Python has variety of variables . This include :

Python numberinteger  → python usually has no maximum and minimum values of integer

      → to type cast you use int() method i.e int(“23”)

         float 

      → This are integers but have decimal value in them.

      → On printing you can chose the number of decimal places you want to printed

        complex 

      → This data type is used in scientific calculations and geometry majorly.

      → You can type integer and all python does is add j to the end of it i.e 2+3j or 3+0j

Python string 

     → String in python can either be between single quote ‘’ or double quote “”.

      → You can also sorround the strong with “”” “”” (triple quotes)

Boolean

     → This takes the value True of False or O or 1

     → can be used to handle excemptions looping through endless untill condition met.

Python List

     → list can have any data type in them i.e  string bool int and float. List can also have other list in them which is called ‘nested list’. To access values of a list you access them just as any array of any programming starting from 0.

     → Operations in list include popping through pop(), appending through append(), extending through extend(), remove through remove(), inserting through insert(), slicing a list through slice() and min(), max() to get the minimum and maximum value.

Example → list_addr = [‘Name’,1278,True]

python Dict

     → Dictionary is used to store values in key:value pair. One can access a value by providing the key. Values in a dictionary can be of any type including list,set,tuple can used.

Python Set

     → Just like list and tuple set can be used to store different data in one variable. It can declared using the  set() or using an empty set {}. For example set_py = set() or set_py = {}.

      Python tuple

    → Unlike list or set tuple uses ()

    → Most features remain same as list or set.

→ From the example below we see the difference between set list and tuple is the enclosing that assigns the values to a variable. For a list is [], for set is {} and for a tuple is ().
 
→ To know the length of value in a dictionary, set, list or tuple you use len() function which takes the variable. For example list = [1,23,4,5,6,7,8,9]
         print(len(list))
  1. ###################################################
    #     Numeric variables
    ###################################################
    
    #integer 
    int_var_smallest =0
    int_var_largest = 1223782734736473463875683562897023792358926988
    
    print("Integer smallest value {} and Integer largest value {}".format(int_var_smallest, int_var_largest))
    
    #float
    float_var = 34843.349648684684756893475934759634
    print(" 2 Decimal float : %.2f  \n 5 Decimal float : %.5f \n   9 Decimal float : %.9f" % (float_var, float_var, float_var ))
    
    
    #complex data 
    a = -1+5j
    print("Complex value : {}".format(a))
    
    #############################################
    #   String Variable
    ############################################
    
    string = " Python is cool "
    string_2 = """ Python has been on demand making java obsolete. """
    string kodlog = """ 
            __
            |  |
            |  |                  _________    ___________
            |  |                  |  |         |  |    |  |            |  |
            |  |           __   |  |         |  |    |  |            |  |
            |  |           |  |   |  |         |  |    |  |            |  |
            |_|______|_|   |_|_____|_|    |_|______ |   |
                                                                      |   |  
                                                                      |   |
                                                       |  |           |   | 
                                                       |_|______|__|
    """
    
    #############################################
    #   Python list
    ############################################
    
    list_str = ["name","string","hi"]
    list_int = [2,3,4,5]
    list_bool = [True,False]
    
    list_all = [" Name ",233234,34394.34, True, 4+3j]
    
    print(list_all)
    print(list_all[3])
    for them in list_all:
        print(them)                                  # will print them 
        print(type(them))                        # you get all the types of the data
    
    list_log = ['k','o','d','l','o','g','s']
    print(list_log[1:3])                             # print from 2nd to 4th elements
    print(list_log[3:])                               # print from 4th element to last element
    print(list_log[:])                                 # print all
    
    #############################################
    #   Python Dict
    ############################################
    car = dict()
    car = {
    "Model" : " Subaru"
    }
    customer = { 
    "Name":"G I Joe",
    "Year":"1984"
    }
    
    print(car + "  "+ customer)                                       #You can print the dictionary as a whole and both the keys and values will be shown
    print(car['Model'] + "   "+ customer['Name'])        # Or you can just specify the key name and value will be shown
    
    #############################################
    #   Python tuple
    ############################################
    list_str = ("name","string","hi")
    list_int = (2,3,4,5)
    list_bool = (True,False)
    
    list_all = (" Name ",233234,34394.34, True, 4+3j)
    
    print(list_all)
    print(list_all[3])
    for them in list_all:
        print(them)                                  # will print them 
        print(type(them))                        # you get all the types of the d
    
    
    
    #############################################
    #   Python Set
    ############################################
    variable = set()                                                  #  you can also create a set variable by its constructor name
    
    variable = set((12,"data",12+2j))
    
    list_str = {"name","string","hi"}
    list_int = {2,3,4,5}
    list_bool = {True,False}
    
    list_all = {" Name ",233234,34394.34, True, 4+3j}
    
    print(list_all)
    print(list_all[3])
    for them in list_all:
        print(them)                                  # will print them 
        print(type(them))                        # you get all the types of the d
    
    
    
    
    
    

More Posts

which of the following data types is not supported in python Tania - Aug 14
Which of the following data types is not supported in python usmanhashmi - Aug 4
in python a comma-separated sequence of data items that are enclosed in a set of brackets is called SharadMagar450 - Aug 11
Introduction to machine learning with python: a guide for data scientists Tasnia Sultana - May 27
Programming data structures and algorithms using python Sanjana Sagar - May 19
Data Structure and Algorithmic Thinking with Python by Narasimha Karumanchi pdf Tasnia Sultana - May 7
how to install python pip on windows ? videepsinghal - 1 day
If else in one line python sam_dev - 2 days
invalid syntax python for no reason sam_dev - 3 days
Python ecommerce | build a django ecommerce web application. Tania - Sep 14