Python Interview Questions

Python Interview Questions - Data Types & Operators

Prepare for Python interviews with detailed questions on data types, collection types, and operators.

By TechCoder TeamLast updated: 2026-06-23
In a Nutshell

Prepare for Python interviews with detailed questions on data types, collection types, and operators. This interview-focused guide covers essential python interview questions - data types & operators concepts for technical interviews.

Python Interview Questions – Level 1: Data Types & Operators

Master the core data structures and operators that form the backbone of Python programming.


41. What are integers in Python?

Integers are whole numbers.

[!NOTE] Technical Detail: In Python 3, integers have arbitrary precision, meaning they can be as large as your computer's memory allows. This is different from languages like C++ where integers are typically 32 or 64-bit.

Example:

x = 5
y = -10
z = 1000000000000000000000  # Super big integer works fine!
print(x, y, z)  # 5, -10, 1000000000000000000000
print(type(x))  # <class 'int'>

42. What are floats?

Floats represent real numbers with a decimal point. They are implemented using double precision (64-bit) in C.

Example:

pi = 3.14159
temperature = -98.6
print(pi, temperature)
print(type(pi))  # <class 'float'>

43. What are complex numbers?

Consist of a real and an imaginary part, written as x + yj. They are useful in scientific computing and signal processing.

Example:

# Real part is 3, imaginary part is 4j
c = 3 + 4j
print(c)  # (3+4j)
print(c.real)  # 3.0
print(c.imag)  # 4.0

44. What is a string?

A string is an immutable sequence of Unicode characters.

Example:

greeting = "Hello World!"
name = 'Alice'
multi_line = """This is a
multi-line string"""
print(greeting, name)
print(type(greeting))  # <class 'str'>

45. How are strings stored?

Python 3 uses an optimized storage system (PEP 393). It stores strings in the most compact format possible:

  • 1 byte/char (Latin-1)
  • 2 bytes/char (UCS-2)
  • 4 bytes/char (UCS-4)

Example:

# Short ASCII string
s1 = "Hello"
# Unicode string with emojis
s2 = "Hello 👋"
print(s1, s2)

46. What is string slicing?

The syntax is string[start:stop:step].

[!TIP] Pro Tip: Slicing always creates a new string object. s[::-1] is the common way to reverse a string in a single line.

Example:

text = "Python"
print(text[0:3])    # 'Pyt'
print(text[:5])     # 'Pytho'
print(text[2:])     # 'thon'
print(text[::2])    # 'Pto' (every other character)
print(text[::-1])   # 'nohtyP' (reverse)

47. What is string immutability?

Strings cannot be modified in-place. If you try to change a character, Python raises a TypeError. This is done for security, caching (interning), and to allow strings to be used as dictionary keys.

Example:

my_string = "Hello"
# Trying to modify a character raises an error!
try:
    my_string[0] = "J"
except TypeError as e:
    print(e)  # 'str' object does not support item assignment

# Instead, create a new string
new_string = "J" + my_string[1:]
print(new_string)  # "Jello"

48. What are lists?

Lists are mutable, ordered sequences. They are implemented as dynamic arrays in CPython.

[!IMPORTANT] Complexity: Appending to a list is O(1) (amortized), but inserting at the beginning is O(n) because all existing elements must be shifted.

Example:

my_list = [1, "apple", True, 3.14]
print(my_list)  # [1, 'apple', True, 3.14]

# Mutable - can change elements
my_list[1] = "banana"
print(my_list)  # [1, 'banana', True, 3.14]

# Add elements
my_list.append("orange")
print(my_list)  # [1, 'banana', True, 3.14, 'orange']

49. How are lists different from tuples?

  • Lists: Mutable, slower, larger memory footprint (due to over-allocation).
  • Tuples: Immutable, faster, smaller memory footprint.

Example:

# List - mutable
my_list = [1, 2, 3]
my_list[0] = 99
print(my_list)  # [99, 2, 3]

# Tuple - immutable
my_tuple = (1, 2, 3)
try:
    my_tuple[0] = 99  # Error!
except TypeError as e:
    print(e)  # 'tuple' object does not support item assignment

50. What are tuples?

Tuples are immutable sequences. They are often used to group related "fields" together (like a database row).

