day04作业

一、 数字类型

整形(int)

作用

描述人的年龄/身份证号

定义方式

age = 19
print(age, type(age))

# 打印结果:
19 <class 'int'>

使用方法

print(1+2)  #  + 加
print(2-1)  #  - 减
print(2*3)  #  * 乘
print(4/2)  #  / 除
print(16%3) #  % 取余
print(16//3) # // 取整
print(2**4)  # **幂运算

浮点型(float)

作用

描述薪资/身高

定义方式

salary = 1.3
print(salary)  # 1.3

使用方法

与整型类似 + - * / % // **

二、字符串类型

作用

描述姓名、性别等

定义方式

name = 'reese'
gender = 'male'

使用方法

  • 索引取值
s = 'helloworld'
print(s[0])  # 取到了h
  • 索引切片
s = 'helloworld'
print(s[0:3])  # 取到了hel
print(s[:])  # 什么都不写默认全取  helloworld
  • 索引步长
s = 'helloworld'
print(s[0:6:2]) # 取到hlo
  • 内置方法 startswith endswith
s = 'helloworld'
print(s.startswith('h'))  # 打印True  
print(s.endswith('d'))   # 打印True

三、列表

作用

可以存储多个值,如 存储爱好

定义方式

列表里的元素可以是任意数据类型

li = [11,2,3,'name','color',3.14,['qq',666]]

使用方法

  • 索引取值
li = [11,2,3,'name','color',3.14,['qq',666]]
print(li[6])

# 打印结果:
['qq', 666]
  • 索引切片
li = [11,2,3,'name','color',3.14,['qq',666]]
print(li[6][1])  # 打印666

print(li[0:3])  # 打印[11, 2, 3]
  • 内置方法

    append方法: 追加

li = [11,2,3,'name','color',3.14,['qq',666]]
li.append('cwz')
print(li)
#打印结果:
[11, 2, 3, 'name', 'color', 3.14, ['qq', 666], 'cwz']

四、字典

作用

使用key: value键值对存数据

定义方式

my_info_dict = {'name': 'setcreed','age': 20, 'height': 180, 'weight': 150}

使用方法

  • 取值
my_info_dict = {'name': 'setcreed','age': 20, 'height': 180, 'weight': 150}
print(my_info_dict['name'])

# 打印结果:
setcreed
  • del删除值
my_info_dict = {'name': 'setcreed','age': 20, 'height': 180, 'weight': 150}
del my_info_dict['height']
print(my_info_dict)

# 打印结果:
{'name': 'setcreed', 'age': 20, 'weight': 150}

五、构建一份词云

import wordcloud
import jieba
from imageio import imread

s = '''
邪恶就是邪恶,没有大小中之分。罪恶的界限因人而异,变幻莫测。
如果要我从两种罪恶中选其一,我宁可不做选择。
仇恨与偏见永远无法连根拔除。
这个世界需要的不是英雄,而是专家。
如果要我两害相权取其轻,那我宁愿什么都不选,通常风险太高了。
有时做出了选择,还是会有些好的后果,虽然影响有限,我选择拯救沼泽的孤儿时,并不知道安娜会死。
而且我从没想过男爵会离开他妻子安息的地方,找条绳索上吊自杀。大多时候做了决定就别再回头了。
'''
mk = imread('test2.png')  # 把图片读取到内存

s_list = jieba.lcut(s)   # 把字符串切成列表
s = ' '.join(s_list)     # 把列表拼接成字符串
w = wordcloud.WordCloud(font_path='C:WindowsFontsSTXINWEI.TTF',mask=mk,background_color='white')
w.generate(s)
w.to_file('cwz.png')

词云

原文地址:https://www.cnblogs.com/setcreed/p/11414432.html