Python Programming Exam Chapters 1-5 Questions With Correct Answers
Which of the following are operators, and which are values? * 'hello' -88.8 - / + 5 - Answer The operators are +,-,*,and /. The values are 'hello' ,-88.8, and 5. Which of the following is a variable , and which is a string ? spam 'spam' - Answer The variable is spam; the string is 'spam'. Strings always start and end with quotes. Name three data types. - Answer The three data types are integers, floating point numbers, and strings. What is an expression made up of ? What do all expressions do ? - Answer An expression is a combination of values and operators. All expressions evaluate (that is, reduce) to a single value. This chapter (1) introduced assignment statements, like spam = 10. What is the difference between an expression and a statement? - Answer An expression evaluates to a single value. A statement does not. What does the variable bacon contain after the following code runs? bacon = 20 bacon + 1 - Answer The bacon variable is set to 20. The bacon + 1 expression does not reassign the value in bacon (that would need an assignment statement: bacon = bacon + 1). What should the following two expressions evaluate to ? 'spam' + 'spamspam' 'spam' * 3 - Answer Both expressions evaluate to the string 'spamspamspam'. Why is egg a valid variable name while 100 is invalid ? - Answer Variable names cannot begin with a number. What three functions can be used to get the integer, floating-point number, or string version of a value ? - Answer The int(), float(), and str() functions will evaluate to the integer, floating-point number, and string versions of the value passed to them. Why does this expression cause an error ? How can you fix it ? 'I have eaten' + 99 + 'burritos' . - Answer The expression causes an error because 99 is an integer, and only strings can be concatenated to other strings with the + operator. The correct way is i have eaten ' + str(99) + ' burritos '. What are the two values of the Boolean data type? How do you write them? - Answer True and False, using capital T and F, with the rest of the word in lowercase. What are the three Boolean operators? - Answer and, or, and not. Write out the truth tables of each Boolean operator (that is, every possible combination of Boolean values for the operator and what they evaluate to). - Answer True and True is True. True and False is False. False and True is False. False and False is False. True or True is True. True or False is True. False or True is True. False or False is False. not True is False. not False is True. What do the following expressions evaluate to ? (5 > 4) and (3 == 5) not (5 >4) (5 > 4) or (3 == 5) not ((5 > 4) or (3 == 5)) (True and True) and (True == False) (not False) or (not True) - Answer False False True False False True What are the six comparison operators ? - Answer ==, !=, <,>,<=,and >= What is the difference between the equal to operator and the assignment operator ? - Answer == is the equal operator to operator that compares two values and evaluates to a Boolean, while = is the assignment operator that stores a value in a variable. Explain what a condition is and where you would use one. - Answer A condition is an expression used in a flow control statement that evaluates to a Boolean value. Identify the three blocks in this code: spam = 0 if spam == 10: print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam') print('spam') - Answer The three blocks are everything inside the if statement and the lines print('bacon') and print('ham'). print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam') Write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints Greetings! if anything else is stored in spam. - Answer if spam == 1: print('Hello') elif spam == 2: print('Howdy') else: print('Greetings!') What keys can you press if your program is stuck in an infinite loop ? - Answer Press ctrl-C to stop a program stuck in an infinite loop. What is the difference between break and continue ? - Answer The break statement will move the execution outside and just after a loop. The continue statement will move the execution to the start of the loop. What is the difference between range(10), range(0,10), and range(0,10,1) in a for loop ? - Answer They all do the same thing. The range(10) call ranges from 0 up to (but not including) 10, range(0,10) explicitly tells the loop to start at 0 , and range (0,10,1) explicitly tells the loop to increase the variable by 1 on each iteration. Write a short program that prints the numbers 1 to 10 using for loop. Then write an equivalent program that prints the numbers 1 to 10 using a while loop. - Answer for i in range (1,11): print(i) and: i = 1 while i <= 10: print(i) i = i + 1
Escuela, estudio y materia
- Institución
- Python Programming
- Grado
- Python Programming
Información del documento
- Subido en
- 7 de febrero de 2024
- Número de páginas
- 14
- Escrito en
- 2023/2024
- Tipo
- Examen
- Contiene
- Preguntas y respuestas
Temas
-
python programming stuvia
-
python programming exam chapters 1 5 questions wit
-
which of the following are operators and which ar
-
which of the following is a variable and which i