Podcast
Questions and Answers
Which of the following statements is true about Python's type system?
Which of the following statements is true about Python's type system?
What is the purpose of Python's type()
function?
What is the purpose of Python's type()
function?
Which of these is NOT a standard numeric data type supported by Python?
Which of these is NOT a standard numeric data type supported by Python?
Which Python data type is primarily used to store a sequence of characters, enclosed in quotes?
Which Python data type is primarily used to store a sequence of characters, enclosed in quotes?
Signup and view all the answers
In Python, what operator is used to concatenate two strings?
In Python, what operator is used to concatenate two strings?
Signup and view all the answers
How can you access a specific range of elements within a Python list?
How can you access a specific range of elements within a Python list?
Signup and view all the answers
What distinguishes a tuple from a list in Python?
What distinguishes a tuple from a list in Python?
Signup and view all the answers
Which of these data types stores a collection of key-value pairs in Python?
Which of these data types stores a collection of key-value pairs in Python?
Signup and view all the answers
What is the output of the following code:
t = ('hi', 'python', 2)
print(t[1:])
What is the output of the following code:
t = ('hi', 'python', 2)
print(t[1:])
Signup and view all the answers
Which of the following statements is TRUE regarding tuples in Python?
Which of the following statements is TRUE regarding tuples in Python?
Signup and view all the answers
Given the following Python code snippet, what is the data type of the variable d
?
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
Given the following Python code snippet, what is the data type of the variable d
?
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
Signup and view all the answers
Which statement best describes the purpose of keywords in Python?
Which statement best describes the purpose of keywords in Python?
Signup and view all the answers
What is the output of the following code?
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
print(d.keys())
What is the output of the following code?
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
print(d.keys())
Signup and view all the answers
Study Notes
Data Types
- Variables in Python can hold different data types
- Python is a dynamically typed language, not needing to define variable type during declaration
- The interpreter automatically assigns data type
- Python's
type()
function returns the type of a variable
Example
-
a = 10
,b = "Hi Python"
,c = 10.5
-
print(type(a))
,print(type(b))
,print(type(c))
- Output:
<class 'int'>
,<class 'str'>
,<class 'float'>
Standard Data Types
- Numbers
- Strings
- Lists
- Tuples
- Dictionaries
Numbers
- Store numeric values
- Python creates number objects when assigned to a variable
- Examples:
a = 3
,b = 5
- Python supports four numeric types:
-
int
(signed integers, e.g., 10, 2, 29) -
long
(large integers, e.g., 908090800L) -
float
(floating-point numbers, e.g., 1.9, 9.902, 15.2) -
complex
(complex numbers, e.g., 2.14j, 2.0 + 2.3j)
-
Strings
- Sequence of characters in quotation marks
- Can use single, double, or triple quotes
- The
+
operator concatenates strings - The
*
operator replicates strings
Example (Strings)
-
str1 = "hello Mech"
-
str2 = " how are you"
-
print(str1[0:2])
,print(str1[4])
,print(str1*2)
,print(str1 + str2)
- Output:
he
,l
,hello Mechhello Mech
,hello Mech how are you
Lists
- Similar to arrays in C
- Can hold data of various types
- Items are separated by commas, enclosed in square brackets
- Can use slice operators
[:]
to access list data
Example (Lists)
-
I = [1, "hi", "python", 2]
-
print(I[3:])
,print(I[0:2])
,print(I)
,print(I+I)
,print(I*3)
- Example output:
[2]
,[1, 'hi']
,[1, 'hi', 'python', 2]
,[1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
,[1, 'hi', 'python', 2, 1, 'hi', 'python', 2, 1, 'hi', 'python', 2]
Tuples
- Similar to lists, but immutable (cannot be changed after creation)
- Items are enclosed in parentheses
- Items are separated by commas
Example (Tuples)
-
t = ("hi", "python", 2)
-
print(t[1:])
,print(t[0:1])
,print(t)
,print(t+t)
,print(t*3)
- Example output:
('python', 2)
,('hi',)
,('hi', 'python', 2)
,('hi', 'python', 2, 'hi', 'python', 2)
,('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2)
Dictionaries
- Ordered collection of key-value pairs
- Keys are unique and hold primitive data types
- Values can be any Python object
Example (Dictionaries)
-
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
-
print("1st name is "+d[1])
,print("2nd name is "+d[4])
,print(d)
,print(d.keys())
,print(d.values())
- Example output:
1st name is Jimmy
,2nd name is mike
,{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
[1,2,3,4]
,['Jimmy', 'Alex', 'john', 'mike']
Keywords
- Reserved words in Python with specific meanings to the compiler/interpreter
- Python 3.7 has 33 keywords
- Python 3.8 has 35 keywords (including
async
andawait
)
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz covers the foundational concepts of data types in Python, including numbers, strings, lists, tuples, and dictionaries. It delves into Python's dynamic typing and provides examples to illustrate how variables can hold different data types. Test your knowledge of these essential elements of Python programming!