Python Notes (2) - Conditionals and Control Flow
2017-05-06 21:21
1016 查看
转载请注明出处:http://blog.csdn.net/cxsydjn/article/details/71302863
The note summaries conditionals, control flow and a game implemented by Python.
Python notes of open courses @Codecademy.
Comparators:
Boolean operators: compare statements and result in boolean values.
Order of operations:
Parentheses
Conditional Statement Syntax:
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.
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
Output
Conditional statements in Python
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,
Elseand
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
Falsesince 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
Conditionals and LoopsConditional statements in Python
相关文章推荐
- Java Notes (2) - Conditionals and Control Flow
- Conditionals and Control Flow 条件控制
- A Novel Approach to Data Retrieval and Instrumentation Control at Remote Field Sites using Python and Network News
- Program flow control (Notes)
- Python学习笔记(6)List and Keyboard Control
- SSIS ->> Control Flow And Data Flow
- Think Python :Chapter 5: Conditionals and Recursion 笔记
- Exceptions and control flow
- Reactive Programming with Rxjava-Chapter6:Flow Control and Backpressure(1)
- RUDP之四 —— Reliability and Flow Control(后附所有源码下载)
- Swift from Scratch: Optionals and Control Flow
- python-More Control Flow Tools ---已阅
- Notes on Python Naming and binding
- The Python Tutorial 3.2-4 More Control Flow Tools(深入控制流)
- 评一篇文章(Experiments of the effectiveness of dataflow- and controlflow-based test adequacy criteria)
- Sublime settings and control for Python
- Congestion and Flow Control at the Bundle Layer
- 6.087 Practical Programming in C, lec4:More control flow. Input and output.
- 评一篇文章(Experiments of the effectiveness of dataflow- and controlflow-based test adequacy criteria)
- Python Notes(Numbers,Strings and Lists)