《python基礎(chǔ)教程》讀書筆記第五章-條件、循環(huán)和其他

1.import功能

import somemodule

或者

from somemodule import somefunction

或者

from somemodule import somefunction , anotherfunction,yetanotherfunction

或者

form somemodule import *

給導(dǎo)入模塊取別名

import somemodule as xxxxmodule

from somemodule import somefunction as xxxfunction

eg.

>>> import math as testmodule

>>> testmodule.sqrt(9)

3.0

>>> from math import sqrt as test

>>> test(9)

3.0

2.賦值魔法

序列解包(sequence unpacking)

交換

eg.

>>> x,y,z = 1,2,3

>>> x,y,z

(1, 2, 3)

>>> x,y,z = z,x,y

>>> x,y,z

(3, 1, 2)

元組賦值

>>> myinfo

{'tel': '18081953671', 'name': 'Bruce'}

>>> key,value=myinfo.popitem()

>>> key,value

('name', 'Bruce')

3.0版本中有一個特殊用法

a,b,rest*=[1,2,3,4,5,6],賦值結(jié)果a=1,b=2,剩余的值收集道rest中

鏈?zhǔn)劫x值

x=y=somefunction()

等效于

y=somefunction()

x=y

增量賦值

x=6

x += 2? x -= 3 x *= 4

對其他數(shù)據(jù)類型同樣適用

>>> x = 'bruce'

>>> x += ' study'

>>> x

'bruce study'

>>> x *= 2

>>> x

'bruce studybruce study'

3.條件和條件語句

bool類型

>>> True

True

>>> False

False

>>> True==1

True

>>> False==0

True

bool函數(shù)

>>> bool('Bruce study python')

True

>>> bool(20)

True

>>> bool('')

False

>>> bool(0)

False

條件執(zhí)行 if elif else

if 條件:

? ? ? 語句1

? ? ? 語句2

? ? ? ....

elif:

? ? ? 語句1

? ? ? 語句2

? ? ? ....

else:

? ? ? 語句1

? ? ? 語句2

? ? ? ....

于其他語言不同的比較

x is y? ? x和y是同一個對象?

x is not y x和y是不同的對象?

x in y y是x容器

x not in y y不是x容器

== 和 is的區(qū)別:==比較兩個對象是否相等,is 比較兩個對象是否是同一個對象

>>> m=[1,2]

>>> n=[1,4]

>>> m==n

False

>>> m is n

False

>>>n[1]=2

>>>m=n

>>>True

>>>m is n

>>>False


in 成員運算符

字符串和序列比較

bool運算符

斷言assert

4.循環(huán)

while循環(huán)

x=1

while x<=100

print x

x +=1

for循環(huán)

for a in b

? xxxx

? xxxx


迭代工具

zip函數(shù)

>>> name=['nancy','bruce','pipi','popo']

>>> age=[28,34,2,61]

>>> zip(name,age)

[('nancy', 28), ('bruce', 34), ('pipi', 2), ('popo', 61)]


enumerate函數(shù)

翻轉(zhuǎn)和排序迭代

>>> a=[2,3,7,2,3,9,5]

>>> sorted(a)

[2, 2, 3, 3, 5, 7, 9]

>>> list(reversed(a))

[5, 9, 3, 2, 7, 3, 2]

注意,sorted函數(shù)并沒有改變a這個列表,reversed函數(shù)也沒有改變a列表本身


循環(huán)跳出

break語句 continue語句

for x in seq:

? if condition1:continue

? if condition1:continue

? if condition1:continue

? do_something()

? .....

? .....

自己寫的簡單程序

while True:

if name != 'Bruce':

? name = raw_input('input your name:')

else:

? if password != '123456':

? password = raw_input('input your password:')

? else:

? print 'you have input the right name and password!'

? name=password=''?

? continue

if name == 'over':

? break

列表推倒式

利用其他列表創(chuàng)建薪列表的一種方法


pass,del,exec語句

pass 什么都不做 跟nop類似,作用是當(dāng)部分代碼未完成而需要代碼來填充格式

del 刪除那些不再使用的對象


exec和eval

書上說這兩個函數(shù)要慎用


小結(jié):

1.打印

2.導(dǎo)入

3.賦值

4.塊

5.條件

6.斷言

7.循環(huán)

8.列表推倒式

9.pass del exec eval語句

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,317評論 0 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile麗語閱讀 4,085評論 0 6
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,638評論 18 399
  • 我的恐懼是一叢刺 默默地長在縫隙 你要是被它扎住 千萬?。〔灰牣?! 它是我長久的侶伴 眼里住著沉積的秘密 它戀著...
    蘇格拉李閱讀 211評論 2 3

友情鏈接更多精彩內(nèi)容