【ATBS with Python】QA Chap1 Python Basics
Automate The Boring Stuff with Python
Practice Questions
文章目录
- Automate The Boring Stuff with Python
- Chap1 Python Basics
Chap1 Python Basics
- Which of the following are operators, and which are values?
*
'hello'
-88.8
-
/
+
5
Operater: *, -, /, +
Value: ‘hello’, -88.8, 5
- Which of the following is a variable, and which is a string?
spam
'spam'
Variable: spam
String: ‘spam’
- Name three data types.
String (str), integer (int), floating-point (float)
- 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.
- 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.
- What does the variable bacon contain after the following code runs?
bacon = 20
bacon + 1
>>> 21
- What should the following two expressions evaluate to?
'spam' + 'spamspam'
'spam' * 3
>>> ‘spam’ + ‘spamspam’
>>> ‘spamspamspam’
>>> ‘spam’ * 3
>>> ‘spamspamspam’
- 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
- What three functions can be used to get the integer, floating-point number, or string version of a value?
int()
float()
str()
- 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.'