Data types are the kind of data that is to be stored in the variable which is being used while writing a program. Python decides the data type of a variable during the execution of program by their syntax. For example string or character data type enclosed in a single or double quotes represents a string value. A list is represented by square brackets and tuples are enclosed in parantheses and curly brackets for a dictionary.
In Python, we don't need to declair a variable by mentioning it with its data type. The interpreter of Python automatically detects the type of data being entered. This is known as dynamic typing.
We can verify the type of the program used variable. The type() function in Python returns the type of the passed variable.
For example:
x = 10 y = "Welcome to Pyhon" z = 20.45 print(type(x)) print(type(y)) print(type(z)) |
---|
Output
<type 'int'> <type 'str'> <type 'float'> |
---|
- numeric
- dictionary
- sequence type
- boolean
- set
- int (integers): These are positive or negative whole numbers with no decimal point. For example: 45, -98 etc
- float (floating point real values): These are positive or negative with decimal point. For example: 0.2, -5.9 etc
- complex: A complex number appear in the form of m + ni where 'm' is the real part and 'n' is the imaginary part and 'i' represents the square root of '-1'.
x = {1:'apple', 2:'mango', 3:'grapes', 4:'pears'}
# Printing dictionary print (x) # Accesing value using keys print("1st fruit is "+x[1]) print("2nd fruit is "+ x[4]) print (x.keys()) print (x.values()) |
---|
{1: 'apple', 2: 'mango', 3: 'grapes', 4: 'pears'} 1st fruit is apple 2nd fruit is pears dict_keys([1, 2, 3, 4]) dict_values(['apple', 'mango', 'grapes', 'pears']) |
---|
# Python program to check the boolean type
a = 8 b = 6 print(a>b) print(a<b) |
---|
True
False |
---|
print(type(True))
print(type(False)) print(false) |
---|
<class 'bool'> <class 'bool'> NameError: name 'false' is not defined |
---|
# Creating Empty set
set1 = set() set2 = {'Bharat', 2, 3,'Python'} #Printing Set value print(set2) # Adding element to the set set2.add(100) print(set2) #Removing element from the set set2.remove(2) print(set2) |
---|
{'Bharat', 'Python',3 , 2}
{'Python', 'Bharat', 3, 2, 100} {3, 'Python', 'Bharat', 100} |
---|
- String
- List and
- Tuple
# String using double quotes
s = "Hi Python" # String using triple quotes s1 = '''Hello world How are you?''' print(s) print(s1) |
---|
Hi Python
Hello world How are you? |
---|
Now see another program to handle the string data types in different ways:
s1 = 'Hello world'
s2 = ' How are you?' print (s2[3]) #printing 3rd character of the string print (s1[0:3]) #printing first three character using slice operator print (s1*2) #printing the string twice print (s1 + s2) #printing the concatenation of s1 and s2 |
---|
w
Hel Hello worldHello world Hello world How are you? |
---|
list1 = [1, 2, 3, "Hello", "world", 4] #Checking type of given list print(type(list1)) #Printing the list1 print (list1) # List slicing print (list1[3:]) # List slicing print (list1[0:2]) # List Concatenation using + operator print (list1 + list1) # List repetation using * operator print (list1 * 3) |
---|
Output
<class 'list'> [1, 2, 3, "Hello", "world", 4] ['Hello', 'world', 4] [1, 2] [1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4] [1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4, 1, 2, 3, 'Hello', 'world', 4] |
---|
tup = (2,4, "Hello", "Python", 6) # Checking type of tup print (type(tup)) #Printing the tuple print (tup) # Tuple slicing print (tup[1:]) print (tup[0:1]) # Tuple concatenation using + operator print (tup + tup) # Tuple repatation using * operator print (tup * 3) |
---|
<class 'tuple'> (2, 4, 'Hello', 'Python', 6) (4, 'Hello', 'Python', 6) (2,) (2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6) (2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6, 2, 4, 'Hello', 'Python', 6) |
---|