天天看點

python學習筆記(九)之語句1cat shiyan.pycat range.pypython range.py

python學習筆記(九)之語句1

print

python2中,print是一個語句,python3中它是一個函數。

執行個體1:

>> print "hello,world!" hello,world! >> print "hello","world!" hello world! 說明:print語句中,字元串後面會接一個\n符号,即換行!但是,如果要在一個字元串後面跟着逗号,那麼換行就取消了,如下: 執行個體2: >> for i in [1,2,3,4]: ... print i ...  1 2 3 4 ... print i, 1 2 3 4

import

執行個體3:

>> import math >> math.pow(3,2) 9.0 或者是: >> from math import pow ##适合引入子產品較少的情況。如果引入子產品多了,可讀性就下降了,會不知道那個函數來自那個子產品,如執行個體4 或>>> from math import * >> pow(3,2)

執行個體4:

>> from math import pow as pingfang ##對pow重命名,使用pingfang()就相當于在使用pow() >> pingfang(3,2)

引入多個函數,标準做法:

執行個體5:

>> from math import pow,e,pi >> pow(e,pi) ##e是歐拉數;pi是π(pai) 23.140692632779263

指派語句

a = 3 就是一個指派語句

執行個體6:

>> x,y,z = "wtf",1,["love","you"] >> x 'wtf' >> y >> z ['love', 'you'] 執行個體7: >> a ('love', 'datagrand') >> type(a) <type 'tuple'>

if--條件語句

執行個體8:

>> a = 4 >> if a != 5: ##冒号是必須的,隻有傳回的是True,才會執行下面的語句 ... print a ##要縮進四個空格

if...elif...else

基本樣式結構:

if 條件1:

語句塊1

elif 條件2:

語句塊2

elif 條件3:

語句塊3

。。。

else:

語句塊4

執行個體9:

#!/usr/bin/env python

#coding:utf-8

print "Please enter any integer number:"

##raw_input() The number entered is a string

##int() Convert the string to an integer

number = int(raw_input())

if number == 10:

print "The number you entered is:%d" %number

print "you are great!"

elif number > 10:

print "The number you entered is:{}".format(number)

print "The number is more than 10."

elif number < 10:

print "The number is less than 10."

else:

print "are you a human?"

三元操作符

文法:a = b if c else d

(1)如果c為真,那麼執行a = b

(2)如果c為假,那麼執行a = d

執行個體10:

>> name = "wtf" if "didi" else "zhao" ##if "didi"相當于if bool("didi") >> name >> name = "wtf" if "" else "zhao" ## if ""相當于if bool("") ps:兩個引号之間沒有空格 'zhao'

for循環

文法:

for 循環規則:

操作語句

執行個體11:

for循環--字元串

>> hello = "world" >> for i in hello: ... print i  w o r l d

執行個體12:

for循環--清單

>> wtf_python = ["data","grand","welcome you",""] >> wtf_python ['data', 'grand', 'welcome you', ''] >> for world in wtf_python: ... print world data grand welcome you >>

執行個體13:

for循環--元組

>> c = ("wtf","datagrand")  >> type(c) >> for i in c: wtf datagrand

執行個體14:

for循環--字典

>> d = {"wtf":"python","didi":"data"} >> type(d) <type 'dict'> >> for i in d: didi

數字是不能進行for循環的

因為int對象是不可疊代的,即for循環所應用的對象,應該是可疊代的。

執行個體15:

for循環--數字(不可for循環)

>> for i in 123: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable

判斷一個對象是否可疊代的方法:

執行個體16:

>> import collections >> isinstance(123,collections.Iterable) False ##不可疊代 >> isinstance([1,2,3],collections.Iterable) True ##可疊代

range

range(start,stop[,step])

文法說明:

start:開始數值,預設為0,也就是如果不寫這項,就是認為start=0

stop:結束的數值,必須要寫的。

step:變化的步長,預設是1,也就是不寫,就是認為步長為1.堅決不能為0.

range()函數特點:

(1)這個函數可以建立一個數字元素組成的清單;

(2)常用于for循環

(3)函數的參數必須是整數,預設從0開始。

(4)step預設是1.如果不寫,就是按照此值;

(5)step不能為零,如果等于零就報錯;

(6)如果step是正數,傳回清單的最後的值不包含stop值,即start+istep這個值小于stop;如果step是負數,start+istep的值大于stop。

執行個體17:

>> range(5) [0, 1, 2, 3, 4] >> range(0,5) >> range(0,5,1) >> range(1,5) [1, 2, 3, 4] >> range(1,5,2) [1, 3] >> range(0,-9,-1) [0, -1, -2, -3, -4, -5, -6, -7, -8] 執行個體18: 找出小于100的能夠被3整除的正整數 wtf = [] for n in range(1,100): if n % 3 == 0: wtf.append(n) print wtf 執行: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]      本文轉自品鑒初心51CTO部落格,原文連結: http://blog.51cto.com/wutengfei/2060129 ,如需轉載請自行聯系原作者