您好,登录后才能下订单哦!
一、数据类型
数字类型:
整型int
用来表示:等级,年龄,×××号,学号,id号
level=10 print(type(level),id(level),level) <class 'int'> 1993698608 10
#浮点型float
#用来表示:身高,体重,薪资
salary=3.1 height=1.80 print(id(salary),type(height),salary) 2996881593040 <class 'float'> 3.1
# 字符串str:包含在引号(单引号,双引号,三引号)内的一串字符
# 用来表示:名字,家庭住址,描述性的数据
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'>
#字符串拼接:+,*
# 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一定要是不可变类型
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
#用途:判断
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
# 空:'',[],{}
if '': print('0===>') if []: print('[]') if {}: print('{}') if None: print('0===>None') if 0: print('00')
#其余全部为真
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'}
二、格式化输出
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
三、基本运算符
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
四、流程控制之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('很差') ----------------------- 良好
五、流程控制之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')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。