Podcast
Questions and Answers
Which of the following is an immutable sequence type in Python?
Which of the following is an immutable sequence type in Python?
What is the data type of the number 3+4j in Python?
What is the data type of the number 3+4j in Python?
Which of the following is a mutable mapping type in Python?
Which of the following is a mutable mapping type in Python?
What is the data type of the string 'hello' in Python?
What is the data type of the string 'hello' in Python?
Signup and view all the answers
Which of the following is a binary type in Python?
Which of the following is a binary type in Python?
Signup and view all the answers
What is the data type of the set {1, 2, 3} in Python?
What is the data type of the set {1, 2, 3} in Python?
Signup and view all the answers
What is the data type of the boolean value True in Python?
What is the data type of the boolean value True in Python?
Signup and view all the answers
Study Notes
Data Types in Python
Numeric Types
- int: whole numbers, e.g. 1, 2, 3, etc.
- float: decimal numbers, e.g. 3.14, -0.5, etc.
- complex: complex numbers, e.g. 3+4j, 2-5j, etc.
Sequence Types
-
str: strings, e.g. "hello", 'hello', etc. (immutable)
- Can be indexed and sliced like lists
- Supports string methods like
upper()
,lower()
, etc.
-
list: ordered collection of items, e.g. [1, 2, 3], ["a", "b", "c"], etc. (mutable)
- Can be indexed and sliced
- Supports list methods like
append()
,extend()
, etc.
- tuple: ordered, immutable collection of items, e.g. (1, 2, 3), ("a", "b", "c"), etc.
Mapping Type
-
dict: unordered collection of key-value pairs, e.g. {"name": "John", "age": 30}, etc. (mutable)
- Can be indexed by key
- Supports dictionary methods like
keys()
,values()
, etc.
Set Types
-
set: unordered collection of unique items, e.g. {1, 2, 3}, {"a", "b", "c"}, etc. (mutable)
- Supports set methods like
union()
,intersection()
, etc.
- Supports set methods like
- frozenset: unordered, immutable collection of unique items, e.g. frozenset({1, 2, 3}), etc.
Boolean Type
- bool: true or false values
Binary Types
- bytes: sequence of integers in the range 0 <= x < 256, e.g. b"hello", etc.
- bytearray: mutable sequence of integers in the range 0 <= x < 256, e.g. bytearray(b"hello"), etc.
None Type
- NoneType: represents the absence of a value, e.g. None
Data Types in Python
Numeric Types
- There are three numeric types: int, float, and complex.
- int represents whole numbers, such as 1, 2, and 3.
- float represents decimal numbers, such as 3.14 and -0.5.
- complex represents complex numbers, such as 3+4j and 2-5j.
Sequence Types
- There are three sequence types: str, list, and tuple.
- str represents strings, such as "hello" and 'hello', which are immutable.
-
str can be indexed and sliced like lists and supports string methods like
upper()
andlower()
. - list represents ordered collections of items, such as [1, 2, 3] and ["a", "b", "c"], which are mutable.
-
list can be indexed and sliced and supports list methods like
append()
andextend()
. - tuple represents ordered, immutable collections of items, such as (1, 2, 3) and ("a", "b", "c").
Mapping Type
- dict represents unordered collections of key-value pairs, such as {"name": "John", "age": 30}, which are mutable.
-
dict can be indexed by key and supports dictionary methods like
keys()
andvalues()
.
Set Types
- There are two set types: set and frozenset.
- set represents unordered collections of unique items, such as {1, 2, 3} and {"a", "b", "c"}, which are mutable.
-
set supports set methods like
union()
andintersection()
. -
frozenset represents unordered, immutable collections of unique items, such as
frozenset({1, 2, 3})
.
Boolean Type
- bool represents true or false values.
Binary Types
- bytes represents sequences of integers in the range 0 <= x < 256.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the basics of data types in Python, including numeric types, sequence types, and more.