Python Notes (2)
来源:互联网 发布:刷优酷视频 淘宝违规 编辑:程序博客网 时间:2024/06/08 18:18
转载请注明出处: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.
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 areTrue
;or
: which checks if at least one of the statements isTrue
;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
andElif
if
: is a conditional statement that executes some specified code after checking if its expression isTrue
.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 returnsFalse
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 PygLatinprint '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_wordelse: print 'empty'
External Resources
- Conditionals and Loops
- Conditional statements in Python
- Python Notes (2)
- Python Notes #2 - 1.21.2013
- Python Notes
- Python notes
- Python Notes
- python notes
- Python notes
- Python Notes
- Python Notes
- Python Notes
- python notes
- python-notes
- Python Notes
- Python 3.3 Tutorial Notes - 2:Control Flow
- Notes of Python Learning 2/10/2016
- Python chapter 2&3 learning notes
- Python Cookbook Notes Chapter1
- Python Reviewing Notes
- android 实现aidl跨进程通信之二
- nodejs群聊和私聊
- Android程序配置文件使用
- sdut 3565 Feed the monkey dp
- 嵌入式面试之RS232-C串口通讯协议解析
- Python Notes (2)
- 向量点积及其意义
- 进程实验报告
- linux下jdk的安装
- redis集群的搭建(伪集群)
- 在android中service启动异常
- MySQL日记——锁机制
- 最新Eric Python IDE 安装方法Eric6_17.04.1 + PyQt5_5.8.2
- jquery-zclip插件