一、课程笔记整理(18:00-19:00) 1.输入与输出 #print print('abc') print('a','b','c') print('100+200=',100+200) #out name = input('请输入您的姓名:') print('my name is :',name) 2.数据类型 #整数 3 0 -19 0xfff000(十六进制整数) 0o7345(八进制整数) 0b10001100010(二进制数) #浮点数 3.1415926 ; e2 10的几次方 #字符串 "abc" 'abc' \转义符eg: 'I\'m fine' \n换行 \t制表符 print('I\'m \n \t fine.') #\\ print('\\') #\\\n\\ print(r'\\\n\\') # \n '''''' print('wojiao\nzhsngsan') #布尔值 True False and or not(取反) #空值 None #变量 #在python中数据是有类型的。而变量是没有类型的 name = 'a' print(name) name = 'b' print(name) #变量的命名规则 ''' 变量名必须是大小写的英文,数字或_的组合,并且不能以数字开头 区分大小写 case-sensitive 起名字太长时eg:my_age myAge ''' #常量 全部字母大写时 PI = 3.1415926 3.字符串和编码 #每一个字符都对应有一个整数,我们把该整数称为一个编码code #返回字符对应的编码ord() print(ord('a')) #ASCII(American Stantard Code Information Interchange) ''' 0/1 称为 1bit 位 8bit为一组,称为byte字节 中文编码集:GB-2312 GBK Unicode 编码集 Universal宇宙级 2-4个字节表示一个字符 utf-8 编码集 1-4字节表示一个字符 ''' #返回编码对应的字符char() print(chr(65)) print(chr(32222)) '''进制转换''' #二进制 print(bin(10)) #八进制 print(oct(10)) #十六进制 a b c d e f(10-15) print(hex(10)) #十进制 print(int(0b1010)) ''' 1.编码 把字符串变成对应的编码的过程 utf-8 : 1个汉字用3个字节 gbk : 1个汉字用2个字节 ''' print('abc'.encode('ascii')) print('啊'.encode('utf-8')) ''' 2.编译 把字符串对应的编码变成字符串的过程 ''' print(b'abc'.decode('ascii')) print(b'\xe5\x95\x8a\xe5\x95\x8a'.decode('utf-8')) #练习题 我叫xxx,今年xxx岁 name = input('请输入姓名:') age = input('请输入年龄:') # %s占位符 格式化输出 print('我叫%s,今年%d岁'%(name,int(age))) #format print('我叫{0},今年{1}岁'.format(name,age)) r= 10/3 # %.2f float保留2位数 print('10/3=%.2f'%r) ''' 格式化输出 %s字符串 %d整数 %f浮点型 int()强制转换成整型 ''' 4.列表和元组 '''list的增删改查''' #元素用,隔开 names = ['zmb','zgr','zrl','jxr'] #下标 0 1 2 3 print(names) #列表长度len() 列表下标范围0:len(names)-1 #names[-1]list中的最后的值 print(len(names)) #获取list中某个值 :下标获取对应的元素 print(names[3]) print(names[-2])#倒数第二位 #向list中添加一个数.append(值)(追加);.insert(索引值,值)(指定位置的添加) #list是有序的,可重复的 #删除list元素 .pop()(删除末尾的值);.pop(2)(删除指定的值) n= names.pop() print(names) names.pop(1) print(names) #修改list元素 names[0] = 'dddd' print(names) #二维列表 o = [1,2,[3,4],5,6] print(o[2][1]) #空list p = [] '''元组tuple''' tnames = ('zmb','zgr','zrl','jxr') #tuple是不可变的 ,只可查 [传数据] print(tnames[1]) #空tuple p = () #元组中只有一个元素 t = ("aa",) print(t) 二、完成作业(19:00-20:00) 1. 作业-Zen of Python 在交互模式下输入 import this。查看并翻译结果 2. 输入张三2次的考试成绩,输出成绩提升百分比 zhangsan_score1 = int(input('输入张三第1次的考试成绩:')) zhangsan_score2 = int(input('输入张三第2次的考试成绩:')) upscore = (zhangsan_score2-zhangsan_score1)/zhangsan_score1*100 print('成绩提升了%.2f'%upscore,'%') 3. 对列表scores = [89,88,91,87,93,95]进行增删改查的操作 scores = [89,88,91,87,93,95] #查 print(scores) print(scores[0]) print(scores[-1]) #增 scores.append(77) scores.insert(1,78) #删 scores.pop() scores.pop(4) #改 scores[0] = 99 print(scores) 三、难点、重点练习(20:00-20:50) 1.字符串与编码 # %s占位符 格式化输出 print('我叫%s,今年%d岁'%(name,int(age))) #format print('我叫{0},今年{1}岁'.format(name,age)) r= 10/3 # %.2f float保留2位数 print('10/3=%.2f'%r) int()强制转换成整型 2.列表的增删改查 scores = [89,88,91,87,93,95] #查 print(scores) print(scores[0]) print(scores[-1]) #增 scores.append(77) scores.insert(1,78) #删 scores.pop() scores.pop(4) #改 scores[0] = 99 print(scores) 3.元组的注意点 #元组中只有一个元素 t = ("aa",) print(t)
|