Python学习的第二天

Python 标准库中的GUI界面 --》 turtle的简单使用

  • 导入turtle as
# 给turtle 起一个别名t
import turtle as t
# 设置画笔的大小
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)

#画E
t.penup()
t.goto(-130,75)
t.pd()
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(50)
t.lt(180)
t.fd(50)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(50)

# 画U
t.penup()
t.goto(-110,75)
t.pd()
t.rt(90)
t.fd(55)
t.circle(25,180)
t.fd(55)

# 画S
t.penup()
t.goto(5,55)
t.pd()
t.circle(22,270)
# r是正数值时,以左手边为圆心画弧,负数以右手边为圆心画弧
t.circle(-22,270)

# 画O
t.penup()
t.goto(100,35)
t.pd()
t.circle(38)

# 画F
t.penup()
t.goto(160,70)
t.pd()
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(40)
t.lt(180)
t.fd(40)
t.lt(90)
t.fd(40)

# 画T
t.penup()
t.goto(180,67)
t.pd()
t.lt(90)
t.fd(70)
t.lt(180)
t.fd(40)
t.lt(90)
t.fd(80)

# 让gui界面一直显示,所有执行代码要写在此函数之前
t.done()
Python学习的第二天_第1张图片
运行截图

Python常用数据类型

  • 列表:与c语言中的数组很相似,可以存储不同类型的数据
hero_name = ['鲁班七号','韩信','李白','百里守约']
# 输出
print(hero_name)
# 遍历
for hero in hero_name:
     print(hero)
# 添加
hero_name.append('武则天')
# 修改
hero_name[1] = 1000
# 删除
del hero_name[1]
# 练习:循环添加1,2,3,4,......10
n_num = []
for i in range(1,11):
    n_num.append(i)
print(n_num)
  • 字符串
    1.定义形式 ' ' " "
name = 'abcdefg'

2.切片:对序列截取一部分的操作,适用于列表

name = 'abcdefg'
{起始位置:终止位置:步长} 左闭右开
print(name[1:4])
# a c e g
# 全切片时可以省略初始和终止位置
print(name[::2])

3.常用方法
(1) 去两端空格

name = '    abcdefg      '
# 查看序列内元素的个数 len()
print(len(name))
name = name.strip()
print(name)
print("去空格之后:",len(name))

(2)替换

price = '$999'
price = price.replace('$','')
print(price)

(3)列表变成字符串方法 join

li = ['a','b','c','d']
li = '_'.join(li)
print(li)
print(type(li))
  • 元组 tuple 和列表很像,但不能修改
a = ('zhangsan','lisi','wangwu',1000)
print(a)
print(type(a))
print(a[1])
# 元组需要注意的是
b = ('lisi',) #是元组
b1 = ('lisi') #不是元组
c = (1000,) #是元组
c1 = (1000) #不是元组
print(type(b)) print(type(c))
  • 字典 dict java hashmap
    1. key-value数据结构
    2. 定义形式 {}
info = {'name':'李四', 'age':34, 'addr':'重庆市渝北区'}
# 长度
print(len(info))
print(info)

3.方法

# 1.字典的访问
print(info['name'])
# 2.修改
info['addr'] = '北京市朝阳区'
print('修改后字典',info)

# 3.增加
info['sex'] = 'female'
print('增加后字典',info)

# 获取字典中所有的键
print(info.keys())

#  # 获取字典中所有的值
print(info.values())

# 获取字典中所有的key-value
print(info.items())
Python学习的第二天_第2张图片
运行截图
  1. 列表变为字典
d = [('name', '李四'), ('age', 34), ('addr', '北京市朝阳区'), ('sex', 'female')]
# 列表变为字典
d1 = dict(d)
print(d1)
#  遍历字典
for k, v in info.items():
    print(k, v)
Python学习的第二天_第3张图片
运行截图
  • 集合,无序,不重复
set1 = {'zhangsan', 'lisi', 222}
print(type(set1))
# 遍历
for x in set1:
    print(x)
Python学习的第二天_第4张图片
运行截图

掌握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)
Python学习的第二天_第5张图片
运行截图
str_info = [
    {"name":"zhangsan","age":18},
    {"name":"lisi","age":32},
    {"name":"wangwu","age":2},
    {"name":"zhaoliu","age":45},
            ]
