Let's Python with VIJIN (Part-4)
These are keywords which determine the type of data stored in a variable. Following table shows data types used in python:
Immutable data types: They are those that can never change their value in place.
Immutable types are:
· integer
· floating point numbers
· booleans
· strings
· types
Let us now try to execute few statements in interactive mode to determine the data type of the variable using built-in function type().
String
· String is a group of characters.
· These characters may be alphabets, digits or special characters including spaces.
· String values are enclosed either in single quotation marks (e.g., ‘Hello’) or in double
quotation marks (e.g., “Hello”).
· The quotes are not a part of the string, they are used to mark the beginning and end of
the string for the interpreter.
o >>> str1 = 'Hello Friend'
o >>> str2 = "452"
We cannot perform numerical operations on strings, even when the string contains a numeric value, as in str2.
o type('Hi')
<class 'str'>
Mutable data types:
They are their value can be changed in place
Immutable types are:
· Lists
· Dictionaries
· Sets
List:
· Lists are used to store multiple items in a single variable.
· List is a mutable datatype.
· Example:
>>>list1=[1,2,3,4,5]
>>>type(list1)
<class ‘list’>
>>>list2=[‘a’, ’e’, ’i’, ‘o’, ‘u’]
>>>list3=[‘Krupa’, 102, 79.5]
Tuple:
· Tuples are used to store multiple items in a single variable.
Recommended by LinkedIn
· Tuple is an immutable datatype.
· Example:
>>>tup1=(1,2,3,4,5)
>>>type(tup1e1)
<class ‘’tuple>
>>>tup2=(‘a’, ’e’, ’i’, ‘o’, ‘u’)
>>>tup3=(‘Krupa’, 102, 79.5)
Set:
· Sets are used to store multiple items in a single variable.
· Set is a mutable datatype.
· Duplicate elements are not allowed in a set.
· Example:
>>>set1={1,2,3,4}
>>>set2={‘a’, ’e’, ’i’, ‘o’, ‘u’}
>>>type(set1)
<class ‘set’>
>>>set3={‘Krupa’, 102, 79.5}
Dictionary
· Dictionaries are used to store data value in key:value pairs.
· Dictionary is a mutable datatype.
· Example:
>>>dict1={‘a’:1, ‘e’:2, ‘i’:3,’o’:4,’u’:5}
>>>type(dict1)
<class 'dict'>
>>>dict1[‘e’]
2
>>>student={
“regno”:123,
“name”: “ram”,
“class” : “PCMC”
}
None
· None is a special data type with a single value.
· It is used to signify the absence of value in a situation.
· None supports no special operations, and it is neither same as False nor 0 (zero).
>>> myVar = None
>>> print(type(myVar))
<class ‘None Type’>
>>> print(myVar)
None