Example:

# Tuple creation
point = (3, 4)
person = ("Alice", 30, "New York")

# Tuple unpacking
x, y = point
name, age, city = person
print(x, y, name)  # 3, 4, Alice

51. What are sets?

Sets are unordered collections of unique items. They are implemented using a hash table.

[!TIP] Complexity: Checking if an item exists in a set ('x' in my_set) is O(1) on average, compared to O(n) for a list.

Example:

numbers = {1, 2, 2, 3, 4, 4, 4}
print(numbers)  # {1, 2, 3, 4} (duplicates removed automatically)

# Check membership (fast!)
print(3 in numbers)  # True

# Set operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))  # {1, 2, 3, 4, 5}
print(set1.intersection(set2))  # {3}

52. What is a dictionary?

A dictionary is a collection of key-value pairs.

[!CAUTION] Interview Trap: Keys must be hashable (immutable). This means you can use a string, int, or tuple as a key, but NOT a list or another dictionary.

Example:

# Creating a dictionary
user = {
    "name": "Bob",
    "age": 25,
    "is_student": True
}
print(user)  # {'name': 'Bob', 'age': 25, 'is_student': True}

# Accessing values
print(user["name"])  # "Bob"

# Modifying values
user["age"] = 26
print(user["age"])  # 26

53. Difference between list, tuple, set, and dict?

FeatureListTupleSetDict
OrderedYesYesNoYes (since 3.7)
MutableYesNoYesYes
DuplicatesYesYesNoNo (Keys)

Example:

# List
my_list = [1, 2, 3]
# Tuple
my_tuple = (1, 2, 3)
# Set
my_set = {1, 2, 3}
# Dict
my_dict = {"a": 1, "b": 2}

print(type(my_list))   # <class 'list'>
print(type(my_tuple))  # <class 'tuple'>
print(type(my_set))    # <class 'set'>
print(type(my_dict))   # <class 'dict'>

54. What are arithmetic operators?

+, -, *, /, // (floor division), % (modulus), ** (exponent).

Example:

x = 10
y = 3

