Python Notes (2) - Conditionals and Control Flow

本文总结了Python中的条件语句和控制流程,包括比较运算符、布尔操作符及其运算顺序,并通过Pig Latin游戏展示了如何使用这些概念进行编程实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处:http://blog.youkuaiyun.com/cxsydjn/article/details/71302863

The note summaries conditionals, control flow and a game implemented by Python.

Python notes of open courses @Codecademy.

Conditionals & Control Flow

  • Control flow: gives us this ability to choose among outcomes based off what else is happening in the program.
  • Comparators:
    • ==: Equal to
    • !=: Not equal to
    • <: Less than
    • <=: Less than or equal to
    • >: Greater than
    • >=: Greater than or equal to
  • Boolean operators: compare statements and result in boolean values.
    • and: which checks if both the statements are True;
    • or: which checks if at least one of the statements is True;
    • not: which gives the opposite of the statement.
    • Order of operations: not > and > or.
    • Parentheses (): ensure expressions are evaluated in the desired order. Anything in parentheses is evaluated as its own unit.
  • Conditional Statement Syntax: If, Else and Elif

    • if: is a conditional statement that executes some specified code after checking if its expression is True.
    • if some_function():
      • In the event that some_function() returns True, then the indented block of code after it will be executed.
      • In the event that it returns False, then the indented block will be skipped.
      • Note the colons at the end of the if statement.
    • else: complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”
    • Elif: is short for “else if.” It means that “otherwise, if the following expression is true, do this!”

      
      # Example for `If`, `Else` and `Elif`
      
      if this_might_be_true():
          print "This really is true."
      elif that_might_be_true():
          print "That is true."
      else:
          print "None of the above."

PygLatin

Pig Latin is a language game, where you move the first letter of the word to the end and add “ay.” So “Python” becomes “ythonpay.” To write a Pig Latin translator in Python, here are the steps we’ll need to take:

  • Ask the user to input a word in English.
  • Make sure the user entered a valid word.
  • Convert the word from English to Pig Latin.
  • Display the translation result.
  • Input
    • raw_input(): accepts a string, prints it, and then waits for the user to type something and press Enter (or Return). This string can also be stored in a variable, e.g., name = raw_input("What's your name?").
    • isalpha(): which returns False since the string contains non-letter characters.
  • Output

    • String[1:4]: accesses a slice of “String”, i.e., returns everything from the letter at position 1 up till position 4 (note not including the position 4).

      
      # Example for PygLatin
      
      print 'Welcome to the Pig Latin Translator!'
      
      pyg = 'ay'
      
      original = raw_input('Enter a word:')
      
      if len(original) > 0 and original.isalpha():
          word = original.lower()
          first = word[0]
          new_word = word + first + pyg
          new_word = new_word[1:len(new_word)]
          print new_word
      else:
          print 'empty'

External Resources

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值