代码改变世界

小白成长之路:初识python(二)

2017-10-23 21:14  张小贤TT  阅读(171)  评论(0编辑  收藏  举报

  迷迷糊糊的又是一天过去了,不过很好的一件事情就是终于找到房子安顿好了,只能说这个研究生读的我真是一万个mmp,有寝室回不去。

今天看的还是关于python的一些基本的数据类型的一些补充好了废话不多说 上代码:

  dict 字典 {key:value}结构,可以进行索引操作,可以循环(循环其key、value或者键值对),不能进行切片操作

dictionary = {'name':'alex','age':26}

# 索引

# 字典的索引为key

# print(dictionary['name'])
# alex

# 可以循环
# for i in dictionary:
# print(i)
# name
# age

# dictionary.keys() 获取字典的所有的key
# dictionary.values() 获取所有的值
# dictionary.items() 获取所有的键值对

# for key,val in dictionary.items():
# print(key,val)
# age
# 26
# name
# alex

# clear 清空字典的内容

# dictionary.get() 根据key获取值,如果key不存在,可以指定一个默认值
# ret = dictionary.get('name')
# print(ret)
# alex
# ret = dictionary.get('asa')
# print(ret)
# None

# update 批量更新
# tmp = {'qqq':'www',111:'ggg'}
# dictionary.update(tmp)
# print(dictionary)
# {'qqq': 'www', 'name': 'alex', 'age': 26, 111: 'ggg'}

# pop
# ret = dictionary.pop('name')
# print(ret)
# alex

# popitem
# ret = dictionary.popitem()
# print(dictionary)
# print(ret)
# {'name': 'alex'}
# ('age', 26)


# del dictionary[key]

============================================================

# s1 = 'alex'
# s2 = str('alex')
# str(val='',encoding = None)
#
# 无参数创建空字符串 一个参数创建普字符串
# 两个参数(字节 编码)

# str类内置方法(常用)
# s.strip()
# s.startswith('xx')
# s.find('') 找子序列的位置
# s.replace('','') 将字符串中的某一子序列替换成另外一子序列
# s.upper()
#

# =====================================================================

# bytes 类型
# 把字符转换成字节
# name = "李璐"
# print(bytes(name,encoding='utf-8'))
# b'\xe6\x9d\x8e\xe7\x92\x90' 用16进制表示的二进制

# for i in name:
# print(i)
# bytes_list = bytes(i,encoding='utf-8')
# print(bytes_list)
# for b in bytes_list:
# print(b,bin(b))
# 李
# b'\xe6\x9d\x8e'
# 230 0b11100110
# 157 0b10011101
# 142 0b10001110
# 璐
# b'\xe7\x92\x90'
# 231 0b11100111
# 146 0b10010010
# 144 0b10010000

# bytes() 把一个字符串转换成其每个字符对应的字节(16进制),在使用的时候要
# 说明字符串的编码形式(因为不同形式的编码占用的字节不同,例如汉字在UTF-8编码的时候
# 占用三个字节),返回一个列表
# 在对其列表进行遍历的时候,输出是10进制表示的
# bin(number=) 把一个数字转换成2进制形式

# python3.5 在for循环的时候循环的元素是每一个“字符”
# 字符 -> 字节
# 汉字在 使用 utf-8 编码的时候占用三个字节
# 使用 gbk 编码的时候占用两个字节

# ==========================================
# 把字节转换成字符串
# b1 = b'\xe6\x9d\x8e'
# s = str(b1,encoding='utf-8')
# print(s)
# 李
# =============================================

# list 可变的元素的集合
# list() # 创建一个空列表 或者将其他元素转换成列表
# # __init__
# s1 = '李璐'
# l1 = list(s1)
# print(l1)

#================================================
#字符串,元组,字典 -> 列表
#
# d1 = {11:'aa',22:'bb',33:'cc'}
# l1 = list(d1)
# print(l1)
# # [33, 11, 22] 把字典的key转换成列表
# l2 = list(d1.values())
# print(l2)
# ['cc', 'aa', 'bb']
#
# l3 = list(d1.items())
# print(l3)
# [(33, 'cc'), (11, 'aa'), (22, 'bb')]

# ======================================================
# list 常用的功能
# l.append() 尾部追加
# l.clear()
# l.extend() 批量扩展(用另外一个可以迭代的对象扩充自己)
# 可以迭代的对象 : list str tuple dict
# l.reverse() 自身反转

# ========================================================
# l.insert() 向指定的位置插入元素
# l = [11,11,22,3]
# l.insert(0,123)
# print(l)
# [123, 11, 11, 22, 3]