What are the Data Types In Python Language?
Data types define the kind of value or data a variable holds. Python is dynamically typed, meaning you don't need to declare the type of a variable; it’s inferred based on the value assigned to it. Python has more than 9 built-in data types. Let's clarify and correctly list the key data types in Python:
Python Data Types:
- Integer
- Float
- Boolean
- Complex
- String
- List
- Tuple
- Set
- Dictionary
- Other Notable Data Types
- NoneType
- Bytes
- Bytearray
1) Integer:
Represents whole numbers. denoted by int.
Example:
x = 10
a = -100
b = 1
2) Float
Represents floating-point numbers (decimal numbers). denoted by float
Example:
x = 10.0
a = -100.12
b = 1.12
3) Boolean
Represents Boolean values, either True
or False
.represented by bool
Example:
x = True
y = False
4) complex
Represents complex numbers with real and imaginary parts.
Example:
x = z = 2 + 3j
5) String
Represents sequences of characters (strings). represented by str. if the data written in quots, even it is single quo, double quotes, triple quots, that is a string data type.
Example:
#this is a sting
name = "theagleye"
websiteName = theagleye.com' #this is also a string
#this is also a string
websiteInto = """
The eagle eye is a free content delivering website,
content deliver by eagle eye website is simple and in very easy word.
thank you
"""
6) list
An ordered, mutable collection of items. This is very similar to the Arrays. We use the list to store multiple elements in a single variable. The list uses square brackets to store data i.e [].
Example:
my_list = [1, 2, "apple"]
7) tuple
An ordered, immutable collection of items. Tuple uses round shape brackets to store the multiple elements in a single variable.
Example:
my_tuple = (1, 2, 3)
8) set
An unordered collection of unique items. Set uses curly brackets to store the multiple elements in a single variable.
Example:
my_set = {1, 2, 3}
9) dict (Dictionary)
A collection of key-value pairs. Dict uses curly brackets to store the multiple elements in a single variable, but the data is stored in the key: value relationship.
Example:
my_dict = {"name": "theagleye", "age": 23}
Other Notable Data Types:
- NoneType: Represents a null value (i.e., no value). Example:
x = None
- bytes: Represents immutable binary data.
- bytearray: Represents mutable binary data.
20+ interview questions covering Python's data types, including int, float, boolean, complex, string, list, tuple, dict, and set
Question 1. What are the main data types in Python?
Answer: The main data types in Python are:
- Numeric: int, float, complex
- Sequence: str, list, tuple
- Mapping: dict
- Set: set
- Boolean: bool
Question 2. How is an int different from a float?
Answer:
- int: Represents whole numbers (e.g., 1, 42).
- float: Represents numbers with a decimal point (e.g., 1.5, 42.0).
Example:
x = 10 # int
y = 10.5 # float
Question 3. What is a boolean (bool) data type in Python?
Answer: The bool data type represents two values: True and False.
Example:
is_python_easy = True
print(type(is_python_easy))
Output:
<class 'bool'>
Question 4. What is a complex number in Python?
Answer: A complex number has a real part and an imaginary part, represented as a + bj.
Example:
z = 3 + 4j
print(z.real)
print(z.imag)
Output:
3.0
4.0
Question 5. What are strings in Python, and how are they defined?
Answer: Strings (str) are sequences of characters enclosed in single (') or double (") quotes.
Example:
name = "Alice"
print(name)
Output:
Alice
Question 6. What is the difference between list and tuple in Python?
Answer:
- list: Mutable (can change its content).
- tuple: Immutable (cannot change its content).
Question 7. What are sets in Python, and how do they differ from lists?
Answer: A set is an unordered collection of unique items, while a list can have duplicate items and maintains order.
Example:
s = {1, 2, 2, 3}
print(s)
Output:
{1, 2, 3}
Question 8. How do you check the type of a variable in Python?
Answer: Use the type() function to check the variable's type.
Example:
x = 10
print(type(x))
Output:
<class 'int'>
Question 9. Can Python handle large integers?
Answer: Yes, Python supports arbitrarily large integers.
Example:
x = 10 ** 100
print(x)
Output:
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Question 10. How are floating-point numbers stored in Python?
Answer: Python uses double-precision (64-bit) floating-point representation.
Question 11. What are mutable and immutable data types in Python?
Answer:
- Mutable: Can be changed (e.g., list, dict, set).
- Immutable: Cannot be changed (e.g., int, float, str, tuple).
Question 12. How do you create a multi-line string in Python?
Answer: Use triple quotes (''' or """) to create multi-line strings.
Example:
text = """Hello,
This is a multi-line string."""
print(text)
Output:
Hello,
This is a multi-line string.
Question 13. How do you create an empty list, tuple, set, and dictionary in Python?
Answer:
- Empty list: []
- Empty tuple: ()
- Empty set: set()
- Empty dictionary: {}
Example:
empty_list = []
empty_tuple = ()
empty_set = set()
empty_dict = {}
Question 14. What is the difference between keys(), values(), and items() in a dictionary?
Answer:
- keys(): Returns all the keys.
- values(): Returns all the values.
- items(): Returns key-value pairs as tuples.
Example:
person = {"name": "Alice", "age": 25}
print(person.keys())
print(person.values())
print(person.items())
Output:
dict_keys(['name', 'age'])
dict_values(['Alice', 25])
dict_items([('name', 'Alice'), ('age', 25)])
Question 15. What are Python's numeric types?
Answer: Python has three numeric types: int, float, and complex.
Question 16. How do you concatenate two strings in Python?
Answer: Use the + operator to concatenate strings.
Example:
a = "Hello"
b = "World"
print(a + " " + b)
Output:
Hello World
Question 17. What happens if you try to add a number to a string?
Answer: It raises a TypeError.
Question 18. How do you convert between data types in Python?
Answer: Use typecasting functions like int(), float(), str(), list(), etc.
Question 19. How do you check if a value is present in a list, tuple, dictionary, or set?
Answer: Use the in keyword.
Question 20. How do you create a dictionary in Python?
Answer: A dictionary is created using curly braces {} with key-value pairs.
Example:
person = {"name": "eagleye", "age": 25}
print(person["name"])
Output:
eagleye
Tags:
Leave a comment
You must be logged in to post a comment.