01-review
"""
容器;(数据1, 数据2, 数据3,...)
不可变的;有序的
元素:没有要求
"""
t1 = (10,)
t2 = 10, 23, 45, 89, 3
print(t2[3])
print(t2[1:-1])
t3 = 10, 20, 30
x, y, z = t3
t4 = (10, 20, 30, 40, 50, 60)
x, *y, z = t4
"""
容器;{10, 23, 90}
可变的;无序的;
元素:不可变的数据;唯一的
"""
s1 = set()
02-homework
python = {'stu1', 'stu3', 'stu4', 'stu5', 'stu9', 'stu10'}
java = {'stu2', 'stu4', 'stu5', 'stu6', 'stu9', 'stu10'}
h5 = {'stu1', 'stu5', 'stu6', 'stu7', 'stu8', 'stu10'}
s1 = python | java | h5
print('选课学生总人数:', len(s1))
s2 = python - java - h5
print('只选了第一个学科的人的数量和对应的名字:', len(s2), s2)
s3 = python & java & h5
print('选了三门学生的学生的数量和对应的名字:', len(s3), s3)
s4 = (python ^ java ^ h5) - (python & java & h5)
print('只选了一门学科的学生的数量和对应的名字:', len(s4), s4)
s5 = s1 - s3 - s4
print('只选了两门学科的学生的数量和对应的名字:', len(s5), s5)
print('------------------------------------------华丽的分割线------------------------------------------------')
nums = [1, 2, 3, 1, 4, 2, 1, 3, 7, 3, 3, 1]
new_nums = set(nums)
max_times = 0
for x in new_nums:
count = nums.count(x)
if count > max_times:
max_times = count
result = []
for x in new_nums:
if nums.count(x) == max_times:
result.append(x)
print(result)
max_times = 0
result = []
for x in new_nums:
count = nums.count(x)
if count > max_times:
max_times = count
result.clear()
result.append(x)
elif count == max_times:
result.append(x)
print(max_times, result)
year = int(input("年份:"))
month = int(input("月份:"))
day = int(input("日期:"))
month_day = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
month_day[2] = 29
total_day = day
for m in range(1, month):
total_day += month_day[m]
print(total_day)
month_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
month_day[1] = 29
total_day = day + sum(month_day[:month-1])
print(total_day)
03-字符
"""
容器型数据类型; 将''、""、''''''、""""""作为容器的标志里面的每个符号就是字符串中元素。
不可变的(不支持增删改);有序的(支持下标操作)
元素(字符):可以是任何文字符号, 包括:数字、字母、汉字、日文、韩语、表情符号、标点等
"""
str1 = '10,20,30'
str2 = '12,=)*mksJkH胡上课❀'
str3 = "abc\n123"
print(str3)
str4 = '''abc
123'''
print(str4, type(str4))
"""
字符 - 字符串中的元素(字符串每一个独立的符号)
字符串中的字符可以分为两种:普通字符、转义字符
1)普通字符: 在字符串中表示符号本身的字符
2)转义字符: \和指定的一些符号组合在一起表示特殊功能或者特殊意义的字符。
转义字符是C语言创建的,但是很多都已经没有存在的价值的,目前常用的转义字符:
\n - 换行
\t - 水平制表符(相当于按一次tab键)
\' - 表示一个普通的单引号
\" - 表示一个普通的双引号
\\ - 表示一个普通的反斜杠
"""
str5 = 'abcKH1234,'
print(str5)
str6 = '\m\k\l'
print(str6)
str7 = '=\a=\f='
print(str7)
print('------------------------------------------华丽的分割线------------------------------------------------')
str8 = '\tabc\n123'
print(str8)
str9 = 'it\'s me'
str10 = "it's me"
str11 = "i say: \"you see see, one day day!\""
print(str11)
str12 = 'i say: \"you see see, one day day!\"'
str13 = '\\name\yuting\论文.txt'
print(str13)
str14 = '\u4e01abc'
print(str14)
print('------------------------------------------华丽的分割线------------------------------------------------')
"""
在字符串的最前面加r或者R,可以让字符串中所有的转义字符功能消失(让字符串中的所有字符自动变成普通字符)
"""
str1 = r'\tabc\n123\''
print(str1)
print('------------------------------------------华丽的分割线------------------------------------------------')
path = R'C:\ser\test\name\yuting\demo\t.txt'
print(path)
04-字符编码
"""
计算机存储数据的时候只能存数字(存的是数字对应的二进制的补码);
为了能够让计算机存储字符,给每个字符对应一个固定的数字,每次需要存储这个字符的时候就去存这个数字;
每个字符对应的那个数字就是这个字符的编码值。
"""
"""
1)ASCII码表 - 有128个字符对应的编码值(只要包含英文符号和其他西欧语言对应的符号)
数字字符0~9从48开始连续递增
大写字母A~Z从65开始连续递增
小写字母a~z从97开始连续递增
2)Unicode编码表(Python) - 是ASCII码表的扩展(包含了ASCII码表),包含了世界上所有国家所有民族所有语言的对应的符号(统一码、万国码)
中文范围:4e00 ~ 9fa5
"""
print(chr(97), chr(65))
for x in range(65, 65+26):
print(chr(x))
print('------------------------------------------华丽的分割线------------------------------------------------')
print(chr(0x4e00), chr(0x9fa5))
for x in range(0x4DC0, 0x4DFF+1):
print(chr(x), end=' ')
print()
print('------------------------------------------华丽的分割线------------------------------------------------')
print(ord('余'), ord('婷'))
print(hex(20313), hex(23159))
print(ord('a'))
str1 = 'a余'
str2 = '\u0061\u4f59'
print(str1, str2)
print('在Unicode编码表中最后一个中文是:', '\u9fa5')
05-字符串基本操作
str1 = 'good good study! day day up!'
print(str1[3], str1[-2])
str2 = '\tabc\n123\u4e00m'
print(str2[1], str2[-3])
message = '小明的电话号码是:13678192303'
print(message[-11:])
print(message[3:-2:-2])
print(message[1:-1:2])
str3 = 'hello python!'
for x in str3:
print(x)
print('------------------------------------------华丽的分割线------------------------------------------------')
for x in range(len(str3)):
print(x, str3[x])
print('------------------------------------------华丽的分割线------------------------------------------------')
for index, item in enumerate(str3):
print(index, item)
str1 = 'hello'
str2 = 'world!'
str3 = str1 + ' ' + str2
print(str3)
str4 = str1 * 3
print(str4)
str1 = 'abc12MK89你好9'
new_str = ''
for x in str1:
if '0' <= x <= '9':
new_str += x
print(new_str)
str1 = 'abc12'
new_str = ''
for x in str1:
new_str += x + '*'
print(new_str[0:-1])
str1 = 'ab2Mk89你好8'
new_str = ''
for x in str1:
if '0' <= x <= '9':
new_str += '*'
else:
new_str += x
print(new_str)
print('abc' == 'acb')
"""
已知字符x:
判断x是否是数字字符: '0' <= x <= '9'
判断x是否是小写字母: 'a' <= x <= 'z'
判断x是否是大写字母: 'A' <= x <= 'Z'
判断x是否是字母: 'a' <= x <= 'z' or 'A' <= x <= 'Z'
判断x是否是中文: '\u4e00' <= x <= '\u9fa5'
"""
print('MKAHDJSS' > 'abc')
str1 = 'shfa换手机223--=2MjshUj行HSJSA'
count1 = count2 = 0
for x in str1:
if '\u4e00' <= x <= '\u9fa5':
count1 += 1
elif 'A' <= x <= 'Z':
count2 += 1
print(count1, count2)
print(10 in [10, 20, 30])
print([10, 20] in [10, 20, 30])
print('a' in 'abc123')
print('abc' in 'abc123')
print('ab1' in 'abc123')
print(100)
str(100)
list1 = [10,20,30]
print(list1)
str(list1)
list2 = ["abc", 100]
print(list2)
str(list2)
dict1 = {"a":10,'b':20, 'c':"abc"}
print(dict1)
str(dict1)
06-字符串相关方法
result = '+'.join(['name', 'age', 'gender'])
print(result)
result = '=='.join('abc')
print(result)
nums = [10, 20, 30, 40]
result = '+'.join([str(x) for x in nums])
print(result)
list1 = [10, 'abc', '你好', 1.23, True, '手机壳']
result = ''.join([x for x in list1 if type(x) == str])
print(result)
list1 = ['name', 'age', 'gender']
result = ''.join([x[0] for x in list1])
print(result)
str1 = '123abc你好abc===abc+++'
result = str1.split('abc')
print(result)
result = str1.split('a')
print(result)
result = str1.split('abc', 2)
print(result)
str1 = 'abc123abcabc你好abc===abc+++abc'
result = str1.split('abc')
print(result)
message = 'how are you? i am fine! thank you! and you?'
result = message.replace('you', 'me')
print(result)
result = message.replace('you', 'me', 1)
print(result)
result = message.replace(' ', '')
print(result)
str1 = '''
你 好,世 界
'''
result = str1.strip()
print(result)
print('====')
str2 = '/+//u/重/庆a++///++//'
result = str2.strip('/+')
print(result)
message = 'how are you? i am fine! thank you! and you?'
print(message.find('you'), message.index('you'))
print(message.find('abc'))
message = 'how are you? i am fine! thank you! and you?'
print(message.find('you', 0, 8))
print(message.find('you', 11))
message = 'how are you? i am fine! thank you! and you?'
print(message.rfind('you'))