By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
By the end of this topic, students will be able to:
input()
In Python, a variable is a name given to a value. Variables are used to store and manipulate data in a program. To declare a variable, you use the assignment operator (=) followed by the variable name and the value you want to assign to it.
x = 5 # declares a variable x and assigns it the value 5 name = "John" # declares a variable name and assigns it the string "John"
To get user input in Python, you use the input() function. This function returns a string value that represents what the user typed.
name = input("What is your name? ") # asks the user for their name and stores it in the variable name
Conditional statements are used to control the flow of a program based on certain conditions. In Python, you can use if, elif, and else statements to create conditional logic.
if
elif
else
x = 5 if x > 10: print("x is greater than 10") elif x == 5: print("x is equal to 5") else: print("x is less than 10")
# declare variables and get user input name = input("What is your name? ") age = int(input("How old are you? ")) # print a greeting message print(f"Hello, {name}! You are {age} years old.")
# get user input score = int(input("What is your score? ")) # use a conditional statement to print a message if score >= 80: print("You passed with distinction!") elif score >= 60: print("You passed!") else: print("You failed!")
int()
float()
What is the purpose of the input() function in Python?
A) To print a message to the user B) To get user input and store it in a variable C) To convert a string to an integer D) To declare a variable
Why the distractors fail: * A) The input() function does not print a message to the user; it returns the user's input. * C) The input() function does not convert a string to an integer; it returns a string value. * D) The input() function does not declare a variable; it gets user input and stores it in a variable.
What is the difference between the if and elif statements in Python?
A) The if statement is used for equality checks, while the elif statement is used for inequality checks. B) The if statement is used for true conditions, while the elif statement is used for false conditions. C) The if statement is used for single conditions, while the elif statement is used for multiple conditions. D) The if statement is used for conditional statements, while the elif statement is used for loops.
Why the distractors fail: * A) The if and elif statements are both used for conditional statements, not for equality or inequality checks. * B) The if and elif statements are both used for true conditions, not for false conditions. * D) The if and elif statements are both used for conditional statements, not for loops.
What is the purpose of the = operator in Python?
=
A) To get user input and store it in a variable B) To print a message to the user C) To declare a variable and assign it a value D) To convert a string to an integer
Why the distractors fail: * A) The = operator is used to assign a value to a variable, not to get user input. * B) The = operator is not used to print a message to the user. * D) The = operator is not used to convert a string to an integer.
What is the difference between the int() and float() functions in Python?
A) The int() function is used to convert a string to an integer, while the float() function is used to convert a string to a floating-point number. B) The int() function is used to convert a string to a floating-point number, while the float() function is used to convert a string to an integer. C) The int() function is used to convert a string to a boolean value, while the float() function is used to convert a string to a string value. D) The int() function is used to convert a string to a string value, while the float() function is used to convert a string to a boolean value.
Why the distractors fail: * B) The int() function is used to convert a string to an integer, not to a floating-point number. * C) The int() function is not used to convert a string to a boolean value. * D) The int() function is not used to convert a string to a string value.
What is the purpose of the elif statement in Python?
A) To declare a variable and assign it a value B) To get user input and store it in a variable C) To print a message to the user D) To use a conditional statement to check for multiple conditions
Why the distractors fail: * A) The elif statement is used to check for multiple conditions, not to declare a variable. * B) The elif statement is not used to get user input and store it in a variable. * C) The elif statement is not used to print a message to the user.
What is the difference between a variable and a constant in Python?
How do you use the input() function to get user input in Python?
What is the purpose of the if statement in Python?
How do you use the elif statement to check for multiple conditions in Python?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.