判斷語句
if-else
if 條件:
滿足條件時要做的事情1
滿足條件時要做的事情2
滿足條件時要做的事情3
...(省略)...
else:
不滿足條件時要做的事情1
不滿足條件時要做的事情2
不滿足條件時要做的事情3
...(省略)...
elif
if xxx1:
事情1
elif xxx2:
事情2
elif xxx3:
事情3
注:elif可以和else搭配一起使用。
if xxx1:
事情1
elif xxx2:
事情2
else:
事情3
if嵌套
if 條件1:
滿足條件1 做的事情1
滿足條件1 做的事情2
...(省略)...
if 條件2:
滿足條件2 做的事情1
滿足條件2 做的事情2
...(省略)...
注:
- 外層的if判斷霞丧,也可以是if-else
- 內(nèi)層的if判斷呢岗,也可以是if-else
- 根據(jù)實際開發(fā)的情況,進(jìn)行選擇
示例:
chePiao = 1 # 用1代表有車票蛹尝,0代表沒有車票
daoLenght = 9 # 刀子的長度后豫,單位為cm
if chePiao == 1:
print("有車票,可以進(jìn)站")
if daoLenght < 10:
print("通過安檢")
else:
print("沒有通過安檢")
else:
print("沒有車票突那,不能進(jìn)站")
循環(huán)語句
while循環(huán)
while 條件:
條件滿足時挫酿,做的事情1
條件滿足時,做的事情2
條件滿足時愕难,做的事情3
...(省略)...
示例:
i = 0
while i<5:
print("當(dāng)前是第%d次執(zhí)行循環(huán)"%(i+1))
print("i=%d"%i)
i+=1
while循環(huán)嵌套
while 條件1:
條件1滿足時早龟,做的事情1
條件1滿足時,做的事情2
條件1滿足時猫缭,做的事情3
...(省略)...
while 條件2:
條件2滿足時葱弟,做的事情1
條件2滿足時,做的事情2
條件2滿足時猜丹,做的事情3
...(省略)...
示例:打印如下圖形
*
* *
* * *
* * * *
* * * * *
代碼:
i = 1
while i<=5:
j = 1
while j<=i:
print("* ",end='')
j+=1
print("\n")
i+=1
for循環(huán)
for 臨時變量 in 列表或者字符串等:
循環(huán)滿足條件時執(zhí)行的代碼
else:
循環(huán)不滿足條件時執(zhí)行的代碼
示例:
name = 'hello'
for x in name:
print(x)
break和continue
- break的作用:用來結(jié)束整個循環(huán)
- continue的作用:用來結(jié)束本次循環(huán)芝加,緊接著執(zhí)行下一次的循環(huán)
- break/continue只能用在循環(huán)中,除此以外不能單獨使用
- break/continue在嵌套循環(huán)中射窒,只對最近的一層循環(huán)起作用
字符串
python中字符串的格式
如下定義的變量a藏杖,存儲的是字符串類型的值.
a = "hello"
或者
a = 'hello'
注:Python字符串雙引號或者單引號皆可
字符串輸出
a = 'hello'
print a
字符串輸入
- Python2:raw_input()将塑、input()
- Python3:input()
字符串的下標(biāo)
字符串實際上就是字符的數(shù)組,如果有字符串:name = 'abcdef',在內(nèi)存中的實際存儲如下:
如果想取出部分字符制市,那么可以通過下標(biāo)的方法(注意python中下標(biāo)從0開始):
name = 'abcdef'
print(name[0])
print(name[1])
print(name[2])
字符串的切片
切片是指對操作的對象截取其中一部分的操作抬旺。字符串、列表祥楣、元組都支持切片操作开财。
切片的語法:[起始:結(jié)束:步長],選取的區(qū)間屬于左閉右開型误褪,即從"起始"位開始责鳍,到"結(jié)束"位的前一位結(jié)束(不包含結(jié)束位本身)。
示例1:
name = 'abcdef'
print(name[0:3]) # 取 下標(biāo)0~2 的字符
print(name[0:5]) # 取 下標(biāo)為0~4 的字符
print(name[3:5]) # 取 下標(biāo)為3兽间、4 的字符
print(name[2:]) # 取 下標(biāo)為2開始到最后的字符
print(name[1:-1]) # 取 下標(biāo)為1開始 到倒數(shù)第2個之間 的字符
示例2:
>>> a = "abcdef"
>>> a[:3]
'abc'
>>> a[::2]
'ace'
>>> a[5:1:2]
''
>>> a[1:5:2]
'bd'
>>> a[::-2]
'fdb'
>>> a[5:1:-2]
'fd'
字符串常見操作
定義字符串mystr = 'hello world python2 and python3'历葛,以下是常見的操作。
find
檢測str是否包含在mystr中嘀略,如果是返回開始的索引值恤溶,否則返回-1。
mystr.find(str, start=0, end=len(mystr))
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.find("world")
6
>>> mystr.find("world", 0, 7)
-1
index
跟find()方法一樣帜羊,只不過如果str不在 mystr中會報一個異常
mystr.index(str, start=0, end=len(mystr))
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.index("world", 0, 7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
count
返回 str在start和end之間 在 mystr里面出現(xiàn)的次數(shù)
跟find()方法一樣咒程,只不過如果str不在 mystr中會報一個異常
mystr.count(str, start=0, end=len(mystr))
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.count("python")
2
replace
把mystr中的str1替換成str2,如果count指定,則替換不超過count次讼育。執(zhí)行replace后帐姻,mystr不變。
mystr.replace(str1, str2, mystr.count(str1))
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.replace("python", "PYTHON", mystr.count("python"))
'hello world PYTHON2 and PYTHON3'
>>> mystr.replace("PYTHON", "python", 1)
'hello world python2 and python3'
>>> mystr.replace("python", "PYTHON", 1)
'hello world PYTHON2 and python3'
split
以str為分隔符切片mystr奶段,如果maxsplit有指定值饥瓷,則僅分隔maxsplit 個以str為分隔子字符串,也就是maxsplit+1個字符串痹籍。
mystr.split(str=" ", maxsplit)
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.split(" ")
['hello', 'world', 'python2', 'and', 'python3']
>>> mystr.split(" ", 2)
['hello', 'world', 'python2 and python3']
capitalize
把字符串的第一個字符大寫
>>> mystr = 'hello world python2 and python3'
>>> mystr.capitalize()
'Hello world python2 and python3'
title
把字符串的每個單詞首字母大寫
>>> a = "hello"
>>> a.title()
'Hello'
startswith
檢查字符串是否是以obj開頭, 是則返回True呢铆,否則返回False。
mystr.startswith(obj)
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.startswith("hello")
True
>>> mystr.startswith("Hello")
False
endswith
檢查字符串是否以obj結(jié)束词裤,如果是返回True,否則返回 False.
mystr.endswith(obj)
lower
轉(zhuǎn)換 mystr 中所有大寫字符為小寫
mystr.lower()
upper
轉(zhuǎn)換mystr中的小寫字母為大寫
mystr.upper()
ljust
返回一個原字符串左對齊,并使用空格填充至長度width的新字符串刺洒。當(dāng)width小于字符串長度,不會切片吼砂。
mystr.ljust(width)
示例:
>>> mystr = "hello world"
>>> mystr.ljust(15)
'hello world '
>>> mystr.ljust(6)
'hello world'
center
返回一個原字符串居中,并使用空格填充至長度 width 的新字符串
mystr.center(width)
lstrip
刪除mystr左邊的空白字符。
mystr.lstrip()
rstrip
刪除 mystr 字符串末尾的空白字符鼎文。
mystr.rstrip()
strip
刪除mystr字符串兩端的空白字符渔肩。
mystr.strip()
rfind
類似于find()函數(shù),不過是從右邊開始查找拇惋,查找到第一個符合的返回其地址周偎。
mystr.rfind(str, start=0, end=len(mystr))
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.rfind("python")
24
rindex
對應(yīng)的抹剩,類似于 index(),不過是從右邊開始蓉坎。
mystr.rindex( str, start=0,end=len(mystr))
partition
把mystr以str分割成三部分,str前澳眷,str和str后。
mystr.partition(str)
示例:
>>> mystr = 'hello world python2 and python3'
>>> mystr.partition('python')
('hello world ', 'python', '2 and python3')
rpartition
類似于partition()函數(shù),不過是從右邊開始蛉艾。
mystr.rpartition(str)
splitlines
按照行分隔钳踊,返回一個包含各行作為元素的列表。
mystr.splitlines()
示例:
>>> mystr = 'hello\nworld'
>>> print mystr
hello
world
>>> mystr.splitlines()
['hello', 'world']
isalpha
如果mystr所有字符都是字母則返回True,否則返回False勿侯。
mystr.isalpha()
isdigit
如果mystr所有字符都是數(shù)字則返回True,否則返回False拓瞪。
mystr.isdigit()
isalnum
如果mystr所有字符都是字母或數(shù)字則返回True,否則返回False。
mystr.isalnum()
isspace
如果mystr中只包含空格助琐,則返回True祭埂,否則返回False。
mystr.isspace()
join
str中每個字符后面插入mystr,構(gòu)造出一個新的字符串兵钮。
mystr.join(str)
示例1:
>>> mystr = ' '
>>> str = 'abc'
>>> mystr.join(str)
'a b c'
示例2:
>>> mystr = ['xixi', 'haha']
>>> str = 'abc'
>>> str.join(mystr)
'xixiabchaha'