Examples of data types in Python Programming
All Contributions
Python Dictionary :
Dictionary is an unordered collection of key-value pairs.
Example :
>>> d = {1:'value','key':2}
>>> type(d)
<class 'dict'>
Another example :
d = {1:'value','key':2}
print(type(d))
print("d[1] = ", d[1])
print("d['key'] = ", d['key'])
# Generates error
print("d[2] = ", d[2])
Python Strings :
String is sequence of Unicode characters
Example :
s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
Another example :
s = 'Hello world!'
# s[4] = 'o'
print("s[4] = ", s[4])
# s[6:11] = 'world'
print("s[6:11] = ", s[6:11])
# Generates error
# Strings are immutable in Python
s[5] ='d'
Python Tuple :
Tuple is an ordered sequence of items same as a list.
Example :
t = (5,'program', 1+3j)
# t[1] = 'program'
print("t[1] = ", t[1])
# t[0:3] = (5, 'program', (1+3j))
print("t[0:3] = ", t[0:3])
# Generates error
# Tuples are immutable
t[0] = 10
total contributions (6)
Conversion between data types :
We can convert between different data types by using different type conversion functions like int(), float(), str()
Example :
Another example :
Another example :
We can even convert one sequence to another :
To convert to dictionary, each element must be a pair: