1、數據類型python
數字類型:
整型int
用來表示:等級,年齡,×××號,學號,id號ide
level=10 print(type(level),id(level),level) <class 'int'> 1993698608 10
#浮點型float
#用來表示:身高,體重,薪資ui
salary=3.1 height=1.80 print(id(salary),type(height),salary) 2996881593040 <class 'float'> 3.1
# 字符串str:包含在引號(單引號,雙引號,三引號)內的一串字符
# 用來表示:名字,家庭住址,描述性的數據spa
name='egon' sex="woman" des="""hobby:man,play,read""" print(name,sex,des,type(name),type(sex),type(des)) egon woman hobby:man,play,read <class 'str'> <class 'str'> <class 'str'>
#字符串拼接:+,*ci
# s1='hello ' # s2="word" # print(s1+s2) hello word # s3="""s_jun """ # print(s3*3) s_jun s_jun s_jun
#列表:定義在[]中括號內,用逗號分隔開多個值,值能夠是任意類型
#用來存放多個值:多個愛好,多我的名字符串
stu_names=['egon','hobby','age'] print(id(stu_names),type(stu_names),stu_names,stu_names[1]) 2389078272136 <class 'list'> ['egon', 'hobby', 'age'] hobby user_info=['egon',18,['read','music','play','dancing']] print(user_info[2][1]) music
#字典:定義{}內用逗號分隔開,每個元素都是key:value的形式,其中value能夠是任意類型,而key必定要是不可變類型input
user_info={'name':'egon','age':18,'hobbies':['read','music','dancing','play']} print(type(user_info),user_info['name'],id(user_info),user_info['hobbies'][3]) <class 'dict'> egon 2025116757160 play
info={ 'name':'egon', 'hobbies':['play','sleep'], 'company_info':{ 'name':'Oldboy', 'type':'education', 'emp_num':40, } } print(info['company_info']['name']) Oldboy
students=[ {'name':'alex','age':38,'hobbies':['play','sleep']}, {'name':'egon','age':18,'hobbies':['read','sleep']}, {'name':'wupeiqi','age':58,'hobbies':['music','read','sleep']}, ] print(students[1]['hobbies'][0])
students={ 'alex':{ 'age':84, 'hobbies':['play','sleep'] }, 'egon':{ 'age':18, 'hobbies':['play',] } } print(students['egon']['age']) 18
#布爾類型bool:True,False
#用途:判斷cmd
age_of_oldboy=18 inp_age=input('your age: ') inp_age=int(inp_age) if inp_age > age_of_oldboy: print('猜大了') elif inp_age < age_of_oldboy: print('猜小了') else: print('猜對了')
# 布爾類型的重點知識!!!:全部數據類型,自帶布爾值
#只有三種類型的值爲False
# 0
# None
# 空:'',[],{}it
if '': print('0===>') if []: print('[]') if {}: print('{}') if None: print('0===>None') if 0: print('00')
#其他所有爲真io
if ['',]: print('1====>') if {'':'',}: print('2===?') if True: print('3===>?')
# 可變類型與不可變類型
# 可變:在id不變的狀況,值能夠改變
×××:
x=1 print(id(x),type(x),x) x=2 print(id(x),type(x),x) ----------------------------------- 1993698320 <class 'int'> 1 1993698352 <class 'int'> 2
列表:
x=['a','b','c'] print(id(x),type(x),x) x[2]=10 print(x) print(id(x),type(x),x) --------------------------------- 2467516409224 <class 'list'> ['a', 'b', 'c'] ['a', 'b', 10] 2467516409224 <class 'list'> ['a', 'b', 10]
字典:
dic={'x':1,'y':2} print(id(dic),type(dic),dic) dic['x']=111111111 print(id(dic),type(dic),dic)
# 不可變類型:數字,字符串
# 可變類型:列表,字典
# dic={[1,2,3]:'a'}
2、格式化輸出
name="s_jun" age=20 print('my name is %s my age is %s' %(name,age)) my name is s_jun my age is 20
print('my name is %s my age is %s'%('egon',18)) print('my name is %s my age is %d'%('egon',18)) x='my name is %s my age is %d' %('egon',18) print(x) ----------------------------------- my name is egon my age is 18 my name is egon my age is 18 my name is egon my age is 18
name="s_jun" msg=""" ------------ info of %s ----------- Name : %s ------------- end ----------------- """%(name,name) print(msg) ------------------------------------------------------------------------ ------------ info of s_jun ----------- Name : s_jun ------------- end -----------------
age=20 A=' %s '%(age) print(A) ------------------------------------- 20
3、基本運算符
print(10/3) print(10//3) print(10%3) print(3**3) ----------------------- 3.3333333333333335 3 1 27
增量賦值
age=18 age+=2 # age=age+2 print(age) age-=10 #age=age-10 print(age) ----------------------- 20 10
#邏輯運算
#and:邏輯與,and用於鏈接左右兩個條件,只有在兩個條件判斷的結果都爲True的狀況下,and運算最終的結果才爲True
print(1 > 2 and 3 > 4) print(2 > 1 and 3 > 4) print(True and True and True and False) ------------------------------------------- False False False
#or:邏輯或,有一個爲真結果就爲真
print(True or False) print(True or False and False) print((True or False) and False) print(not 1 > 2) ------------------------------------ True True False True
4、流程控制之if
sex='female' age=20 is_beutiful=True if sex == 'female' and age > 18 and age < 26 and is_beutiful: print('表白....') -------------- 表白....
sex='female' age=20 is_beutiful=True if sex == 'female' and age > 18 and age < 26 and is_beutiful: print('表白....') else: print('阿姨好') ------------------------------- 表白....
age_of_oldboy=18 inp_age=input('your age: ') inp_age=int(inp_age) if inp_age > age_of_oldboy: print('猜大了') elif inp_age < age_of_oldboy: print('猜小了') else: print('猜對了')
username='s_jun' password='123' inp_name=input('name>>: ') inp_pwd=input('password>>: ') if inp_name == username and inp_pwd == password: print('login successfull') else: print('user or password not vaild')
sex='female' age=20 is_beutiful=True is_successful=False if sex == 'female' and age > 18 and age < 26 and is_beutiful: print('表白....') if is_successful: print('在一塊兒') else: print('對不起,我也不喜歡你,我逗你玩呢...') else: print('阿姨好') ------------------------------------------ 表白.... 對不起,我也不喜歡你,我逗你玩呢...
''' 若是:成績>=90,那麼:優秀 若是成績>=80且<90,那麼:良好 若是成績>=70且<80,那麼:普通 其餘狀況:不好 ''' score=89 if score >= 90: print('優秀') elif score >= 80: print('良好') elif score >= 70: print('普通') else: print('不好') ----------------------- 良好
5、流程控制之while
while:條件循環
import time count=1 while count < 3: print('=====>',count) time.sleep(0.1)
count=1 while count <= 3: print('=====>',count) count+=1
#break:跳出本層循環
age_of_oldboy=18 while 1: inp_age=input('your age: ') inp_age=int(inp_age) if inp_age > age_of_oldboy: print('猜大了') elif inp_age < age_of_oldboy: print('猜小了') else: print('猜對了') break
age_of_oldboy=18 count=0 while count < 3: inp_age=input('your age: ') inp_age=int(inp_age) if inp_age > age_of_oldboy: print('猜大了') elif inp_age < age_of_oldboy: print('猜小了') else: print('猜對了') break count+=1 print('猜的次數',count)
age_of_oldboy=18 count=0 while True: if count == 3: print('try too many times') break inp_age=input('your age: ') inp_age=int(inp_age) if inp_age > age_of_oldboy: print('猜大了') elif inp_age < age_of_oldboy: print('猜小了') else: print('猜對了') break count+=1 print('猜的次數',count)
#continue:跳過本次循環,進入下一次循環
count=1 while count < 5: #3 if count == 3: count += 1 continue print(count) count+=1
while True: print('=========>') continue print('=========>') print('=========>') print('=========>')
while True: inp_name=input('name>>: ') inp_pwd=input('password>>: ') if inp_name == "s_jun" and inp_pwd == "123": print('login successfull') while True: cmd=input('cmd>>>: ') if cmd == 'quit': break print('%s 命令正在執行...' %cmd) break else: print('user or password not vaild')
tag=True while tag: inp_name=input('name>>: ') inp_pwd=input('password>>: ') if inp_name == "s_jun" and inp_pwd == "123": print('login successfull') while tag: cmd=input('cmd>>>: ') if cmd == 'quit': tag=False continue # break print('%s 命令正在執行...' %cmd) else: print('user or password not vaild')