一、数据类型与格式化输出、基本运算符、流程控制
一、数据类型
公司主营业务:网站建设、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出宝兴免费做网站回馈大家。
数字类型:
整型int
用来表示:等级,年龄,×××号,学号,id号
level=10 print(type(level),id(level),level)1993698608 10
#浮点型float
#用来表示:身高,体重,薪资
salary=3.1 height=1.80 print(id(salary),type(height),salary) 29968815930403.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
#字符串拼接:+,*
# 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['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])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) ----------------------------------- 19936983201 1993698352 2
列表:
x=['a','b','c'] print(id(x),type(x),x) x[2]=10 print(x) print(id(x),type(x),x) --------------------------------- 2467516409224['a', 'b', 'c'] ['a', 'b', 10] 2467516409224 ['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')
分享标题:一、数据类型与格式化输出、基本运算符、流程控制
网站链接:http://pwwzsj.com/article/iepojs.html