1. 程式人生 > >python學習筆記(九)之語句1

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

如果 red 開始 就是 整除 for 循環 個數 hello 基本

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
>> for i in [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)
9.0

實例4:

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

>> pingfang(3,2)
9.0

引入多個函數,標準做法:
實例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
1
>> z
[‘love‘, ‘you‘]
實例7:
>> a
(‘love‘, ‘datagrand‘)

>> type(a)
<type ‘tuple‘>

if--條件語句
實例8:

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

if...elif...else
基本樣式結構:
if 條件1:
語句塊1
elif 條件2:
語句塊2
elif 條件3:
語句塊3
。。。
else:
語句塊4
實例9:

cat shiyan.py

#!/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 you entered is:{}".format(number)
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
‘wtf‘
>> name = "wtf" if "" else "zhao" ## if ""相當於if bool("") ps:兩個引號之間沒有空格
>> name
‘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)
<type ‘tuple‘>
>> for i in c:
... print i
...
wtf
datagrand

實例14:
for循環--字典

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

數字是不能進行for循環的
因為int對象是不可叠代的,即for循環所應用的對象,應該是可叠代的。
實例15:
for循環--數字(不可for循環)

>> for i in 123:
... print i
...
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)
[0, 1, 2, 3, 4]
>> range(0,5,1)
[0, 1, 2, 3, 4]
>> 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整除的正整數

cat range.py

#!/usr/bin/env python
#coding:utf-8
wtf = []
for n in range(1,100):
if n % 3 == 0:
wtf.append(n)
print wtf
執行:

python range.py

[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]

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