Python Data Types
- Categories Python Basics, Python, Python
In this post we will cover Data Types in Python. You can watch our videos on Data Types in Python – Click Here
Q. 1 What is data type in python?
Ans.
- Data types are the classification of data items.
- It represents the value that states what operations can be performed on a specific data.
- As everything is an object in Python programming, data types are classes and variables are instance (object) of these classes.
- Following are the standard or built-in data type of Python:
- Numeric
- String
- Boolean
- List
- Tuple
- Set
- Dictionary
Q. 2 What is difference between mutable/immutable types in Python?
Ans:
Mutable Objects can change their state or contents e.g. list, Dictionary
Immutable Objects can’t be changed after it is created. These are of in-built types like int, float, bool, string, tuple.
Q. 3 What is Dynamic Typing?
Ans. A variable pointing to a value of certain type can be made to point to value /object of different type. This is called Dynamic Typing
E.g.
X = 20
print (X)
X = 20.5
print (X)
X = “Yes”
print (X)
Q. 4 What does the type() function return?
Ans. The type() function returns the type of the specified object.
a = ('apple', 'banana', 'cherry')
b = "Hello World"
c = 33
x = type(a)
y = type(b)
z = type(c)
a = type(True)
b = type ({1:2})
c = type(23.5)
Output:
<class 'tuple'>
<class 'str'>
<class 'int'>
<class 'bool'>
<class 'dict’>
<class 'float’>
Q. 5 How do you do type conversion in Python?
Ans. We use following functions to convert
- int() – To convert any string, float to int
- float() – To convert any number to float
- str() – To convert any number to string
- bool() – To convert any number to Python
- list() – to convert any other type to tuple.
- tuple() To convert any other sequence to tuple
- dict() – To convert any other sequence to dict
Q. 8 What is list in python?
Ans. A list contains items separated by commas and enclosed within square brackets ([]).
e.g.
L1 = [ ‘Roshani’, 546 , 3.20, ‘Priya’, 65.3 ]
L2 = [123, ‘jay’]
Q. 9 Write a short note on numeric data types.
Ans.
- Numeric data type represent the data which has numeric value.
- Numeric value can be integer, floating number or even complex numbers.
- These values are defined as int, float and complex class in Python.
Q. 10 What is Complex data type in python? Give an example of same.
Ans.
- Complex number is represented by complex class.
- It is specified as (real part) + (imaginary part)j.
- For example : 2.3 + 3j
Q. 11 Write a short note on Dictionary in python.
Ans.
- Dictionaries are hash table type.
- They are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).
- dict = {‘name’: ‘Riya’,’code’:2458, ‘dept’: ‘sales’}
Q. 12 What are tuples? Give one example.
Ans.
- A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. It is read only list
- T1 = ( ‘abcd’, 145 , 2.23, ‘john’, 70.2 )
- T2= (123, ‘john’)
Q.13 Explain Boolean data type.
Ans.
- The bool data type is used to represent boolean values – True or False.
- The data type can’t contain any other value.
- True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error.
Q.14 Identify whether following are mutable or immutable types
(i) String (ii) Dictionary (iii) float (iv) List
Ans.
(i) String – immutable
(ii) Dictionary – mutable
(iii) float– immutable
(iv) List – mutable
Q. 15 Write a Python program to test if a variable is a list or tuple or a set.
Ans.
x = eval(input(“Enter list, tuple or set”))
if type(x) is list:
print('x is a list')
elif type(x) is set:
print('x is a set')
elif type(x) is tuple:
print('x is a tuple')
else:
print('Neither a list or a set or a tuple.')
Q. 16 Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
Ans.
values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)