Automate The Boring Stuff with Python

Practice Questions



Chap1 Python Basics

  1. Which of the following are operators, and which are values?
*
'hello'
-88.8
-
/
+
5

Operater: *, -, /, +

Value: ‘hello’, -88.8, 5


  1. Which of the following is a variable, and which is a string?
spam
'spam'

Variable: spam

String: ‘spam’


  1. Name three data types.

String (str), integer (int), floating-point (float)


  1. What is an expression made up of? What do all expressions do?

An expression is made up of values and operations, and they can evaluate down to a single value. A single value without no operators is also considered an expression, though it evaluates only to itself.


  1. This chapter introduced assignment statements, like spam = 10. What is the difference between an expression and a statement?

An expression is a combination of values, variables, and operators that evaluates to a single value (e.g., 2 + 3 or spam + 5).

A statement is a complete line of code that performs an action, such as assigning a value (spam = 10) or controlling the flow of execution (e.g., if, for).

Expressions can be part of statements, but statements do not return a value.


  1. What does the variable bacon contain after the following code runs?
bacon = 20
bacon + 1

>>> 21


  1. What should the following two expressions evaluate to?
'spam' + 'spamspam'
'spam' * 3

>>> ‘spam’ + ‘spamspam’

>>> ‘spamspamspam’

>>> ‘spam’ * 3

>>> ‘spamspamspam’


  1. Why is eggs a valid variable name while 100 is invalid?

A valid variable name should ebey the following four rules:

  • It can’t have spaces
  • It can use only letters, numbers, underscore (_) character
  • It can’t begin with a number
  • It can’t be a Python keyword, such as if, for, return, or other keywords

  1. What three functions can be used to get the integer, floating-point number, or string version of a value?
    int() float() str()

  1. Why does this expression cause an error? How can you fix it?
'I eat ' + 99 + ' burritos.'

99 is an integer

'I eat ' + str(99) + ' burritos.'
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