麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Notes of Py for informatics

2019-11-10 20:17:29
字體:
來源:轉載
供稿:網友

Chapter 2 Var, exp and statements

2.1 Type Conversion

sval = '123'type(sval)ival = int(sval)PRint ival+1

2.2 User Input

nam = raw_input('Who are you?') # the screen shows 'Who are you?', and the input value is saved in namprint 'Welcome', nam# if we want to read a number from the user. We must convert it from a string to a number using a type conversion functioninp = raw_input('Europe Floor?')print 'American Floor:', int(inp)+1

Chapter 3 Conditional execution

3.1 One Way Decision & Indentation

if x == 6:   print 'is 6'   print 'is still 6'Turn off tabs: If you mix tabs and spaces, you may get 'indentation errors'.

3.2 Two Way Decisions

if x > 2:   print 'Bigger'else:   print 'Smaller'

3.3 Muti-way

if x < 2:   print 'small'elif x < 10:     #elif -> else if   print 'medium'else:   print 'large'print 'all done'3.4 try / except structure

You surround a dangerous section of code with try and except.

If the code in the try works - the except is skipped.

If the code in the except works - the try is skipped.

astr = 'Hello Bob'try:    istr = int(astr)  # if the input is 'qwe', then the 'try' can not be executedexcept:    istr = -1comment: have a try. It's totally different from the if/else. It has a network. 'a blow up' stuff.example:

rawstr = raw_input('Enter a number:')try:    ival =int(rawstr)except:    ival = -1if ival > 0:    print 'Nice work'else:    print 'Not a number'3.5 if not

Chapter 4 Functions

two types of functions:

build-in functions

functions that we define ourselves and then use

no arguments:

def print_lyrics():    print "I sleep all night and I work all day"

arguments:

def greet(lang):    if lang == 'es':       print 'Hola'....

return value:

def greet(lang):    if lang == 'es':       return 'Hola'....print greet('es')

multiple parameters / Arguments:

def addtwo(a,b):    added = a + b    return addedvoid function

Chapter 5 Loops and Iteration

5.1 breaking out of a loop

while True:    line = raw_input('>')    if line == 'done':        break    print lineprint 'Done!'break: end the current loop and jump to the statement immediately following the loop

5.2 continue

the continue statement ends the current iteration andjumps to the top of the loop and starts the next iteration

5.3 Indefinte Loops & definite Loops

Indefinte Loops: While loops are called "indefinite loops" because they keep going until a logical condition becomesFalse.  while

Defintie Loops: finite list of things to do. For

for i in [5,4,3,2,1]:    print i print 'Blastoff!'
friends = ['Joe','Sally','Mike']for friend in friends:    print 'Happy New Year:', friendprint 'Done!'

5.4 Loop Idioms
smallest = None...if smallest is None: # trigger the first iteration  #if value is None/True/False is ok # if value is 4     ..elif value < smallest:    smallest = valueChapter 6 Strings

both quotes: 'Hello' and "Hello" are ok

'+' means concatenate

6.1 Look inside strings

fruit = 'banana'letter = fruit[1]print letter

output:

a

you will get a python error if you attempt to indexbeyond the end of a string

len(fruit)
# approach1
index = 0while index < len(fruit):    ...    index = index + 1
# approach2
for letter in fruit:     ...# I love the 'in'

6.2 Slicing Strings: colon Operator

s = 'Monty Python'print s[0:4]  #starts from1, up to but not including 4

output:

Mont

print s[:2]print s[8:]print s[:]

output:

MothonMonty Python

6.3 Using in as an operator

print 'nan' in fruit

Output:

True

Word < 'banana'word == 'banana'#comparison6.4 String Library

xxx.function()

These functions do not modify the original string, instead theyreturn a new string that has been altered.  =

greet = 'Hello Bob'zap = greet.lower()print zap

output:

hello bob

print dir(greet)['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Find

pos = fruit.find('na')print posoutput:

2
pos = fruit.find('a',pos+1) #starts from the position of the next characterfind() finds the first occurance of the substring

if the substring is not found, find() returns-1

Replace

nstr = greet.replace('Bob','Jane')print nstrHello JaneStripping Whitespace

Sometimes we want to take a string and remove whitespace at the beginning and/or end

lstrip() #left

rstrip() #right

strip() #all

greet = '     Hello Bob'print greet.strip()     Hello BobHello Bob

Prefixes

print greet.startswith(' ')True


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品视频一区二区三区四区国 | 国产成人自拍视频在线观看 | 久久国产91 | 成人性视频免费网站下载软件 | 国产一区二区免费在线观看 | 美国av免费看 | 国产精品一区在线看 | 精品一区二区免费视频视频 | 国产成人自拍视频在线 | 国产精品片一区二区三区 | 黄色一级片在线免费观看 | 欧美级毛片 | 国产91久久久久 | 欧美日韩免费看 | 欧美成人精品一区二区三区 | 欧美大逼网 | 国产高潮好爽受不了了夜色 | 国产一级在线免费观看 | 中国产一级毛片 | 黄色片在线免费播放 | 日韩大片在线永久观看视频网站免费 | 午夜视频在线观 | 成人免费网站在线观看 | 久久精品亚洲一区二区 | 中文字幕激情视频 | 欧美视频首页 | 中文字幕四区 | 久久视频精品 | 黄污网站在线 | 动漫孕妇被羞羞视频 | 在线观看麻豆 | 免费毛片电影 | 黄色网址你懂的 | 国产免费中文字幕 | 欧美精品一区二区中文字幕 | 色交视频 | 在线成人免费观看视频 | 午夜精品福利视频 | 成人免费毛片片v | 国产精品自在线拍 | 国产亚洲欧美日韩在线观看不卡 |