print("排序前:",str_info)
# def 函数名():
#     函数体
def sort_by_age(x):
    return x['age']
# key= 函数名 ---  按照什么方式进行排序
# 根据年龄大小进行正序排序
str_info.sort(key=sort_by_age, reverse=True)
print("排序后:",str_info)
# 练习 按照工资大小进行排序
name_info_list = [
    ("张三",5465),
    ("李四",6545),
    ("王五",5674),
    ("赵六",9676),
]
def sort_by_num(x):
    return x[1]
name_info_list.sort(key=sort_by_num)
print(name_info_list)
运行截图
  • 调用.py中的方法
  1. Project1.py
def say_hello(name):
    print('hello,{}'.format(name))
say_hello("重庆师范")

  1. Project2.py
# 导入Project5模块
import Project5
Project5.say_hello("佩奇")
运行截图
  • 本地文件读取——使用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)
  1. with as 上下文管理器
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
    data = f.read()
    print(data)
  1. 写入文本
txt = 'i like Python'
with open('./chongqinshifan.html','w',encoding='utf-8') as f:
    f.write(txt)
  • 中文分词 jieba
# 安装jieba分词库
# 指定国内镜像安装
# 在用户目录下新建pip文件夹
# 新建pip.ini 文件
# 添加
# pip install jieba
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
# pip install jieba
Python学习的第二天_第6张图片
添加jieba
# 导入jieba分词
import jieba
# 三种分词模式
sea = "我来到北京清华大学"
# 精确模式  精确分词
seg_list = jieba.lcut(sea)
print(seg_list)
# 全模式  找出所有可能胡分词结果  亢余性大
seg_list1 = jieba.lcut(sea,cut_all=True)
print(seg_list1)
# 搜索引擎模式
seg_list2 = jieba.lcut_for_search(sea)
print(seg_list2)

text = "小明硕士毕业于中国科学院计算所,后在日本京都大学深造"
seg_list3 = jieba.lcut(text,cut_all=True)
print(seg_list3)
#  搜索引擎模式  先执行精确模式,在对其中的长词进行处理
seg_list4 = jieba.lcut_for_search(text)
print(seg_list4)
  • 三国演义小说分词
import  jieba
# 三国演义小说分词
# 读取三国演义小说
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
    words = f.read()
    print(len(words))  # 字数  55万
    words_list = jieba.lcut(words)
    print(len(words_list))  # 分词后的词语数  35万
    print(words_list)
  • 词云的展示
    1. 安装
# pip install wordcloud
# 本地安装Python库的展示
Python学习的第二天_第7张图片
添加wordcloud
  1. 导入词云文件
import jieba
from wordcloud import WordCloud
import imageio
  1. 绘制老人与海词云
text = 'He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish. In the first forty days a boy had been with him. But after forty days without a fish the boy’s parents had told him that the old man was now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at their orders in another boat which caught three good fish the first week. It made the boy sad to see the old man come in each day with his skiff empty and he always went down to help him carry either the coiled lines or the gaff and harpoon and the sail that was furled around the mast. The sail was patched with flour sacks and, furled, it looked like the flag of permanent defeat.'
wc = WordCloud().generate(text)
wc.to_file('老人与海.png')
Python学习的第二天_第8张图片
老人与海词云
  1. 三国演义小说词云绘制
# 三国演义小说分词
# 读取三国演义小说
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
    words = f.read()
    print(len(words))  # 字数  55万
    words_list = jieba.lcut(words)
    print(len(words_list))  # 分词后的词语数  35万
    print(words_list)
# 将words_list转换成字符串
mask = imageio.imread('./image/china.jpg')
novel_words = " ".join(words_list)
print(novel_words)
# Wordcloud()里面设置参数
wac = WordCloud(
    font_path = 'msyh.ttc',
    background_color = 'white',
    width=800,
    height=600,
    mask=mask
).generate(novel_words)
wac.to_file('三国演义.png')
Python学习的第二天_第9张图片
China
Python学习的第二天_第10张图片
三国演义词云

你可能感兴趣的:(Python学习的第二天)