Python - Data Types

Why should I learn to solve Python: Data Types technical interview questions?

Learn and practise solving Python: Data Types technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.

Where can I get technical Python: Data Types technical interview questions and answers with explanations?

IndiaBIX provides you with lots of fully solved Python: Data Types technical interview questions and answers with a short answer description. You can download Python: Data Types technical interview questions and answers as PDF files or e-books.

How do I answer Python: Data Types technical interview questions from various companies?

You can answer all kinds of Python: Data Types technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Data Types technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.

1.
What are the built-in data types in Python?

In Python, there are several built-in data types that serve different purposes. Some of the main built-in data types include:

  • int: Integer
  • float: Floating-point number
  • str: String
  • bool: Boolean
  • list: List
  • tuple: Tuple
  • set: Set
  • dict: Dictionary

Here's an example program showcasing the use of these built-in data types:

# Example program using built-in data types

# Integer
integer_variable = 42

# Floating-point number
float_variable = 3.14

# String
string_variable = "Hello, Python!"

# Boolean
bool_variable = True

# List
list_variable = [1, 2, 3, "four", 5.0]

# Tuple
tuple_variable = (10, 20, 30)

# Set
set_variable = {1, 2, 3, 4, 5}

# Dictionary
dict_variable = {'key1': 'value1', 'key2': 'value2'}

# Printing variables
print("Integer:", integer_variable)
print("Float:", float_variable)
print("String:", string_variable)
print("Boolean:", bool_variable)
print("List:", list_variable)
print("Tuple:", tuple_variable)
print("Set:", set_variable)
print("Dictionary:", dict_variable)
Integer: 42
Float: 3.14
String: Hello, Python!
Boolean: True
List: [1, 2, 3, 'four', 5.0]
Tuple: (10, 20, 30)
Set: {1, 2, 3, 4, 5}
Dictionary: {'key1': 'value1', 'key2': 'value2'}

In this example, variables of different built-in data types are defined and printed. Each data type serves a specific purpose and offers various functionalities for data manipulation.


2.
Explain the difference between mutable and immutable data types with examples.

In Python, data types are categorized as either mutable or immutable. Mutable data types can be modified after creation, while immutable data types cannot be changed. Understanding the difference is crucial for proper data manipulation and avoiding unintended side effects.

Mutable Data Types: Objects whose values can be changed after creation.

# Example of a mutable data type: List
mutable_list = [1, 2, 3]

# Modifying the list
mutable_list[0] = 99

print("Mutable list:", mutable_list)

Immutable Data Types: Objects whose values cannot be changed after creation.

# Example of an immutable data type: Tuple
immutable_tuple = (1, 2, 3)

# Attempting to modify the tuple would result in an error
# Uncommenting the next line would raise an error
# immutable_tuple[0] = 99
Mutable list: [99, 2, 3]

In the example, the list (mutable) can be modified by changing its elements, while attempting to modify the tuple (immutable) would result in an error. Mutable data types allow in-place modifications, while immutable data types provide safety against accidental changes.


3.
What is the purpose of the type() function in Python?

In Python, the type() function is used to get the type of an object. It returns the type of the specified object, providing information about the data type of the variable.

Here's an example program demonstrating the use of the type() function:

# Example program using the type() function

# Integer
integer_variable = 42
print("Type of integer_variable:", type(integer_variable))

# Float
float_variable = 3.14
print("Type of float_variable:", type(float_variable))

# String
string_variable = "Hello, Python!"
print("Type of string_variable:", type(string_variable))

# List
list_variable = [1, 2, 3]
print("Type of list_variable:", type(list_variable))

# Tuple
tuple_variable = (10, 20, 30)
print("Type of tuple_variable:", type(tuple_variable))
Type of integer_variable: <class 'int'>
Type of float_variable: <class 'float'>
Type of string_variable: <class 'str'>
Type of list_variable: <class 'list'>
Type of tuple_variable: <class 'tuple'>

In this example, the type() function is used to determine the data type of different variables, including integers, floats, strings, lists, and tuples.


4.
Differentiate between int, float, and complex numeric data types in Python.

In Python, there are different numeric data types, including int (integer), float (floating-point), and complex (complex numbers). These data types are used to represent different kinds of numbers.

Integer (int): Represents whole numbers without any fractional part.

# Example of the int data type
integer_variable = 42
print("Integer variable:", integer_variable)

Floating-point (float): Represents numbers with a fractional part.

# Example of the float data type
float_variable = 3.14
print("Float variable:", float_variable)

Complex (complex): Represents numbers in the form of a + bj, where a and b are real numbers and j represents the imaginary unit.

# Example of the complex data type
complex_variable = 2 + 3j
print("Complex variable:", complex_variable)
Integer variable: 42
Float variable: 3.14
Complex variable: (2+3j)

In this example, variables of different numeric data types are defined and printed. The int data type represents whole numbers, the float data type represents numbers with a fractional part, and the complex data type represents complex numbers.