全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 python论坛
1065 1
2015-01-26
  • How might you code a multiway branch in Python?

  • How can you code an if/else statement as an expression in Python?

  • How can you make a single statement span many lines?

  • What do the words True and False mean?



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-1-26 00:14:19
  • An if statement with multiple elif clauses is often the most straightforward way to code a multiway branch, though not necessarily the most concise or flexible. Dictionary indexing can often achieve the same result, especially if the dictionary contains callable functions coded with def statements or lambda expressions.

  • In Python 2.5 and later, the expression form Y if X else Z returns Y if X is true, or Z otherwise; it’s the same as a four-line if statement. The and/or combination (((X and Y) or Z)) can work the same way, but it’s more obscure and requires that the Y part be true.

  • Wrap up the statement in an open syntactic pair ((), [], or {}), and it can span as many lines as you like; the statement ends when Python sees the closing (right) half of the pair, and lines 2 and beyond of the statement can begin at any indentation level. Backslash continuations work too, but are broadly discouraged in the Python world.

  • True and False are just custom versions of the integers 1 and 0, respectively: they always stand for Boolean true and false values in Python. They’re available for use in truth tests and variable initialization, and are printed for expression results at the interactive prompt. In all these roles, they serve as a more mnemonic and hence readable alternative to 1 and 0.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群