python
是一種解釋行,面向?qū)ο蟮膭?dòng)態(tài)數(shù)據(jù)類型的高級(jí)程序設(shè)計(jì)語(yǔ)言,在1989
年發(fā)明;shell
;python
提示符,互動(dòng)執(zhí)行程序;python
的特點(diǎn):Python
代碼閱讀清晰;GUI
變成以及系統(tǒng)調(diào)用;Python
代碼嵌入到C/C++
程序中;C
程序相比非常慢,Python
屬于解釋性語(yǔ)言,代碼在執(zhí)行時(shí)會(huì)一行一行翻譯成CPU
機(jī)器代碼,翻譯過(guò)程十分耗時(shí);C
語(yǔ)言發(fā)布成二進(jìn)制的可執(zhí)行文件;#
進(jìn)行注釋;'''注釋多行 '''
,寫(xiě)代碼時(shí),不應(yīng)該存在中文字符;'''注釋多行'''
;print
:打印雙引號(hào)之間的內(nèi)容;print('hello world')
,
進(jìn)行分割print('hi python','hello python')
print(19)print(10 + 8)print('10 + 8 ', 18)
input
:用于從標(biāo)準(zhǔn)輸入獲取變量的值age=input('please input your age ')print('age = ', age)
輸出的結(jié)果為
Python的數(shù)據(jù)類型
number
:
整數(shù):Python
可以處理任意大小的整數(shù),同時(shí)也包括負(fù)整數(shù),在程序中的寫(xiě)法和數(shù)學(xué)表達(dá)是一致的;
連續(xù)定義多個(gè)變量
num1 = 10num3 = num2 = num1 //這樣變量的地址是一樣的num6,num7 = 6,6這兩個(gè)變量的地址也是一樣的;print(id(num2),id(num3));93941385645920 93941385645920
float1 = 1.1float2 = 2.30000print(type(float1))print(float1+float2)
結(jié)果為
復(fù)數(shù):表示的實(shí)數(shù)部分和虛數(shù)部分組成;
數(shù)值類型轉(zhuǎn)換:
print(int(1.9))print(int('123'))print(int('+123'))print(int('-112'))
abs()
:用于返回某個(gè)數(shù)的絕對(duì)值;print((num2>num3)-(num2<num3))
:用于表示比較這兩個(gè)數(shù)的大小,返回值為1 0 -1
;max(arg1,arg2,arg3..)
:用于返回給定參數(shù)的最大值;min(arg1,arg2,arg3..)
:用于返回給定參數(shù)的最小值;pow(x,y)
:用于計(jì)算x^y
的結(jié)果;round(x,y)
:表示保留y
位,x
的值,省略表示的就是只保留整數(shù)位;math
庫(kù):數(shù)學(xué)運(yùn)算相關(guān)的庫(kù);math.ceil(18.1)
:表示向上取整;math.flooe()
:表示向下取整;math.modf(22.3)
:用于將一個(gè)數(shù)拆分為整數(shù)[浮點(diǎn)型]部分和小數(shù)部分;math.sqrt()
:表示用于開(kāi)平方;print(max(num2,num3))print(min(num2,num3))print(pow(num2,num3))print(round(3.456,1))
print(random.choice([1,3,5,7,9,'aa'])) //在這些數(shù)里面選出隨機(jī)數(shù)print(random.choice(range(5)))print(random.choice('sunck'))print(random.choice(range(100))+1)print(random.randrange(1,100,2)) //1-->100生成隨機(jī)數(shù)[包含1,但是不包含100],步長(zhǎng)為2;print(random.randrange(100)) //start默認(rèn)是0,步長(zhǎng)默認(rèn)為1print(random.random()) //隨機(jī)生成[0,1)之間的浮點(diǎn)數(shù);
list = [1,2,3,4,5]random.shuffle(list)print(list)
random.uniform(3,9) //范圍是[3,9]
import randomnum = int(input('please input your number '))res = random.choice(range(100)) + 1if num == res: print('you are lucky')else: print('not you')
string
: 表示字符串,python
里面沒(méi)有字符的概念;str1 = 'hello today'str2 = 'hello tomorrow'
+
:用于連接字符串str1 = 'hello today 'str2 = 'hello tomorrow'print(str1)str3 = str1 + str2
*
:表示字符串的多次輸出,也就是拼接str3 = 'oo 'print(str3*3)
[]
用于獲取某個(gè)單個(gè)字符str1 = 'hello today 'str2 = 'hello tomorrow'print(str1[1])str1[1]='a' //字符串不可變?cè)瓌t,所以一定會(huì)出錯(cuò);
str2 = 'hello tomorrow,it's a new day'print(str2[2:14])print(str2[:13])print(str2[10:])
in
的使用str2 = 'hello tomorrow,it's a new day'print('hello' in str2)print('hi' in str2)
\n
:用于表示換行;print('num = %d \nstr = %s\nfnum =%f ' %(num,str1,fnum2))print('today is \\n a new day')print('today is a \'new\' day')print('today is a 'new' day')
print('''today is a goodday''')
\t
:表示制表符,默認(rèn)是4
個(gè)空格print('today is a \t good day')
r
:如果字符串中,存在很多字符進(jìn)行轉(zhuǎn)義,為了簡(jiǎn)化,允許使用r
表示內(nèi)部的字符串默認(rèn)不轉(zhuǎn)義print(r'\\\\t\\\\')
eval()
:用于將字符串當(dāng)成有效的表達(dá)式來(lái)求值,并且返回計(jì)算結(jié)果,用于將字符串轉(zhuǎn)換成整數(shù)不能夠用于處理字母;num = eval('1234')print(num)print(type(num))num = eval('12+34')print(num)print(type(num)) //輸出結(jié)果為46
len()
:用于返回字符個(gè)數(shù),并不計(jì)算字節(jié)個(gè)數(shù)print(len('today is a new day'))
lower(str)
:轉(zhuǎn)換字符串中的大寫(xiě)字母為小寫(xiě)字母str2 = 'Hello tomoRRow,it's a new day'print(str2.lower()) //并不改變?cè)凶址?
upper()
:將小寫(xiě)字母轉(zhuǎn)換為大寫(xiě)字母print(str2.upper())
swapcase()
:用于將大寫(xiě)轉(zhuǎn)換為小寫(xiě),用于將小寫(xiě)轉(zhuǎn)換為大寫(xiě)print(str2.swapcase())
capitalize()
:只有將首字母大寫(xiě),其他字母都是小寫(xiě)print(str2.capitalize())Hello tomorrow,it's a new day
title()
:每個(gè)單詞的首字母大寫(xiě)print(str2.title())Hello Tomorrow,It'S A New Day
center(width,filechar)
:表示寬度和填充字符print(str1.center(40,'*'))**************hello today**************
ljust()
:表示進(jìn)行左對(duì)齊,默認(rèn)是空格填充print(str1.ljust(20,'%'))hello today%%%%%%%%%
rjust()
:表示返回一個(gè)指定長(zhǎng)度的字符串,是右對(duì)齊,默認(rèn)使用空格填充print(str1.rjust(20,'*'))*********hello today
zfill()
:返回指定字符串的長(zhǎng)度,字符串右對(duì)齊,默認(rèn)使用0
進(jìn)行填充print(str1.zfill(20))000000000hello today
count(str[,start][,end])
:可以用于在指定字符串的位置中查找指定字符串出現(xiàn)的位置str2 = 'Hello tomotoRRow,it's a new day'print(str2.count('to',5,len(str2)))2
find(str[,start][,end[)
:用于查找指定字符串重做開(kāi)始第一次出現(xiàn)的位置,如果不存在就返回-1
str2 = 'Hello tomotoRRow,it's a new day'print(str2.find('to',7,len(str2)))10
rfind()
:用法和上面一樣,表示從由向左進(jìn)行查找print(str2.rfind('to'))10
index(str,start=0,end-len=(str))
:用法和上面一樣,如果指定字符串不存在,就會(huì)報(bào)錯(cuò);str2 = 'Hello tomotoRRow,it's a new day'print(str2.index('too'))Traceback (most recent call last): File '/root/PycharmProjects/test/first.py', line 5, in <module> print(str2.index('too'))ValueError: substring not found
rindex()
:表示從右向左進(jìn)行查找,如果找不到,就會(huì)出錯(cuò)
lstrip()
:用于去掉字符串左側(cè)指定的字符,默認(rèn)為空格
str2 = '*******Hello tomotoRRow,it's a new day'print(str2.lstrip('*'))Hello tomotoRRow,it's a new day
split(' ',num)
:表示按照某個(gè)關(guān)鍵字將字符串進(jìn)行切割,如果指定num
表示截取指定的num
個(gè)字符串;string1 = 'today is a new day, but where are you'string2 = string1.split(' ')wordnum = 0for i in string2: if len(i) > 0: wordnum +=1print(wordnum)9
splitlines(true | false)
:表示按照\r, \r\n, \
分隔,true
表示返回?fù)Q行符,false
表示不返回?fù)Q行符;string1 = '''today is an new day, but where are youthe next day is an new day hello tomorrow'''print(string1.splitlines())print(string1.splitlines(True))['today is an new day, but where are you', 'the next day is an new day ', 'hello tomorrow']['today is an new day, but where are you\n', 'the next day is an new day \n', 'hello tomorrow\n']
join
:用于將列表組合成為字符串list1 = ['today is an new day, but where are you', 'the next day is an new day ', 'hello tomorrow']string1 = '\n'.join(list1)print(string1)today is an new day, but where are youthe next day is an new day hello tomorrow
max()
:表示用于返回字符串里面的最大值;min()
:表示用于返回字符串里面的最小值,按照ASCII
進(jìn)行比較;replace(old,new,count)
:表示要替換的值,替換的值,替換的次數(shù),默認(rèn)進(jìn)行全部替換,如果指定了count
,那么只進(jìn)行前count
的替換;string1 = 'today is an new day'string2 = string1.replace('an','a',1)print(string2)
maketrans('映射前','映射后')
:表示創(chuàng)建字符串映射表,是一張表,每個(gè)字符對(duì)應(yīng)進(jìn)行替換,是按照字符為單位的,替換前后的字符數(shù)必須相等;translate()
:表示執(zhí)行上面的映射string1 = str.maketrans('an', '6o')string2 = 'today is an an an an new day'string3 = string2.translate(string1)print(string3)tod6y is 6o 6o 6o 6o oew d6y
這種替換較少使用;
startwith('str', num1, num2)
:表示從num1
到num2
進(jìn)行查找,是否是以str
開(kāi)頭的字符串,如果沒(méi)有指定范圍,默認(rèn)是整個(gè)字符串;
string2 = 'today is an an an an new day'print(string2.startswith('an', 3, 10))False
endwith('str',
num1,
num2)
:使用同上,用于判斷是否是str
結(jié)尾的encode('utf-8,'ignore')
string2 = 'today is an an an an new day'data = string2.encode('utf-8')print(data)print(type(data))解碼string3 = data.decode('utf-8')print(string3)b'today is an an an an new day'today is an an an an new day
解碼和編碼必須使用同樣的格式,否則會(huì)出錯(cuò);
isalpha()
:表示如果字符串中至少有一個(gè)字符,并且所有的字符都是字母,但會(huì)True
,否則返回False
;
isalnum
:表示字符串中至少有一個(gè)字符,并且所有的字符都是字母或者數(shù)字返回True
,否則返回False
;
isupper()
:表示字符串中至少有一個(gè)英文字符,且所有的英文字符必須都是大寫(xiě)的英文字母,返回True
,否則返回False
,如果包含數(shù)字也會(huì)返回True
islower()
:表示字符串中至少一個(gè)英文字符,并且所有的英文字符必須都是小寫(xiě)的英文字符;
istitle()
:表示如果字符串是標(biāo)題化的就返回True
,否則就是返回False
,標(biāo)題化表示單詞的第一個(gè)字母必須是大寫(xiě);
isdigit()
:如果字符串只包含數(shù)字,返回True
,否則返回False
;
isnumeric()
:字符串中只包含數(shù)字字符,返回為True
;
isdecimal()
:字符串中只包含十進(jìn)制字符;
isspace()
:如果字符串中只包含空格,返回True
,否則返回False
;
rstrip()
:作用和上面的一樣
ord()
:用于將字母轉(zhuǎn)換成為ACSII
chr()
:用于將某些ASCII
轉(zhuǎn)換成為值;
字符串進(jìn)行比較的規(guī)則:
ASCII
的大,那么就是比第二個(gè);ASCII
;boolean
:用于表示true
以及false
,有且只有這兩種值,
None
:是一個(gè)特殊值,但是不能夠使用0
來(lái)進(jìn)行表示;
list
列表用于存儲(chǔ)更多的數(shù)據(jù) ,列表表示一組有序的集合,并且允許時(shí)不同的類型
age = []print(age)[]age = [12, 23, 34, 45]print(age)[12, 23, 34, 45]list1 = [1, 2, 'sunck', True]print(list1)[1, 2, 'sunck', True]
0
開(kāi)始,并且訪問(wèn)不能夠越界;list1 = [1, 2, 'sunck', True]print(list1[1])list1[1] = 12print(list1)
3
層list2 = age + list1print(list2)
print(age * 3)[12, 23, 34, 45, 12, 23, 34, 45, 12, 23, 34, 45]
print(12 in age)
list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]print(list2[2:6])print(list2[2:])print(list2[:6])[3, 4, 5, 6][3, 4, 5, 6, 7, 8, 9][1, 2, 3, 4, 5, 6]
list33 = [[1,2,3],[4,5,6],[7,8,9]]print(list33[1][1])
append
:表示在末尾一次性追加另一個(gè)列表中的多個(gè)值;list1 = [1, 2, 3, 4, 5]list1.append(6)print(list1)list1.append([7, 8, 9])print(list1)[1, 2, 3, 4, 5, 6][1, 2, 3, 4, 5, 6, [7, 8, 9]]
list1 = [1, 2, 3, 4, 5]list1.extend([6, 7, 8])print(list1)[1, 2, 3, 4, 5, 6, 7, 8]
insert
: 在下標(biāo)處,添加一個(gè)元素,將列表當(dāng)做一個(gè)元素來(lái)處理list1 = [1, 2, 3, 4, 5]list1.insert(3, 100)print(list1)[1, 2, 3, 100, 4, 5]
list1 = [1, 2, 3, 4, 5]list1.insert(3, [100, 200])print(list1)[1, 2, 3, [100, 200], 4, 5]
pop(x=list[-1])
:默認(rèn)表示列表最后一個(gè)元素被彈走,可以指定下標(biāo)移除指定元素,并且返回刪除的數(shù)據(jù)list1 = [1, 2, 3, 4, 5]print(list1[-1])5list1.pop()print(list1)[1, 2, 3, 4]list1.pop(2)print(list1)[1, 2, 4]
remove()
:表示用于刪除指定元素第一個(gè)匹配的結(jié)果,首先會(huì)查找指定元素的值list1 = [1, 2, 3, 4, 4, 4, 5]list1.remove(4)print(list1)[1, 2, 3, 4, 4, 5]
clear()
:用于刪除列表中的所有元素list1 = [1, 2, 3, 4, 4, 4, 5]list1.clear()print(list1)[]
index()
:表示從列表中找出第一個(gè)匹配的索引list1 = [1, 2, 3, 4, 4, 4, 5]print(list1.index(3))print(list1.index(4, 2, 7))23
len()
:用于獲取列表里面的元素個(gè)數(shù)list1 = [1, 2, 3, 4, 4, 4, 5]print(len(list1))
max()
:用于獲取列表中的最大值;min()
:用于獲取列表中的最小值;count()
:用于查詢某個(gè)元素出現(xiàn)的次數(shù);reverse()
:用于進(jìn)行列表的倒序;sort()
:默認(rèn)進(jìn)行升序排序;list1 = [1, 2, 3, 4, 4, 4, 5]list2 = list1list2[3] = 300print(list2)print(list1)print(id(list1))print(id(list2))[1, 2, 3, 300, 4, 4, 5][1, 2, 3, 300, 4, 4, 5]140657342356296140657342356296
list27
和list28
里面保存的是堆區(qū)里面的同一段地址空間的首地址0x100
,其實(shí)訪問(wèn)的是同一片地址空間;list1 = [1, 2, 3, 4, 4, 4, 5]list2 = list1.copy()list2[3] = 300print(list2)print(list1)print(id(list1))print(id(list2))[1, 2, 3, 300, 4, 4, 5][1, 2, 3, 4, 4, 4, 5]140278067989320140278067987720
深拷貝表示的是內(nèi)存拷貝,
元組轉(zhuǎn)換為列表
list2 = list((1, 2, 3, 4, 5))print(list2)[1, 2, 3, 4, 5]
num = int(input())i = 2list1 = []if num == 1: print(1)while num != 1: if num % i == 0: list1.append(i) num //= i else: i += 1print(list1)
tuple2 = (1, 2, 3, 4, 'hello', True)
tuple1 = (1, )
0
開(kāi)始,并且不允許越界;print(tuple2[3]) //下標(biāo)為 0print(tuple2[-1]) //用于獲取最后一個(gè)元素 [-2]表示倒數(shù)第二個(gè)元素
tuple2 = (1, 2, 3, 4, 5, [7, 8, 9])tuple2[-1][0] = 50print(tuple2)(1, 2, 3, 4, 5, [50, 8, 9])
del tuple2
+
:表示相加print(tuple2 + tuple1)(1, 2, 3, 4, 5, [50, 8, 9], 1, 2, 3, 4)
*
:表示乘法tuple3 = (1, 2, 3)print(tuple3 * 3)(1, 2, 3, 1, 2, 3, 1, 2, 3)
in
: 判斷元素是否在元組里面tuple3 = (1, 2, 3)print( 1 in tuple3)
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9)print(tuple1[3:])print(tuple1[3:5])print(tuple1[:5])(4, 5, 6, 7, 8, 9)(4, 5)(1, 2, 3, 4, 5)
tuple2 = ((1, 2, 3, 4),(1, 2, 3, ),(1, 2, 3, ),(4, 5, 6, ))print(tuple2[1][1])
len()
:用于返回元組里面元素的個(gè)數(shù)tuple1 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
max()
:用于返回元組里面的最大值print(max(tuple1))
list1 = [1, 2, 3, 4, 5]tuple2 = tuple(list1)print(tuple2)(1, 2, 3, 4, 5)
for i in tuple2: print(i)
key-value
,對(duì)于key
key
必須是唯一的;key
:必須是不可變的對(duì)象;key
;list
是可變的,不能夠作為key
dict1 = {'tom':60, 'xiaoxiao':90, 'houhou':100}
key
來(lái)訪問(wèn)value
,通過(guò)這種方式取值,如果值不存在,就會(huì)報(bào)錯(cuò);print(dict1['jerry'])
print(dict1.get('houhouo'))
dict1['cat'] = 99 //如果字典里面內(nèi)部存在就是添加dict1['jerry'] = 90 //字典里面存在就是修改
dict1.pop('houhou') //刪除的前提是必須存在
dict1 = {'tom':60, 'jerry':90, 'houhou':100}print(dict1.get('houhouo'))dict1['cat'] = 99dict1['jerry'] = 90for key in dict1: print(key, dict1[key])for value in dict1.values(): print(value)for key, value in dict1.items(): print(key, value)for key, values in enumerate(dict1): print(key, values)0 tom1 jerry2 houhou3 cat
dictionary
:list
相比,查找和插入的速度是極快的,不會(huì)隨著key-value
的增加而變慢;dict
在存儲(chǔ)數(shù)據(jù)時(shí),還需要存儲(chǔ)序號(hào),會(huì)導(dǎo)致內(nèi)存浪費(fèi);list
:w = 'the'str = 'this is a good day, hello the new day, the the a and what the weather;'d = {}list = str.split(' ')print(list)for value in list: need = d.get(value) if need == None: d[value] = 1 else: d[value] += 1print(d[w])print(d)w = 'the'str = 'this is a good day, hello the new day, the the a and what the weather;'d = {}list = str.split(' ')print(list)for value in list: need = d.get(value) if need == None: d[value] = 1 else: d[value] += 1print(d[w])print(d)['this', 'is', 'a', 'good', 'day,', 'hello', 'the', 'new', 'day,', 'the', 'the', 'a', 'and', 'what', 'the', 'weather;']4{'what': 1, 'good': 1, 'and': 1, 'day,': 2, 'this': 1, 'is': 1, 'the': 4, 'new': 1, 'hello': 1, 'a': 2, 'weather;': 1}
set
:表示集合;
標(biāo)識(shí)符
標(biāo)識(shí)符是字符串,但是字符串未必是標(biāo)識(shí)符;
標(biāo)識(shí)符遵守的規(guī)則:
Python
的關(guān)鍵字和保留字;Python3
中,允許使用非ASCII
來(lái)定義標(biāo)識(shí)符;表示符是用來(lái)給變量和函數(shù)進(jìn)行命名的;
查看關(guān)鍵字
import keywordprint(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
name = 初始值
,初始值是為了確定變量的類型,變量的類型根據(jù)初始值確定的;變量名 = 數(shù)據(jù)值
,這個(gè)是用來(lái)存儲(chǔ)數(shù)據(jù)值的;number1 = int(input('please input a number'))number2 = int(input('please input a number '))print('the sum is ',number1+number2)
顯示結(jié)果為
刪除變量
del name
,刪除之后,變量無(wú)法引用,用于釋放內(nèi)存空間;
del number1del number2
print(type(number1))<class 'int'>
print(id(age))140043296231312 //變量的首地址
常量:程序運(yùn)行期間不能夠改變的數(shù)據(jù),成為常量
運(yùn)算符和表達(dá)式
表達(dá)式:由變量,常量和運(yùn)算符組成的式子;
算術(shù)運(yùn)算表達(dá)式不會(huì)改變變量的值,
運(yùn)算符:
算術(shù)運(yùn)算符:+ - * /
,%
取模,**
求冪,//
取整;
賦值運(yùn)算符:=
,格式是變量 = 表達(dá)式
,計(jì)算了等號(hào)右側(cè)的表達(dá)式的值并且賦值給等號(hào)左側(cè)的變量;
復(fù)合運(yùn)算符:+= -= /= %= **= //=
位運(yùn)算符:將數(shù)值當(dāng)做二進(jìn)制數(shù)來(lái)進(jìn)行運(yùn)算;
&
:表示按位與運(yùn)算符,如果按位進(jìn)行運(yùn)算,兩個(gè)值都為1
,結(jié)果為1
,否則為0
;|
:表示二進(jìn)制位有一個(gè)為1
時(shí),結(jié)果為1
;^
:表示按位異或運(yùn)算符,兩個(gè)二進(jìn)制位不同時(shí),結(jié)果為1
;~
:表示按位取反運(yùn)算符;<<
:表示二進(jìn)制位向左移動(dòng)幾位,高位丟棄,低位補(bǔ)0
;0000 0000 0000 00100000 0000 0000 1000
* `>>`:表示各個(gè)二進(jìn)制向右移動(dòng)幾位,低位丟棄;
關(guān)系運(yùn)算符和關(guān)系運(yùn)算表達(dá)式的結(jié)果是true
和false
==
!=
>
<
>=
<=
邏輯運(yùn)算符:
表達(dá)式1 and 表達(dá)式2
:雙真為真;
表達(dá)式1 or 表達(dá)式2
:一個(gè)為真,就是真;
not 表達(dá)式
:真為假,假為真;
成員運(yùn)算符
in
:如果在指定的序列中,找到的指定的值,返回值為true
,否則為false
;
not in
:如果在指定的序列中,沒(méi)有找到的指定的值,返回值為true
,否則為false
;
身份運(yùn)算符
is
:判斷兩個(gè)標(biāo)識(shí)符是不是引用同一個(gè)對(duì)象;
is not
:判斷兩個(gè)標(biāo)識(shí)符是不是引用不同的對(duì)象;
運(yùn)算符優(yōu)先級(jí)
** --> ~ + - --> * / % // --> + - --> << >> --> & --> ^ | --> < > <= >= --> == != --> = --> is --> in --> not or and
判斷語(yǔ)句
if 表達(dá)式: 語(yǔ)句else: 語(yǔ)句
當(dāng)程序執(zhí)行到if 語(yǔ)句
時(shí),首先計(jì)算表達(dá)式的值,并且得到表達(dá)式的真假,表達(dá)式為真,執(zhí)行if
語(yǔ)句 ,如果表達(dá)式為假,則跳過(guò)if
語(yǔ)句,向后執(zhí)行;
假:0
以及 0.0
或者 ''
再或者 False
;
其余都為真;
判斷三位的水仙花數(shù)
num1 = int(input('please input a number '))a = num1 % 10b = num1 //10 % 10c = num1 //100if num1 == a**3 + b**3 + c**3: print('yes')else: print('no')
max = num1if num2 > max: max = num2if num3 > max: max = num3
while
循環(huán)while 表達(dá)式: 語(yǔ)句
while i < 100: sum += i i += 1print('down')print(sum)while index < len(string1) - 1: print(string1[index]) index += 1
if-elif-else
語(yǔ)句if 表達(dá)式1: 語(yǔ)句elif 表達(dá)式2: 語(yǔ)句2 . . .else: 語(yǔ)句n
elif
都是對(duì)上面條件的否定;while 表達(dá)式: 語(yǔ)句1else: 語(yǔ)句2
false
,會(huì)執(zhí)行else:
中的語(yǔ)句2
;for
循環(huán)語(yǔ)句:for 變量名 in 集合: 語(yǔ)句
range()
:列表生成器,用于生成數(shù)列,表示范圍[0,n)
;for i in range(2,20,2): 開(kāi)始位置默認(rèn)為0,結(jié)束位置,步長(zhǎng)默認(rèn)為1 print(i)
for index, i in enumerate([1, 2, 3, 4, 5]): print(index, i)
break
:用于跳出for
和while
的循環(huán),只能跳出距離最近的那一層循環(huán);continue
:用于跳過(guò)當(dāng)前循環(huán)的剩余語(yǔ)句,然后繼續(xù)下一次循環(huán),同樣只能夠跳過(guò)最近的一次循環(huán);turtle
:是一個(gè)簡(jiǎn)單的繪圖工具done()
:用于保持程序不結(jié)束;forward()
:表示向前移動(dòng),原點(diǎn)默認(rèn)是從中間開(kāi)始的;backward()
:表示向后移動(dòng);right()
:left()
goto(x,y)
:移動(dòng)到坐標(biāo)為x
,y
的位置speed()
:表示移動(dòng)的速度;up
:表示在移動(dòng)過(guò)程中,不畫(huà)圖down()
:表示開(kāi)始劃線setheading()
:改變朝向pencolor()
:用于表示顏色pensize()
:用于表示粗細(xì)reset()
:用于清空窗口,重置turtle
狀態(tài)clear()
:表示清空窗口,但是不會(huì)重置turtle
circle()
:表示用于繪制圓形,或者是五邊形turtle.begin_fill()
turtle.fillcolor('blue')
:表示填充什么顏色turtle.end_fill()
:表示結(jié)束填充undo()
:表示撤銷生一次動(dòng)作;hideturtle()
:表示隱藏海龜showtutle()
:表示用于顯示海龜screensize()
:表示更改顯示的屏幕大小聯(lián)系客服