Podcast
Questions and Answers
Which data type is used to store a sequence of characters in Python?
Which data type is used to store a sequence of characters in Python?
- int
- bool
- float
- str (correct)
In Python, a variable's data type is explicitly declared before assigning a value to it.
In Python, a variable's data type is explicitly declared before assigning a value to it.
False (B)
Name three of the built-in numeric data types in Python.
Name three of the built-in numeric data types in Python.
int float complex
The __________
data type is used to store ordered collections of items in Python, which are mutable.
The __________
data type is used to store ordered collections of items in Python, which are mutable.
Match the following Python data types with their corresponding descriptions:
Match the following Python data types with their corresponding descriptions:
Which of the following data types is immutable?
Which of the following data types is immutable?
You can convert a complex number directly into an integer using the int()
function in Python.
You can convert a complex number directly into an integer using the int()
function in Python.
What is the primary difference between a set
and a frozenset
in Python?
What is the primary difference between a set
and a frozenset
in Python?
The __________
data type represents boolean values, which can be either True
or False
.
The __________
data type represents boolean values, which can be either True
or False
.
Match the following Python code snippets with their corresponding data types:
Match the following Python code snippets with their corresponding data types:
Which of the following functions can be used to convert a number to a floating-point number?
Which of the following functions can be used to convert a number to a floating-point number?
Python has a built-in function called random()
that generates a random number.
Python has a built-in function called random()
that generates a random number.
Explain the use of the range()
function in Python.
Explain the use of the range()
function in Python.
The __________
data type stores key-value pairs in Python.
The __________
data type stores key-value pairs in Python.
Match each operator with its description:
Match each operator with its description:
What will the following code output?
x = 10
y = 3
print(x % y)
What will the following code output?
x = 10
y = 3
print(x % y)
The **
operator in Python is used for multiplication.
The **
operator in Python is used for multiplication.
What is the purpose of the //
operator in Python?
What is the purpose of the //
operator in Python?
The __________
operator is used to assign a value to a variable.
The __________
operator is used to assign a value to a variable.
Match the assignment operators with their equivalent expressions:
Match the assignment operators with their equivalent expressions:
What will the following code output?
x = 5
y = 10
print(x > y)
What will the following code output?
x = 5
y = 10
print(x > y)
The ==
operator checks if two values are not equal.
The ==
operator checks if two values are not equal.
Name three comparison operators in Python.
Name three comparison operators in Python.
The __________
operator returns True
if both statements are true.
The __________
operator returns True
if both statements are true.
Match each logical operator with its description:
Match each logical operator with its description:
What will the following code output?
x = True
y = False
print(x or y)
What will the following code output?
x = True
y = False
print(x or y)
The not
operator returns True
if the statement is true.
The not
operator returns True
if the statement is true.
Explain the purpose of logical operators in Python.
Explain the purpose of logical operators in Python.
In Python, the ______ data type is used to store a sequence of characters.
In Python, the ______ data type is used to store a sequence of characters.
Match data types with their example values:
Match data types with their example values:
Flashcards
Data Type
Data Type
A category defining the type of value a variable can hold, such as text, numbers, or booleans.
String (str)
String (str)
A built-in Python data type representing a sequence of characters.
Integer (int)
Integer (int)
A built-in Python data type representing whole numbers.
Float
Float
Signup and view all the flashcards
Complex
Complex
Signup and view all the flashcards
List
List
Signup and view all the flashcards
Tuple
Tuple
Signup and view all the flashcards
Range
Range
Signup and view all the flashcards
Dictionary (dict)
Dictionary (dict)
Signup and view all the flashcards
Set
Set
Signup and view all the flashcards
Boolean (bool)
Boolean (bool)
Signup and view all the flashcards
Type Conversion
Type Conversion
Signup and view all the flashcards
Random Module
Random Module
Signup and view all the flashcards
Operators
Operators
Signup and view all the flashcards
Arithmetic Operators
Arithmetic Operators
Signup and view all the flashcards
Assignment Operators
Assignment Operators
Signup and view all the flashcards
Comparison Operators
Comparison Operators
Signup and view all the flashcards
Logical Operators
Logical Operators
Signup and view all the flashcards
Study Notes
- In programming, data type is an important concept
- Variables can store data of different types, and different types can perform different actions
- Python has the following built-in data types by default:
Built-in Data Types
- Text Type:
str
- Numeric Types:
int
,float
,complex
- Sequence Types:
list
,tuple
,range
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- Binary Types:
bytes
,bytearray
,memoryview
Setting the Data Type
- In Python, the data type is set when you assign a value to a variable
Example | Data Type |
---|---|
x = "Hello World" |
str |
x = 20 |
int |
x = 20.5 |
float |
x = 1j |
complex |
x = ["apple", "banana", "cherry"] |
list |
x = ("apple", "banana", "cherry") |
tuple |
x = range(6) |
range |
x = {"name": "John", "age" : 36} |
dict |
x = {"apple", "banana", "cherry"} |
set |
Python Numbers
- There are three numeric types in Python:
int
,float
, andcomplex
- Variables of numeric types are created when a value is assigned to them
Int
- Integers can be positive or negative, without decimals, and of unlimited length
- For example:
x = 1
y = 35656222554887711
z = -3255522
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'int'>
print(type(z)) # Output: <class 'int'>
Float
- Floats are numbers containing a decimal point, or can be exponential numbers
- For example:
x = 1.10
y = 1.0
z = -35.59
print(type(x)) # Output: <class 'float'>
print(type(y)) # Output: <class 'float'>
print(type(z)) # Output: <class 'float'>
Complex
- Complex numbers are written with a "j" as the imaginary part
- For example:
x = 3+5j
y = 5j
z = -5j
print(type(x)) # Output: <class 'complex'>
print(type(y)) # Output: <class 'complex'>
print(type(z)) # Output: <class 'complex'>
Type Conversion
- Conversion from one type to another can be performed with the
int()
,float()
, andcomplex()
methods - Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex
## convert from int to float:
a = float(x)
print(a) # Output: 1.0
print(type(a)) # Output: <class 'float'>
## convert from float to int:
b = int(y)
print(b) # Output: 2
print(type(b)) # Output: <class 'int'>
## convert from int to complex:
c = complex(x)
print(c) # Output: (1+0j)
print(type(c)) # Output: <class 'complex'>
- Complex numbers cannot be converted into another number type
Random Number
- To make a random number, Python utilizes a built-in module called
random
- Example:
import random
print(random.randrange(1, 10)) # Output: 5
Python Booleans
- Booleans represent one of two values:
True
orFalse
Boolean Values
- It's often necessary to determine whether an expression is
True
orFalse
in programming - Evaluation of any expression in Python yields either
True
orFalse
- Comparison of two values leads to Python evaluating the expression and returning the Boolean answer
Example
print(10 > 9) # Output: True
print(10 == 9) # Output: False
print(10 < 9) # Output: False
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a") # Output: b is not greater than a
Python Operators
- Operators are used to perform operations on variables and values
- Python divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic Operators
- Arithmetic operators are used with numeric values to perform common mathematical operations
Operator | Name | Example |
---|---|---|
+ |
Addition | x + y |
- |
Subtraction | x - y |
* |
Multiplication | x * y |
/ |
Division | x / y |
% |
Modulus | x % y |
** |
Exponentiation | x**y |
Example
x = 15
y = 4
print('x + y =',x+y) # Output: x + y = 19
print('x - y =',x-y) # Output: x - y = 11
print('x * y =',x*y) # Output: x * y = 60
print('x / y =',x/y) # Output: x / y = 3.75
print('x // y =',x//y) # Output: x // y = 3
print('x ** y =',x**y) # Output: x ** y = 50625
Assignment Operators
Operator | Example | Same As |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
Example
a = 10
b = a
print(b) # Output: 10
b += a
print(b) # Output: 20
b -= a
print(b) # Output: 10
b *= a
print(b) # Output: 100
b <<= a
print(b) # Output: 102400
Comparison Operators
- Comparison operators are used to compare two values
Operator | Name | Example |
---|---|---|
== |
Equal | x == y |
!= |
Not equal | x != y |
> |
Greater than | x > y |
< |
Less than | x < y |
>= |
Greater than or equal to | x >= y |
<= |
Less than or equal to | x <= y |
Example
x = 10
y = 12
print('x > y is',x>y) # Output: x > y is False
print('x < y is',x<y) # Output: x < y is True
print('x == y is',x==y) # Output: x == y is False
print('x != y is',x!=y) # Output: x != y is True
print('x >= y is',x>=y) # Output: x >= y is False
print('x <= y is',x<=y) # Output: x <= y is True
Logical Operators
- Logical operators are used to combine conditional statements
Operator | Description | Example |
---|---|---|
and |
Returns True if both statements are true | x < 5 and x < 10 |
or |
Returns True if one of the statements is true | x < 5 or x < 4 |
not |
Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Example
x = True
y = False
print('x and y is',x and y) # Output: x and y is False
print('x or y is',x or y) # Output: x or y is True
print('not x is',not x) # Output: not x is False
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.