【ATBS with Python】Q&A Chap1 Python Basics

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.'
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值