列表: 与c语言中的数组很相似, 只不过可以存储不同类型的数据
定义方式 []
hero_name = ['鲁班七号', '安琪拉', '李白', '刘备']
输出
print(hero_name)
hero_name = ['鲁班七号', '安琪拉', '李白', '刘备']
print(hero_name)
遍历
for hero in hero_name:
print(hero)
hero_name = ['鲁班七号', '安琪拉', '李白', '刘备']
print(hero_name)
常见操作
1、 列表的访问
列表名[索引]
print(hero_name[2])
2.添加 append
hero_name.append('后羿')
print('添加后的列表', hero_name)
3.修改
hero_name[1] = 1000
print('修改后的列表',hero_name)
4.删除
del hero_name[1]
print('删除后的列
hero_name = ['鲁班七号', '安琪拉', '李白', '刘备']
print(hero_name)
for hero in hero_name:
print(hero)
print(hero_name[2])
hero_name.append('后羿')
print('添加后的列表', hero_name)
hero_name[1] = 1000
print('修改后的列表',hero_name)
del hero_name[1]
print('删除后的列表',hero_name)
-----------------------------------------------------------------
Python标准库中的GUI界面 -- 》 turtle
turtle的简单使用
导入turtle as 是给起一个别名
import turtle as t
设置画笔的大小 10px
t.pensize(10)
t.color('blue')
绘制 NEUSOFT
水平左移 抬笔
t.penup()
t.goto(-260, 0)
t.pd()
绘制 N
t.left(90)
t.forward(80)
t.right(145)
简写
t.fd(100)
t.lt(145)
t.fd(80)
绘制 S
t.penup()
t.goto(120, 50)
t.pd()
t.circle(22,270)
r 是正数值时,以左手边为圆心画弧,负值是以右手边为圆心画弧
angle 是负数时候 代表绘制方向
t.circle(-22,270)
t.circle(30)
让gui界面一直显示, 所有执行的代码要写在此函数之前
t.done()
import turtle as t
t.pensize(10)
t.color('blue')
t.penup()
t.goto(-260, 0)
t.pd()
t.left(90)
t.forward(80)
t.right(145)
t.fd(100)
t.lt(145)
t.fd(80)
t.penup()
t.goto(120, 50)
t.pd()
t.circle(22,270)
t.circle(-22,270)
t.done()

0VBDUCZ$G{[Z5N$@JOI~BNW.png
---------------------------------------------------------------
列表常见操作
1.列表的访问
列表名[索引]
print(hero_name[2])
2.添加 append
hero_name.append('后羿')
print('添加后的列表', hero_name)
3.修改
hero_name[1] = 1000
print('修改后的列表',hero_name)
4.删除
del hero_name[1]
print('删除后的列表',hero_name)
例子:
创建 [1, 2, 3......10] 这样的一个数字列表
1.创建空列表
li = []
2.使用for 循环, 在循环中添加元素值
for i in range(1, 11):
li.append(i)
print(li)
字符串
定义形式 '' ""
切片 对序列截取一部分的操作,适用于列表
name = 'abcdefg'
name[1]
[起始位置:终止位置:步长] 左闭右开
print(name[1:4])
a c e g
print(name[0:7:2])
全切片的时候可以省略初始和终止位置
print(name[::2])
常用方法
去两端空格
name = ' abcdefg '
查看序列内元素的个数 len()
print(len(name))
name = name.strip()
print('去空格之后', len(name))
替换
price = '$999'
price = price.replace('$','')
print(price)
列表变成字符串的方法 join
li = ['a', 'b', 'c', 'd']
a = '_'.join(li)
print(a)
print(type(a))
数字
元组 tuple 元组和列表很像只不过元组不可以修改
定义 ()
a = ('zhangsan', 'lisi', 'wangwu',1000)
print(a)
print(type(a))
访问
print(a[1])
修改
a[3] = 'zhaoliu'
关于元组需要注意的是 只有一个元素的元组
b = ('lisi',) #是不是元组
c = (1000,) #是不是元组
print(type(b))
print(type(c))
字典 dict java hashmap
key-value数据结构
定义形式 {}
info = {'name':'李四', 'age':34, 'addr':'重庆市渝北区'}
print(len(info))
print(info)
1.字典的访问
print(info['name'])
2.修改
info['addr'] = '北京市朝阳区'
print('修改后字典',info)
3.增加
info['sex'] = 'female'
print('增加后字典',info)
获取字典中所有的键
print(info.keys())
获取字典中所有的z值
print(info.values())
获取字典中所有的key-value
print(info.items())
d = [('name', '李四'), ('age', 34), ('addr', '北京市朝阳区'), ('sex', 'female')]
d1 = dict(d)
print(d1)
遍历字典
for k, v in info.items():
print(k, v)
集合
无序,不重复
set1 = {'zhangsan', 'lisi', 222}
print(type(set1))
遍历
for x in set1:
print(x)
-----------------------------------------------------------------
1.掌握python常用数据类型和语法
列表的排序
li = []
for i in range(10):
li.append(i)
print(li)
from random import shuffle
shuffle(li)
print('随机打乱的列表', li)
li.sort(reverse=True)
print('排序后的列表', li)
stu_info = [
{"name":'zhangsan', "age":18},
{"name":'lisi', "age":30},
{"name":'wangwu', "age":99},
{"name":'tiaqi', "age":3},
]
print('排序前', stu_info)
def 函数名(参数):
函数体
def sort_by_age(x):
return x['age']
key= 函数名 --- 按照什么进行排序
根据年龄大小进行正序排序
stu_info.sort(key=sort_by_age, reverse=True)
print('排序后', stu_info)
练习
name_info_list = [
('张三',4500),
('李四',9900),
('王五',2000),
('赵六',5500),
]
def sort_by_grade(i):
return i[1]
根据元组第二个元素进行正序排序
name_info_list.sort(key=sort_by_grade)
print(name_info_list)
2.本地文件读取
python中使用open内置函数进行文件读取
f = open(file='./novel/threekingdom.txt', mode='r', encoding='utf-8')
data = f.read()
f.close()
data = open(file='./novel/threekingdom.txt', mode='r', encoding='utf-8').read()
print(data)
with as 上下文管理器 不用手动关闭流
with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
data = f.read()
print(data)
写入
txt = 'i like python'
with open('python.txt','w', encoding='utf-8') as f:
f.write(txt)
text = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>重庆师范欢迎你</h1>
</body>
</html>"""
print(text)
with open('chongqingshifan.html','w', encoding='utf-8') as f:
f.write(text)