Questions and Answers
How do you declare a variable in Python?
Which of the following creates a set in Python?
What is the output of the following code: x = {1, 2, 3}; x.add(4); print(x)
How do you declare a constant variable in Python?
Signup and view all the answers
What is the output of the following code: x = 10; y = 20; print(x == y)
Signup and view all the answers
How do you convert a string to an integer in Python?
Signup and view all the answers
What is the correct syntax to create a variable in Python?
Signup and view all the answers
What is the output of the following code? x = 5; y = "5"; print(x + y)
Signup and view all the answers
How do you create a list in Python?
Signup and view all the answers
What is the keyword used to define a function in Python?
Signup and view all the answers
Which of the following is a mutable data type in Python?
Signup and view all the answers
What is the output of the following code? x = (1, 2, 3); x = 10; print(x)
Signup and view all the answers
Which of the following is an immutable data type in Python?
Signup and view all the answers
Study Notes
Variables and Data Types
- In Python, a variable is created by assigning a value to a name using the assignment operator (=).
- The correct syntax to create a variable in Python is
x = 5
.
Data Types
- Integer data type in Python:
C. 42
. - The output of
type(3.14)
isfloat
. - A valid variable name in Python cannot start with a number, so
A. 3dogs
is not valid. - The output of
print(type(x))
wherex = "Hello"
isstr
. - Mutable data type in Python:
D. list
. - The keyword used to define a function in Python is
B. def
. - The keyword used to create a class in Python is
B. class
.
List
- A list in Python is created using square brackets
[]
. - The output of
print(type(x) == int)
wherex = 5
isTrue
. - An immutable data type in Python:
C. str
. - The output of
print(x)
wherex = [1, 2, 3]
and thenx = 10
is10
.
Dictionary
- A dictionary in Python is created using curly brackets
{}
. - The output of
print(x)
wherex = (1, 2, 3)
and thenx = 10
is10
.
Boolean
- A boolean data type in Python:
C. True
.
Sets
- A set in Python is created using the
set()
function. - The output of
print(x)
wherex = {1, 2, 3}
and thenx.add(4)
is{1, 2, 3, 4}
.
Tuples
- A tuple in Python is created using parentheses
()
. - The output of
print(x + y)
wherex = (1, 2, 3)
andy = (4, 5, 6)
is(1, 2, 3, 4, 5, 6)
.
Constants
- There is no specific way to declare a constant variable in Python, but by convention, constants are written in uppercase letters, such as
X = 10
.
Operations
- The output of
print(x + y)
wherex = 5
andy = "5"
isTypeError
. - The output of
print(x == y)
wherex = 10
andy = 20
isFalse
.
Conversions
- The correct way to convert a string to an integer in Python is
A. int("10")
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about the basics of variables and data types in Python, including syntax, integer data types, and more.