一、基本数据类型
1、什么是数据类型
数据的种类,不同种类的数据的存取机制不一样,用途也不一样
整型int 浮点型float 字符串类型str 列表类型list 字典类型dict 布尔类型bool
2、数据为何要分类型
数据是事物的状态,事物的状态是分为多种多样的,对应着就应该用不同类型的数据去记录。
3、数据类型
(1)整型int
age = 18
level = int ( 10 )
print ( level, type ( level) )
作用:年龄、等级、各种号码,个数
(2)浮点型float
salary = 3.1
print ( type ( salary) )
作用:身高、体重、薪资
(3)字符串类型str
定义:在"“或者’'或者”"" “”"或者’’’ ‘’'中间包含一系列字符
msg1= "你he xxx"
msg2= '你he xxx'
msg3= """
111
222
333
"""
print ( msg3)
print ( type ( msg3) )
print ( "my name is 'egon'" )
x= "abc"
y= "ddd"
print ( x+ y)
print ( "abc" * 3 )
print ( '=' * 50 )
print ( "hello" )
print ( '=' * 50 )
作用:记录一些描述性质的内容,比如名字、国籍、一段话
(4)列表类型
定义:在[ ]内逗号分割开多个任意类型的值
l= [ 111 , 3.1 , "abc123" , [ 'xxx' , 'yyy' , [ 333 , 444 ] ] ]
print ( type ( l) )
print ( l[ 0 ] )
print ( l[ - 1 ] )
print ( l[ - 2 ] )
print ( l[ - 1 ] [ 0 ] )
print ( l[ - 1 ] [ 2 ] [ 0 ] )
作用: 按照索引/顺序存放多个值
hobbies= "read music movie basketball"
print ( hobbies)
hobbies= [ "read" , "music" , "movie" , "basketball" ]
print ( hobbies[ 1 ] )
info = [ "egon" , 18 , "male" , 10 , 18 ]
print ( info[ 1 ] )
(5)字典类型
定义: 在{ }内用逗号分隔开多个key:value,其中value可以是任意类型,而key通常是字符串类型
d= { "k1" : 111 , "k2" : 1.1 , "k3" : [ 222 , 333 ] , "k4" : { "a" : 1 , "b" : 2 } }
print ( d[ "k3" ] [ 0 ] )
print ( d[ "k4" ] [ "a" ] )
作用:按照属性名存放多个值,key:value组合
info = {
"name" : "egon" ,
"age" : 18 ,
"gender" : "male" ,
"annual_salary" : 18 ,
"level" : 10
}
print ( info)
print ( info[ "age" ] )
(6)布尔类型
定义:总共就两个值True、False
x= True
y= False
print ( type ( x) )
print ( 10 > 9 )
print ( 10 == 10 )
x= None
print ( x is None )
print ( bool ( 0 ) )
print ( bool ( "" ) )
print ( bool ( [ ] ) )
print ( bool ( { } ) )
print ( bool ( None ) )
print ( bool ( 1 ) )
作用:通常会作为条件
二、与用户交互
name = input ( "请输入你的用户名:" )
print ( name, type ( name) )
age = input ( "请输入你的年龄:" )
age = int ( age)
print ( age, type ( age) )
print ( age > 16 )
msg= "my name is %s my age is %s" % ( "egon" , 18 )
msg= "my name is %s my age is %s" % ( "egon" , [ 1 , 2 , 3 ] )
msg= "my name is %s my age is %d\n" % ( "egon" , 18 )
print ( msg, end= "" )
print ( msg)
print ( msg)
三、基本运算符
1、算数运算符
print ( 10 + 3 )
print ( 10 - 3 )
print ( 10 * 3 )
print ( 10 ** 3 )
print ( 10 / 3 )
print ( 10 // 3 )
print ( 10 % 3 )
print ( 10 * 3.333 )
print ( [ 1 , 2 , 3 ] + [ 3 , 4 , 5 ] )
print ( [ 1 , 2 , 3 ] * 3 )
2、比较运算符
print ( 10 == 10 )
print ( 10 > 10 )
print ( 10 >= 10 )
print ( "abc" != "ddd" )
print ( 10 > 3.1 )
print ( 10 > "12" )
print ( "a12" == "a12" )
print ( [ 1 , 2 , 3 ] == [ 1 , 2 , 3 ] )
print ( "abc" > "az" )
l2= [ 111 , "aaa" ]
print ( l1 > l2)
3、赋值运算符
age= 10
age += 2
age -= 2
age **= 2
print ( age)
x= y= z= 10
print ( id ( x) )
print ( id ( y) )
print ( id ( z) )
x= 10
y= 20
x, y= y, x
print ( x)
print ( y)
salary = [ 1.1 , 2.2 , 3.3 , 4.4 , 5.5 ]
mon1, mon2, mon3, mon4, mon5= salary
print ( mon1, mon2, mon3, mon4, mon5)
mon1, mon2, mon3, mon4= salary
mon1, mon2, mon3, mon4, mon5, mon6= salary
mon1, mon2, mon3, * _= salary
print ( mon1, mon2, mon3)
print ( _)
dic= { "k1" : 111 , "k2" : 222 , 'K3' : 333 }
x, y, z= dic
print ( x, y, z)
x, * _= dic
print ( x)
print ( not 10 > 3 )
print ( True and 1 > 0 and "aaa" == "aaa" )
print ( True and 1 < 0 and "aaa" == "aaa" )
print ( True or 1 > 0 or "aaa" == "aaa" )
print ( False or 1 < 0 or "aaa" != "aaa" )
res= 3 > 4 and 4 > 3 or not 1 == 3 and 'x' == 'x' or 3 > 3
res= ( 3 > 4 and 4 > 3 ) or ( not 1 == 3 and 'x' == 'x' ) or 3 > 3
print ( res)
print ( 1 and 3 )
print ( 1 and 3 or 4 )
四、流程控制
1、if判断
age= 22
height= 170
weight= 100
gender= "female"
print ( "start..." )
if age > 16 and age < 26 and height > 160 and gender == "female" :
print ( "开始表白。。。" )
print ( 'end...' )
age= 18
height= 170
weight= 100
gender= "female"
print ( "start..." )
if 16 < age < 26 and height > 160 and gender == "female" :
print ( "开始表白。。。" )
else :
print ( "阿姨好。。。" )
print ( 'end...' )
age= 18
height= 170
weight= 100
gender= "female"
is_ok= True
print ( "start..." )
if 16 < age < 26 and height > 160 and gender == "female" :
print ( "开始表白。。。" )
if is_ok:
print ( "在一起。。。" )
else :
print ( "我们不太合适。。。" )
else :
print ( "阿姨好。。。" )
print ( 'end...' )
score = input ( "your score: " )
score = int ( score)
if score >= 90 :
print ( "优秀" )
elif score >= 80 :
print ( "良好" )
elif score >= 70 :
print ( "普通" )
else :
print ( "很差" )
2、while循环(条件循环)
print ( 'start...' )
while True :
inp_name= input ( "username>>>: " )
inp_pwd= input ( "password>>>: " )
if inp_name == "egon" and inp_pwd == "123" :
print ( 'login successful' )
break
else :
print ( 'username or password error' )
print ( "stop..." )
count= 0
while count < 6 :
print ( count)
count+= 1
print ( 'start...' )
tag= True
while tag:
inp_name= input ( "username>>>: " )
inp_pwd= input ( "password>>>: " )
if inp_name == "egon" and inp_pwd == "123" :
print ( 'login successful' )
tag= False
break
else :
print ( 'username or password error' )
print ( '=========================>' )
print ( "stop..." )
count= 0
while count < 6 :
if count == 3 :
count += 1
continue
print ( count)
count+= 1
count= 0
while count < 6 :
if count == 3 :
break
print ( count)
count+= 1
else :
print ( "==========>" )
while True :
x= input ( ">>: " )
2.1、while流程控制循环嵌套
登录账户(限制三次密码错误)
print ( "start..." )
count = 0
tag = True
while tag:
username = input ( "用户名:" )
password = input ( "密码:" )
if username == "wushu" and password == "123" :
print ( "登陆成功" )
while tag:
print ( """
0 退出
1 查询
2 转账
""" )
opt = input ( "输入序号:" )
if opt == '0' :
tag = False
elif opt == '1' :
print ( "查询" )
elif opt == '2' :
print ( "转账" )
else :
print ( "请输入正确的序号" )
else :
print ( "登录失败" )
count += 1
if count == 3 :
print ( "输入密码错误超过最大次数" )
break
print ( "end..." )
3、for循环
3.1 for循环也可以叫迭代循环
3.1.1 遍历值
names = [ 'egon' , 'jason' , 'alex' , 'xu' ]
for name in names:
print ( name)
info = [ [ "name" , "egon" ] , [ "age" , 18 ] , [ "gender" , "male" ] ]
for x, y in info:
print ( x, y)
3.1.2 range()
print ( range ( 5 ) )
print ( range ( 1 , 100 ) )
li = [ ]
for i in range ( 10 , 20 , 2 ) :
li. append( i)
print ( li)
for i in range ( 5 , 0 , - 1 ) :
print ( i)
l = [ 111 , 222 , 333 , 444 , 555 ]
i = len ( l) - 1
while i >= 0 :
print ( l[ i] )
i -= 1
for x in range ( len ( l) - 1 , - 1 , - 1 ) :
print ( l[ x] )
3.1.3 for 和 enumerate()
for x, y in enumerate ( l) :
print ( x, y)
3.1.4 for 和 break
for i in range ( 5 ) :
if i == 3 :
break
print ( i)
3.1.5 for 和 continue
for i in range ( 5 ) :
if i == 3 :
continue
print ( i)
3.1.6 for 和 else
for i in range ( 7 ) :
if i == 3 :
break
print ( i)
else :
print ( "---xxxx" )
3.1.7 嵌套多层for循环
for i in range ( 3 ) :
print ( "----------loop1----------> %s" % i)
for j in range ( 5 ) :
print ( "---loop2----> %s" % j)