print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.333...
print(x // y) # 3 (floor division)
print(x % y)  # 1 (remainder)
print(x ** y) # 1000 (exponent)

55. What are comparison operators?

==, !=, >, <, >=, <=.

Example:

a = 5
b = 10

print(a == b)  # False
print(a != b)  # True
print(a < b)   # True
print(a > b)   # False
print(a <= 5)  # True
print(b >= 10) # True

56. What are logical operators?

and, or, not. They use short-circuit evaluation (e.g., if the first part of an and is False, the second part isn't even checked).

Example:

x = True
y = False

print(x and y)  # False
print(x or y)   # True
print(not x)    # False

57. What are assignment operators?

=, +=, -=, *=, etc.

Example:

x = 10
x += 5  # x = x + 5 → 15
x -= 2  # x = 13
x *= 3  # x = 39
print(x)  # 39

58. What are bitwise operators?

& (AND), | (OR), ^ (XOR), ~ (NOT), <<, >>. These operate on the binary representation of integers.

Example:

a = 60  # 0011 1100
b = 13  # 0000 1101

print(a & b)   # 12  (0000 1100)
print(a | b)   # 61  (0011 1101)
print(a ^ b)   # 49  (0011 0001)
print(a << 2)  # 240 (1111 0000)
print(a >> 2)  # 15  (0000 1111)

59. What is operator precedence?

The order of operations (PEMDAS). Parentheses > Exponent > Multiplication/Division > Addition/Subtraction.

Example:

result = 5 + 3 * 2
print(result)  # 11 (multiplication first, not 16)

# Use parentheses to change order
result = (5 + 3) * 2
print(result)  # 16

60. What is type casting?

Converting data from one type to another.

  • Implicit: Done by Python (e.g., 5 + 1.0 = 6.0).
  • Explicit: Done by you (e.g., int("10")).

Example:

# Explicit type casting
x = "10"
y = 5
z = int(x) + y
print(z)  # 15
print(type(z))  # <class 'int'>

# Float to int
print(int(3.14))  # 3 (truncates decimal part)

61. What is implicit casting?

Refer to question 60. It's an automatic conversion by the interpreter to prevent data loss.

num_int = 10      # Integer
num_float = 2.5   # Float

# Implicit casting: int + float -> float
result = num_int + num_float 
print(result)     # 12.5
print(type(result)) # <class 'float'>

62. What is explicit casting?

Refer to question 60. It's a manual conversion using functions like float(), str(), etc.

x = 10
y = "20"

# Explicit casting: converting str to int
result = x + int(y)
print(result) # 30

63. What is membership operator?

in and not in. They check if an element is a member of a sequence.

fruits = ["apple", "banana"]
print("apple" in fruits)      # True
print("cherry" not in fruits) # True

[!TIP] Performance: Membership checks are O(1) for sets and dictionaries, but O(n) for lists and strings.

64. What is identity operator?

is and is not.

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)  # True (Same values)
print(a is b)  # False (Different objects)
print(a is c)  # True (Same object)

[!IMPORTANT] Interview Question: What's the difference between == and is? Answer: == compares values (equality), while is compares memory addresses (identity).

65. What is indexing?

Accessing an element by its position. Python supports negative indexing, where -1 is the last element.

text = "Python"
print(text[0])   # 'P' (First element)
print(text[-1])  # 'n' (Last element)

66. What is slicing?

Refer to question 46.

67. What is len()?

Returns the number of items in a collection. It's an O(1) operation because Python collections store their size.

items = [1, 2, 3, 4]
print(len(items)) # 4

68. What is max()?

Returns the largest item. Works on any iterable with comparable elements.

print(max([10, 50, 20])) # 50
print(max("Hello"))      # 'o' (Lexicographically largest)

69. What is min()?

Returns the smallest item.

print(min([10, 50, 20])) # 10
print(min("Hello"))      # 'H' (Lexicographically smallest)

70. What is sum()?

Calculates the total of numeric items. You can also provide a start value: sum([1, 2], 10) # 13.

numbers = [1, 2, 3, 4]
print(sum(numbers))      # 10
print(sum(numbers, 10))  # 20 (starts adding from 10)

71. What is sorted()?

Returns a new sorted list. It uses Timsort (an O(n log n) algorithm).

nums = [3, 1, 4, 1, 5]
print(sorted(nums))             # [1, 1, 3, 4, 5]
print(sorted(nums, reverse=True)) # [5, 4, 3, 1, 1]

72. What is any()?

Returns True if at least one element is truthy.

print(any([False, False, True]))  # True
print(any([0, 0, 0]))             # False

73. What is all()?

Returns True if all elements are truthy.

print(all([True, True, True]))    # True
print(all([True, False, True]))   # False

74. What is hash()?

Returns the hash value. Only immutable objects are hashable.

print(hash("hello"))   # -123... (Some integer)
# print(hash([1, 2]))  # TypeError: unhashable type: 'list'

75. What is enumerate()?

Returns an iterator of (index, value) tuples. Much cleaner than using range(len(list)).

colors = ["red", "green"]
for index, value in enumerate(colors):
    print(f"{index}: {value}")
# Output:
# 0: red
# 1: green

76. What is zip()?

Combines multiple iterables into one iterator of tuples.

names = ["Alice", "Bob"]
ages = [25, 30]
# [('Alice', 25), ('Bob', 30)]
print(list(zip(names, ages))) 

[!NOTE] If the iterables have different lengths, zip stops at the shortest one by default. Use itertools.zip_longest to change this.

77. What is range()?

In Python 3, range() is a lazy object (a generator-like object) that produces numbers on demand. It consumes very little memory regardless of the range size.

# start, stop (exclusive), step
print(list(range(5)))        # [0, 1, 2, 3, 4]
print(list(range(1, 10, 2))) # [1, 3, 5, 7, 9]

78. What is eval()?

Evaluates a string as a Python expression.

x = 10
# Evaluates the string expression
print(eval("x + 5")) # 15

[!CAUTION] Security Risk: Never use eval() on untrusted input (like user data), as it allows execution of arbitrary code!

79. What is ord()?

Converts a single character to its integer Unicode code point.

print(ord("A"))  # 65
print(ord("€"))  # 8364

80. What is chr()?

Converts an integer Unicode code point back to a character.

print(chr(65))    # 'A'
print(chr(97))    # 'a'