python基础学习02

一、模块

import sys

# 取环境变量
print(sys.path)
# 取运行程序时的参数,需要在终端或命令行中执行,比如 python sys_mod.py 1 2 3
print(sys.argv)
# 取运行程序时的参数,需要在终端或命令行中执行,比如 python sys_mod.py 1 2 3,并将下标为2的参数取出
print(sys.argv[2])

import os

# 执行命令,不保存结果
r = os.system('df -h')
# 返回0表示命令执行成功
print('--->', r)
# 返回命令执行结果的内存地址,read()后,将内容读出来
r = os.popen('df -h').read()
print(r)
os.mkdir('new_dir')

二、列表
n_list = ['123', '234', '345', '456', [1, 2, 3]]
# 循环
for n in n_list:
print(n)


# 浅拷贝
n_list1 = n_list.copy()
n_list[1] = '223'
n_list[4][0] = 11

print(n_list)
print(n_list1)

# 深拷贝
import copy
n_list1 = copy.deepcopy(n_list)
print(n_list)
print(n_list1)


n_list = ['123', '234', '345', '456']
print(n_list[:2])
print(n_list[-3:])
print(n_list[1:3])
print(n_list[::2])

# 新增
n_list.append('567')
print(n_list)
n_list.insert(1, '678')
print(n_list)
# 修改
n_list[2] = '789'
print(n_list)
# 删除
# n_list.remove('234')
# n_list.pop(0)
del n_list[1]
print(n_list)
# 查询
print(n_list.index('345'))
# 统计
print(n_list.count('567'))
# 排序 按ascii码排序
n_list.sort()
# 合并
n_list2 = [1, 3, 4, 5]
n_list.extend(n_list2)
print(n_list)

三、编码
from builtins import print

msg = '我爱北京天安门'
# 编码
print(msg.encode())
# 解码
print(msg.encode().decode())


四、字符串操作
name = 'my \tname is {name}, i am {year} old'
# 字符串首字母大写
print(name.capitalize())
# 统计a的个数
print(name.count('a'))
# 将字符串放在中间,并字符串长度为50,不足用'-'补足
print(name.center(50, '-'))
# 判断字符串是否以ex结尾
print(name.endswith('ex'))
# tab键转成30个空格
print(name.expandtabs(tabsize=30))
# 字符串切片
print(name[name.find('name'):])
# 字符串格式化,推荐使用
print(name.format(name='alex', year=23))
# 字符串格式化,不推荐使用
print(name.format_map({'name': 'alex', 'year': 23}))
# 判断字符串只包含字母和数字
print('ab23'.isalnum())
# 判断字符串只包含字母
print('ab'.isalpha())
# 判断是否为十进制
print('1.23'.isdecimal())
# 判断是否为整数
print('1'.isdigit())
# 判断是不是一个合法的标识符
print('1a1a'.isidentifier())
# 判断字符串是否小写
print('abC'.islower())
# 判断字符串是否为数字
print('22'.isnumeric())
# 判断是否为空格
print(' '.isspace())
# 判断是否为每个单词首字母大写
print('My Name Is'.istitle())
# 判断是否能打印,字符串不需要判断,当tty文件或drive文件时,返回False
print('My'.isprintable())
# 判断字符串是否大写
print('My'.isupper())
# 将列表加入到字符串中,并以当前字符串为间隔)
print('++'.join(['1', '2', '3']))
# 字符串长度为50,不足时,在右侧以‘*’补足
print(name.ljust(50, '*'))
# 字符串长度为50,不足时,在左侧以‘*’补足
print(name.rjust(50, '*'))
# 将字符串中大写变小写
print('Alex'.lower())
# 将字符串中小写变大写
print('Alex'.upper())
# 将字符串左侧的空格和回车去掉
print(' Alex1\n'.lstrip())
# 将字符串右侧的空格和回车去掉
print(' Alex1\n'.rstrip())
# 将字符串两侧的空格和回车去掉
print(' Alex1\n'.strip())
# “abcdef”123456相对应,并将字符串用该对应关系进行替换
p = str.maketrans("abcdef", '123456')
print('alex li'.translate(p))
# 字符串替换
print('alex li'.replace('l', 'L', 1))
# 查找字符串中最右侧的
print('alex li'.rfind('l'))
# 字符串分割,没有参数默认用空格
print('1+2+3+4'.split('+'))
# 按换行符分割
print('1+2+\n3+4'.splitlines())# 将字符串中的大写转小写,小写转大写
print('Aelx Li'.swapcase())
# 将每个单词首字母大写
print('alex li'.title())
# 左侧以0补足位数
print('alex li'.zfill(50))
五、字典
info = {
"stu01": "a",
"stu02": "b",
"stu03": "c"
}
print(info)

# 两个字典合并,如果KEY重复则更新,不重复的插入
b = {
"stu01": "abc",
1: 2,
2: 3
}
info.update(b)
print(info)

# 字典转列表
print(info.items())

# 初始化字典
dic = dict.fromkeys([6, 7, 8], [1, {"name": "abc
print(dic)
dic[7][1]["name"] = "bcd"
print(dic)

# 循环 这种效率很高
for i in info:
print(i, info[i])
# 这种需要把字典转列表的过程,数据量大的时候效率很低
for k, v in info.items():
print(k, v)

# 新增
info["stu04"] = "d"
print(info)

# 修改
info["stu01"] = "aa"
# 删除
info.pop("stu01")
# del info["stu01"]
# 随机删除
info.popitem()
print(info)

# 查找 找不到KEY时会报错
# print(info["stu02"])
# 查找不到返回None
print(info.get("stu05"))
# 判断KEY是否存在字典中
print('stu03' in info)

# 字典嵌套 KEY尽量不要使用中文,因为编码不同时,会取不出VALUE
catalog = {
"123": {
"1234": [1, 2, 3],
"12345": [1, 2, 3]
},
"456": {
"12346": [1, 2, 3],
"123457": [1, 2, 3]
}
}

catalog["456"]["123457"][2] = 4

print(catalog)

# 取字典中所有值
print(info.values())
# 取字典中所有KEY
print(info.keys())
# 如果字典中KEY存在将值返回,不存在则创建
catalog.setdefault("678", {"123456789": [1, 2]})
print(catalog)
 
 

猜你喜欢

转载自www.cnblogs.com/WhiteBai2018/p/9276561.html
今日推荐