Junior — Middle
Can you explain the purpose and usage of data types in Python?
sobes.tech AI
Answer from AI
In Python, data types define what kind of values a variable can hold and what operations are permissible on them. This helps the interpreter process data correctly.
Python is a dynamically typed language, meaning the type of a variable is determined automatically at runtime and can change. The main built-in types:
- int — integers
- float — floating-point numbers
- str — strings
- bool — logical values True/False
- list — mutable sequences
- tuple — immutable sequences
- dict — dictionaries (key-value pairs)
Example of using types:
x = 10 # int
x = "hello" # now x is a string
numbers = [1, 2, 3] # list of integers
if isinstance(x, str):
print("x is a string")
Types help write correct code, understand data structures, and use appropriate methods.