一、与用户交互: 1、什么是与用户交互 程序等待用户输入一些数据,然后程序执行完毕后为用户反馈信息
2、为何程序要与用户交互 为了让计算机能够像人一样与用户的交互
3、如何用 在python3中: input ''' 在python3中,input会将用户输入的任何内容都存成字符串类型 name=input('请输入你的账号名: ') #name='123' print(name,type(name))
在python2中:raw_input会将用户输入的任何内容都存成字符串类型 ''' C:\Users\oldboy>python2 Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> name=raw_input('>>>>>>>>>>>>>>>>>>>>>>>>>>>>: ') >>>>>>>>>>>>>>>>>>>>>>>>>>>>: egon >>> >>> >>> >>> print(name) egon
二、格式化输出:
name=input('please input your username:') age=input('please input your age:') print('my name is',name,'my age is',age) print('my name is %s,my age is %s' %(name,age))
''' my name is 输入的用户名,my age is 输入的年龄 '''
print('my name is %s my age is %s' %(18,'egon'))
print('my name is %s my age is %d' %('egon',18)) print('my name is %s my age is %s' %('egon',18)) print('my name is %s my age is %s' %('egon',[1,2,3])) # %s可以接收任意类型的值 print('my name is %s my age is %d' %('egon','xxx')) # %d只能接收数字类型
三、数据类型之数字类型:
一:数字类型:int与float 1、整型:int 作用:记录年龄,等级,QQ号,各种号码 定义: age=18 #age=int(18) print(age,type(age)) 使用
2、浮点型:float 作用:记录身高、体重、薪资 定义: salary=3.1 #salary=float(3.1) print(salary,type(salary)) 使用:
四、数据类型之字符串类型:
字符类型:str 作用:记录描述性质的数据,比如人的名字、性别、家庭地址、公司简介 定义:在引号内按照从左到右的顺序依次包含一个个字符,引号可以是单引号、双引号、三引号 name1='egon1' print(type(name1))
name2="egon2" print(type(name2))
info=""" compay name:OLDBOY addr:SH """ # 三引可以存多行 print(type(info)) print(info)
注意引号的配对 print("my name is 'egon'") print('my name is "egon"')
使用: x=1 y=2 print(x+y) msg1='hello' msg2='world' print(msg1+msg2)
强调: 1、字符串之间可以相加: '1111'+2222 2、字符串相加是新申请内存空间然后拷贝相加的字符串到新的空间中,效率不高 print('my name is '+'egon'+' my age is '+'18')
3、字符串还可以做乘法运算 print('hello'*10) print('='*100)
五、数据类型之列表类型:
列表类型:list 作用:记录/存多个值,可以方便地取出来指定位置的值,比如人的多个爱好,一堆学生姓名 定义:在[]内用逗号分隔开多个任意类型的值 l=[10,3.1,'egon',['a','b']] # l=list([10,3.1,'egon',['a','b']]) print(l) print(l[0]) print(l[2]) print(l[3]) print(l[3][1])
l1=['a','b',['c',['d',]]] print(l1[2][1][0]) print(type(l))
使用: hobbies="read music sleep eat play" hobbies=["read","music","sleep","eat","play"] print(hobbies[2])
students_info=[ ['egon',18,['play',]], ['alex',18,['play','sleep']] ] print(students_info[1][2][0])
六、数据类型之字典:
字典类型:dict 作用:记录多个key:value值,优势是每一个值value都有其对应关系/映射关系key,而key对value有描述性的功能 定义: 在{}内用逗号分隔开多个key:value元素,其中value可以是任意的数据类型,而key通常应该是字符串类型 info={'name':'egon','sex':'male','age':18} #info=dict({'name':'egon','sex':'male','age':18}) print(type(info))
print(info['name'])
# name age sex comapy_info emp_info=['egon',18,'male',['oldboy',200,'SH']]
print(emp_info[2]) print(emp_info[3][0])
emp_info={ 'name':'egon', 'age':18, 'sex':'male', 'comapy_info':{ 'cname':'oldboy', 'emp_count':200, 'addr':'SH' } }
print(emp_info['comapy_info']['cname'])
names={ 'name1':'egon', 'name2':'alex' }
七、运算符:
算数运算 res=1 + 3 print(res) print(1 + 3) print(10 / 3) #结果保留小数部分 print(10 // 3) # 只保留整数部分 print(10 % 3) # 取余数 print(2**3)
比较运算:== != > < >= <= 了解知识: 数字之间可以互相比较大小 print(10 > 3) print(10 > 3.1)
而字符串只能与字符串比较大小(按照对应位置的字符参考ASCII表去比较的) msg1='hello' msg2='z' print(msg1 > msg2)
A-Za-z print('a' > 'Z') print('Z' > 'Y')= print(len('hello') > 3) print('a' > 3)
列表只能与列表比较大小(按照对应位置的值依次比较,对应位置的值必须是相同的类型) l1=[1,2,3] l2=[10,] print(l2 > l1) l3=[10,2,'b',3] l4=[10,2,'b','c'] print(l3 > l4)
赋值运算 age=18 增量赋值 age+=1 #age=age+1 print(age)
链式赋值 x=100 y=x z=x x=z=y=100 print(id(x),id(y),id(z))
交叉赋值 m=1000 n=2000
# temp=m # m=n # n=temp n,m=m,n print(m,n)
解压赋值 salaries=[11,22,33,44,55,] mon1=salaries[0] mon2=salaries[1] mon3=salaries[2] mon4=salaries[3] mon5=salaries[4] mon1,mon2,mon3,mon4,mon5=salaries print(mon1,mon2,mon3,mon4,mon5)
等号右面包含的值的个数必须与等号左边变量名的个数一致 mon1,mon2,mon3,mon4,mon5,mon6=salaries mon1,mon2,mon3,mon4,=salaries
_=3333 print(_) mon1,mon2,_,_,_=salaries mon1,mon2,*_=salaries print(mon1) print(mon2)
salaries=[11,22,33,44,55,] first=salaries[0] last=salaries[4]
first,_,_,_,last=salaries first,*_,last=salaries print(first) print(last)
age=20 sex='female'
逻辑运算 and:连接左右两个条件,只有在两个条件同时成立的情况下最终结果才为True print(age > 18 and age < 26 and sex == 'female' and 1 > 3)
or:连接左右两个条件,但凡有一个条件成立最终结果就为True print(1 > 3 or 2 > 4 or 'x' == 'y' or 1==1)
not print(not 1 > 3) print(not (1 > 3 or 2 > 4 or 'x' == 'y' or 1==1))
res=(3>4 and 4>3) or (1==3 and ('x' == 'x' or 3 >3)) print(res)
八、数据类型之布尔型:
布尔类型:bool 作用:用来作为判断的条件去用 定义: tag=True # tag=bool(True) tag=False print(type(tag))
通过判断去得到布尔值
print(age > 20) print(age >= 20)
age=18 ==比较的是值 print(age == 18) is:比较的是id是否相等 强调:id相等值一定相等,id不等但是值仍然可以相等
x=1 y=x print(x is y)
tag=True print(id(tag))
res=3 > 1 print(id(res))
res2=1 < 10 print(id(res))
>>> print(name,type(name)) ('egon', <type 'str'>) >>> >>> >>> name=raw_input('>>>>>>>>>>>>>>>>>>>>>>>>>>>>: ') >>>>>>>>>>>>>>>>>>>>>>>>>>>>: 1231231231232 >>> >>> >>> print(name,type(name)) ('1231231231232', <type 'str'>) >>> >>> >>> name '1231231231232' >>> name,type(name) ('1231231231232', <type 'str'>) '''
#在python2中:input要求用户必须输入一个明确的数据类型,输入什么类型就存成什么类型 ''' >>> x=input("输入: ") 输入: egon Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'egon' is not defined >>> x=input("输入: ") 输入: 'egon' >>> >>> x,type(x) ('egon', <type 'str'>) >>> >>> >>> >>> x=input("输入: ") 输入: [1,2,3] >>> >>> x,type(x) ([1, 2, 3], <type 'list'>) >>> >>> >>> y=raw_input('####: ') ####: [1,2,2,3] >>> >>> y,type(y) ('[1,2,2,3]', <type 'str'>)
'''