Menu

Casting

Card 1 of 16 0 got it 0 still learning
Question
TapClick or press space to see the answer
Answer
All 16 cards in this set
What is the name for converting a value from one data type to another?
Casting
Which function converts a string to an integer?
int()
What does the function float do?
Converts a value to a real number with a decimal point
Why is int(input('Enter a number: ')) often used when reading a number from the user?
Input is a string and must be converted before it can be used in a calculation
Which function converts an integer or floating point number to a string?
str()
A variable called age holds a string. Which statement converts the value in age to an integer?
Age = int(age)
Which function converts a number such as 3.14 to a string?
str()
What is the result of the expression int('5') + 2?
7
What is the result of the expression str(3) + str(4)?
'34'
Which function converts a string to a real number that can hold decimal places?
float()
A user types 10 in response to an input statement. What data type is the value stored before any conversion?
String
A program stores the user's score as a string but needs to add points to it. Why is casting required?
The string must be converted to a number before it can be used in a calculation
A program asks the user for the price of an item and the user types 2.5. Which statement converts the input so it can be used in a calculation?
Price = float(input('Enter the price: '))
A program holds a score as an integer and needs to display it joined to the text 'Your score is '. What must the program do to the score?
Convert it to a string before joining it to the text
Which function converts a string such as 'True' to a Boolean value?
bool()
What is the value of the expression 'Total: ' + str(10 + 5)?
'Total: 15'