数据类型

数字类型

作用

身份证/电话号码

整形

定义方法

num = 123
num = int(123)

使用方法

x = 1
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)  # 取余
print(x // y)  # 取整
print(x ** y)  # 幂
import cmath  # 科学计算器

# cmath.log()
# cmath.cos()
print(cmath.pi)
print(cmath.e)  # 自然底数

浮点型

作用

薪资3.1w

定义方法

salary = 3.1
print(salary)
salary1 = float(3)  # 强制类型转换(动态语言),静态语言不能改变
height = int(180.5)
print(salary1)  # 3.0
print(height)

使用方法

# +-*/ % // **

字符串

什么是字符串:字符(空字符,输入的所有内容都叫字符)+串,即把字符串起来

作用

姓名/性别

定义方式

name = 'nick'  # 单引号内把字符串起来
name1 = "nick"


# height = 'nick's height'  # 读取第一个单引号的时候,字符串开始;第二引号结束
height1 = 'nick"s height'  # 读取第一个单引号的时候,字符串开始;第二引号结束
height2 = "nick's height"  # 读取第一个单引号的时候,字符串开始;第二引号结束

使用方法(内置方法)

str = 'nick handsome'
# 1. startswith : 以。。。。开始
print(s.startswith('nick'))  # True --》 正确

# 2. endswith: 以。。。。结束
print(s.endswith('a'))  # False  # 错误

# 索引取值


poem2 = '''孩儿立志出湘关,学不成名誓不还;埋骨何须桑梓地,人生无处不青山。'''

#          0 1 2 3 4 5 6 7 8 9   # 索引--》描述了字符的位置,从0开始
#                                                                   -4-3-2-1
print(poem2[0])  # 孩
print(poem2[-1])  # 孩
# 索引切片
print(poem2[0:7])  # 0-6个,顾头不顾尾  #  孩儿立志出湘关
print(poem2[2:4])  # 0-6个,顾头不顾尾

# 步长

print(poem2[0:7:3])  # 孩志关

列表

什么是列表:一个字符串只能存一个之,用来存多个值的

作用

爱好

定义方式

hobby_list = ['jiao', 'dapao', 'dsb', 'piao', ['666', '233']]

使用方法

# 用[]存储,用逗号隔开多个元素(任意数据类型)
hobby_list = ['jiao', 'dapao', 'dsb', 'piao', ['666', '233']]

# hobby_list2 = ['jiao']


# 存不是目的,取才是目的


# 3. 使用方法

hobby_list1 = ['jiao', 'dapao', 'dsb', 'piao', ['666', '233']]
#               0       1       2      3       4

# 索引取值
print(hobby_list1[4][0])  # ['666', '233'][0]  --> 666


# 切片
print(hobby_list1[0:4])

print(hobby_list1[:4])  # 如果没写,默认为0
print(hobby_list1[0:-1])  # 如果没写,默认为最后一个
print(hobby_list1[:])


# 内置方法
hobby_list2 = ['jiao', 'dapao', 'dsb', 'piao', ['666', '233']]

# append: 加值
hobby_list2.append('read')
print(hobby_list2)


# 索引修改值
hobby_list2[0] = 'singing'
print(hobby_list2)
hobby_list2[:] = 1,1,1,1,1,1,  # 仅作了解
print(hobby_list2)

字典

什么是键值对:key:value

{}内有多个键(一般为字符串,具有描述意义)值(具体的值,值为任意数据类型)对

定义方式

dict = {'name':'nick','height':180,'weight':150,'hobby_list':t['singing','jumping','rap','basketball']}

使用方法

print(yy_info_dict['name'])  # 按key取值
print(yy_info_dict['weight'])  # 按key取值

yy_info_dict['height'] = yy_info_dict['height'] + 1  # 151  # 按key修改值
print(yy_info_dict)

# del删除值
del yy_info_dict['height']
print(yy_info_dict)



s = 'a+b+c+d'
print(s.split('+'))  # 按照*把字符串切开,然后把切开的元素放入列表内

# lis = ['a','b','c','d']
# res = ' '.join(lis)  # 按照字符串把列表里的每一个元素取出来拼接
# print(res)w

词云图

pic

# pip install wordcloud
# pip install matplotlib
# pip install imageio


# ctrl + r 刷新



# s = "Nick 是上海虹桥最帅的男人,没有之一,因为他就是最帅的"
# s_list = jieba.lcut(s)  # 把字符串切割成列表
# s = ' '.join(s_list)  # 把列表拼接成字符串

# w = wordcloud.WordCloud(width=1000,height=1000,font_path=r'C:\Windows\Fonts\simsun.ttc')  # 生成一个词云对象  # 词云外国人写的,默认支持英文,为了实现中文
# w.generate(s)
# w.to_file('nick1.png')


# mask标记: 首先找到一个白色底的五角星的图片
import jieba
import wordcloud
from imageio import imread

mk = imread('shandian.png')  # 把图片读入内存
with open('word.txt','r',encoding = "utf-8")as f:
    for line in f.readlines():  # 依次读取每行

        s = line
#s = "Nick is the most handsome man in Shanghai Hongqiao, no one, because he is the most handsome"
s_list = jieba.lcut(s)  # 把字符串切割成列表

s = ' '.join(s_list)  # 把列表拼接成字符串


w = wordcloud.WordCloud(width=1000,height=1000,font_path='C:\Windows\Fonts\simsun.ttc',mask=mk,background_color='white')  # 生成一个词云对象  # 词云外国人写的,默认支持英文,为了实现中文


w.generate(s)
w.to_file('nick2.jpg')

# python解决问题的语言,c造轮子


#word.txt
'''In many parts of the world, the problem of gun shot has become more serious, especially in America, because of the available source of guns, many people commit crimes with guns. How to reduce the crime has caught the world’s attention and the public calls for taking affective action.In the United States, the gun shot incidents in campus have increased, many people worry about their security and the world is calling for taking action to prevent such incidents. Most people believe that it is necessary to control guns, even to ban selling it. But parts of the Americans believe that the best way to protect themselves is to have more guns, so they can fight against the gangsters.'''

posted on 2019-08-26 16:15  shenblogs  阅读(175)  评论(0编辑  收藏  举报

导航