Python Data Types Msbte and Data Types In Python MSBTE

Python Data Types Msbte || Data Types In Python MSBTE

Python Data Types Msbte and Data Types In Python MSBTE


    The programmers from C or Core Java background are already familiar about data type and its purpose.  Python variables/references don't require any specific declaration using data type. Based on assigned value, Python will automatically assign data type to its variables/references.

    Python has five built-in primary data types :-
    1. Numbers 
    2. String
    3. List
    4. Tuple
    5. Dictionary

    1. Python Numbers 

    • The Number data types are to store numeric values. Like other programming languages, Python have four different types in Numbers.
      1. int (signed integers) 
      2. long (long integers,can be a octal and hexadecimal)
      3. float (floating point real values)
      4. complex (complex numbers)
    • The int is the most preferred and commonly used data type. It stores signed numeric values without decimal point.The long stores signed long integers. Internally, they can also represent Octal or Hexa Decimal values.Python displays long integers with an uppercase L. But it is no more available in Python 3.xx. The boat stores the signed floating point real values. And the complex number consists of an ordered pair of real floating-point numbers denoted by x + y), where x and y are the real numbers and is the imaginary unit. Refer following examples
                   

    • In the assignment statements, h = 0x421F5 the x represents hexa-decimal number and in 1 = 0o14512 the o represents an Octal number. Similarly, we can use b to represent binary number. Try executing following statements.
        1. numl = 0x4S21
        2. print("numl makes :",num1)
        3. num2 = 0o4521
        4. print("num2 makes :",num2)
        5. num3 = 0b10111010001
        6. print("num3 makes ",num3)

    • Such numbers must start with 0 (zero) followed by indicating character x or o or b.

    2. Python String

    • In Python, a String is set of contiguous set of characters represented in single or double quotes. Python allows three operators to work for a String.The slicing operator ( [] or [:]) that retrieves sub-string; based on index number that starts from 0. (Python also allows backward string index where last character of String has index -1, second last character at -2, and so on.). The string concatenation operator (+) to concatenate/join Strings. And the repetition operator that repeat the left operand String according to right operand numeric value.

    Example :
    1. strings="Welcome To Cwpedia"
    2.  
    3. print(strings) #Print complete string                             
    4. print(strings[0]) #Prints first character (at Oth index) of the string 
    5. print(strings[0:11]) #Prints character starting from 0th to 11th index
    6. print(strings[11:]) #print string starting from 11th index 
    7. print(strings * 3) #Prints string three times 
    8. print(strings + " Website.")#Prints concatenated string 
    9. print(strings[-1]) #Prints last character of string
    10. print(strings[::-1]) #Prints A Reverse String

    output:-
    1. Welcome To Cwpedia
    2. W
    3. Welcome To 
    4. Cwpedia
    5. Welcome To CwpediaWelcome To CwpediaWelcome To Cwpedia
    6. Welcome To Cwpedia Website.
    7. a
    8. aidepwC oT emocleW
    • One more thing, in Python also strings are immutable i.e other variables and references . Strings will also not change in itself, But as a result it will always produce a new String, whereas original string remains unchanged.

    3. Python List

    • The list are most dynamic and most used data type that are represented in square braces). While dealing with list, it looks similar to Array But it is not. Array is static, whereas list is dynamic in size. Array has fixed datatype for all its elements, whereas list has no data type bounding. For example, 
    • list1=[1,22,"Rahul",23.2,"Cwpedia"]
    • list2=[25,"Python"]
    • We can also use all three operators for list that we used on String data type.
    Example:
    1. list1=[1,22,"Rahul",23.2,"Cwpedia"]
    2. list2=[25,"Python"]
    3.  
    4. print(list1) #Print complete list1                             
    5. print(list1[0]) #Prints first element (at 0th index) of the list1 
    6. print(list1[0:2]) #Prints element starting from 0th to 2nd index
    7. print(list1[1:]) #print list1 starting from 1st index 
    8. print(list1 * 3) #Prints list1 three times 
    9. print(list1 + list2) #Prints concatenated lists 
    10. print(list1[-1]) #Prints last element of list1
    11. print(list1[::-1]) #Prints A Reverse list1

    output:-
    1. [1, 22, 'Rahul', 23.2, 'Cwpedia']
    2. 1
    3. [1, 22]
    4. [22, 'Rahul', 23.2, 'Cwpedia']
    5. [1, 22, 'Rahul', 23.2, 'Cwpedia', 1, 22, 'Rahul', 23.2, 'Cwpedia', 1, 22, 'Rahul', 23.2, 'Cwpedia']
    6. [1, 22, 'Rahul', 23.2, 'Cwpedia', 25, 'Python']
    7. Cwpedia
    8. ['Cwpedia', 23.2, 'Rahul', 22, 1]

    4. Python Tuple

    • A Tuple is another data type which stores sequence of values/objects, similar to List. A Tuple is also a set of values separated by comma(,)enclosed in parenthesis ().
    • For example, tup1=(1,22,"Rahul",23.2,"Cwpedia")
    • The main difference between Tuple and List are: List is enclosed in square braces ([]), whereas Tuple is enclosed in parenthesis (()). Another thing is, List allows change/update its size and elements, whereas Tuple does not allow updation in size or elements.A Tuple is readonly entity. A Tuple also allows all three operators that we used on String data type.
    Example:
    1. tup1=(1,22,"Rahul",23.2,"Cwpedia")
    2.  
    3. print(tup1) #Print complete tup1                             
    4. print(tup1[0]) #Prints first element (at Oth index) of the tup1 
    5. print(tup1[0:2]) #Prints element starting from 0th to 2nd index
    6. print(tup1[1:]) #print tup1 starting from 1st index 
    7. print(tup1 * 3) #Prints tup1 three times 
    8. print(tup1[-1]) #Prints last element of tup1
    9. print(tup1[::-1]) #Prints A Reverse tup1

    output:-
    1. (1, 22, 'Rahul', 23.2, 'Cwpedia')
    2. 1
    3. (1, 22)
    4. (22, 'Rahul', 23.2, 'Cwpedia')
    5. (1, 22, 'Rahul', 23.2, 'Cwpedia', 1, 22, 'Rahul', 23.2, 'Cwpedia', 1, 22, 'Rahul', 23.2, 'Cwpedia')
    6. Cwpedia
    7. ('Cwpedia', 23.2, 'Rahul', 22, 1)

    5. Python Dictionary

    • To work with List and Tuple and Arrays we have to remember the index numbers of element. The Python Dictionary solves this problem and allows us to create our own index numbers for our elements.
    • In Python, Dictionaries are like hash-table that has pair of an index(key) and element(value). Its syntax will be {key :value}
    • The data type of index (key) and element(value) can be any valid Python data type. A dictionary is represented in pair of curly braces ({}).
    • For example, dict1 ={1:"Cwpedia",2:"Python",3:"Programming",4:"MSBTE"}
    • Above dictionary shows the number[1,2.3,4] is behaving as key(index) and Strings["Cwpedia","Python","Programming","MSBTE"]is behaving as element(value) of that index. Similar to List, A Dictionary is also changeable/updatable. 

    Example:

    1. dict1 ={1:"Cwpedia",2:"Python",3:"Programming",4:"MSBTE"}
    2.  
    3. print(dict1)  #Print complete dict1  
    4. print(dict1.keys())  #Print all keys of dict1  
    5. print(dict1.values())  #Print all values of dict1  

    output:-
    1. {1'Cwpedia', 2'Python', 3'Programming', 4'MSBTE'}
    2. dict_keys([1, 2, 3, 4])
    3. dict_values(['Cwpedia', 'Python', 'Programming', 'MSBTE'])


    Post a Comment

    Previous Post Next Post