Python学习(第三章)

一、 转义字符

1. \t 使输出垂直方向保持对齐
# \t 在控制台输出一个制表符,是输出垂直方向保持对齐
print("1 2 3")
print("10 11 12")
print("1\t2\t3")
print("10\t11\t12")
---------------------------------------------------

1 2 3
10 11 12
1    2    3   
10   11   12

二、Pycharm小拓展

1. 调试:     F8: step over

                F7: step into

2. 函数要定义的上方应该和其他代码(包括注释)保留两个空行

3. View –> Quick Documentation 查看函数注释  快捷键 CTRL+Q

三、 函数

1. 函数的返回值 return, return后的代码都不会执行

四、 模块

1. 所有以 .py 结尾的文件都是一个模块

   在模块中定义的全局变量函数都是模块可以提供给外界使用的工具

   模块好比是工具包,要使用工具包中的工具要 import 这个模块

2. 模块名也是一个标识符,不能以数字开头,否则无法导入

3. 在目录 \__pycache__  中, Python解释器为了提高程序的执行性能,会把使用import导入的模块先编译成一个二进制 .pyc 文件(字节码),之后执行速度会明显改善

五、高级变量类型

1. 数字型

  • 整型
  • 浮点数
  • 布尔型
  • 复数型--用于科学计算

2. 非数字型

  • 字符串
  • 列表
  • 元组
  • 字典

所有非数字型都支持以下特点:

>1 都是一个序列 sequence, 也可以理解为容器

>2 取值 []

>3 遍历 for in

>4 计算长度、最大/最小值、比较、删除

>5 连接 + 和重复 *

>6 切片

六、 列表

列表用于存储一串信息,用 [ ] 定义,用 , 分隔

列表的常用11个操作:

name_list = ["zhangsan","lisi","wangwu"]

# 1. 取值和取索引
print(name_list[2])
# index 取索引
print(name_list.index("lisi"))

# 2. 修改
name_list[1] = "xiaoer"

# 3. 增加数据
# append 末尾增加
name_list.append("王小二")
print(name_list)
# insert 把数据增加到指定索引位置
name_list.insert(1,"小美眉")
print(name_list)
# extend 把其他列表的内容追加到末尾
temp_list= ["孙悟空","zhangsan","沙师弟"]
name_list.extend(temp_list)
print(name_list)

# 4. 删除数据
# remove 删除某个数据
name_list.remove("wangwu")
print(name_list)
# pop 把列表中最后一个元素删除
name_list.pop()
print(name_list)
# pop 可以指定要删除元素的索引
name_list.pop(3)
print(name_list)
# clear 可以清空列表
name_list.clear()
print(name_list)

# 5. 统计
name_list = ["zhangsan","lisi","wangwu","xiaoer","zhangsan"]
# len 列表数据的个数
list_len = len(name_list)
print("列表中的元素有%d个"%list_len)
# count 数据出现的次数
count = name_list.count("zhangsan")
print("zhangsan有%d个"%count)
# remove 只会删除第一个出现的数据
name_list.remove("zhangsan")
print(name_list)

name_list = ["zhangsan", "lisi", "wangwu", "wangxiaoer"]
num_list = [6, 4, 1, 8, 7, 10]
# sort 升序与降序
name_list.sort()
num_list.sort(reverse = True)
print("升序",name_list)
print("降序",num_list)
# reverse 列表反转 逆序
num_list.reverse()
print("逆序", num_list)
-----------------------------------------------------------------

wangwu
1
['zhangsan', 'xiaoer', 'wangwu', '王小二']
['zhangsan', '小美眉', 'xiaoer', 'wangwu', '王小二']
['zhangsan', '小美眉', 'xiaoer', 'wangwu', '王小二', '孙悟空', 'zhangsan', '沙师弟']
['zhangsan', '小美眉', 'xiaoer', '王小二', '孙悟空', 'zhangsan', '沙师弟']
['zhangsan', '小美眉', 'xiaoer', '王小二', '孙悟空', 'zhangsan']
['zhangsan', '小美眉', 'xiaoer', '孙悟空', 'zhangsan']
[]
列表中的元素有5个
zhangsan有2个
['lisi', 'wangwu', 'xiaoer', 'zhangsan']
升序 ['lisi', 'wangwu', 'wangxiaoer', 'zhangsan']
降序 [10, 8, 7, 6, 4, 1]
逆序 [1, 4, 6, 7, 8, 10]

七、 关键字、函数与方法

  • 关键字 是Python内置的,使用时后面不用加括号。如 while、del、for……
  • 函数 封装了独立的功能,但需要记住函数名,死记硬背
  • 方法 和函数类似,同样封装了独立的功能,但需要通过 对象 来调用,表示针对这个对象可以做哪些操作

八、 元组

()定义,元素不能修改

1. 元素个数为0的元组

single_tuple = (5)
print(type(single_tuple))
single_tuple = (5,)
print(type(single_tuple))
-------------------------------------------------------------------
<class 'int'>
<class 'tuple'>

2. 三个主要应用场景

  • 作函数的参数和返回值 一个函数可以接受任意多个参数一次返回多个数据
  • 格式字符串,格式化字符串后面的()本身就是一个元组
  • 让列表可以不被修改,保护数据安全

九、 列表与元组转换

列表转元组 tuple(列表),元组转列表 list(元组)

字典是无序的 对象的集合,键值对间用 , 分开

十、 字典

应用场景:存储某一物体的所有特征

用 { } 定义,key是索引,value是值,key和value用 : 分开,key具有唯一性

# 无序
xiaoming = {"name":"xiaoming"}

print(xiaoming)
# 1. [] 取值
print(xiaoming["name"])
# 2. 增加/修改,key存在则修改,不存在则增加
xiaoming["age"] = 18
xiaoming["name"] = "xiaoxiaoming"
print(xiaoming)
# 3. pop 删除
xiaoming.pop("name")
print(xiaoming)
------------------------------------------------------------------------------------------------------
{'name': 'xiaoming'}
xiaoming
{'name': 'xiaoxiaoming', 'age': 18}
{'age': 18}
xiaoming_dic = {"name":"xiaming",
                "age":18}
#1. len 统计键值对数量
print(len(xiaoming_dic))

#2. updata 合并字典,有相同的key则覆盖新的value
temp_dic = {"heighe":1.5,"age":1000}
xiaoming_dic.update(temp_dic)
print(xiaoming_dic)

#3,clear 清空字典
xiaoming_dic.clear()
print(xiaoming_dic)
--------------------------------------------------------------------------------------
2
{'name': 'xiaming', 'age': 1000, 'heighe': 1.5}
{}

十一、 字符串

hello_str = "hello hello"

# 字符串长度
print(len(hello_str))

# 统计子字符串出现次数,若不存在则输出0
print(hello_str.count("llo"))
print(hello_str.count("abc"))

# 输出第一个子字符串索引位置,不存在会报错
print(hello_str.index("llo"))
-------------------------------------------------------------------------------------------------
11
2
0
2

判断

# isspace() 注意 \t \r \n
space_str = " "
print(space_str.isspace())  # True
space_str = "     a"
print(space_str.isspace())  # False
space_str = "         \t\r\n"
print(space_str.isspace())  # True
# 判断字符串中是否只包含数字 常用.isdicimal()
# 都不能判断小数 1.1
# isdigit():unicode 字符串(1)
# isnumeric():中文数字“一千零一”
num_str = "1"
print(num_str.isdecimal())  #True
print(num_str.isdigit())  # True
print(num_str.isnumeric())  # True

查找和替换

注意:replace() 不会改变原始字符串内容

hello_str = "hello hello world"
# 判断是否以指定的字符串开始或结束
print(hello_str.startswith("Hello"))  # False 
print(hello_str.endswith("world"))  #Trus
# find() 与 index() 不同,当子字符串不存在时不会报错,而是会输出 -1
print(hello_str.find("hello")) # 0
print(hello_str.find("abc"))  # -1
# replace()注意:不会修改原有字符串的内容
print(hello_str.replace("world","python"))  # hello hello python
print(hello_str) # hello hello world

文本对齐

poem = ["登鹳雀楼",
        "王之涣",
        "白日依山尽",
        "黄河入海流",
        "欲穷千里目",
        "更上一层楼"]
# 居中对齐  因为是英文的空格所以不太整齐
for poem_str in poem:
    print("|%s|"%poem_str.center(10," "))
# 居中对齐
for poem_str in poem:
    print("|%s|"%poem_str.ljust(10," "))
# 居中对齐
for poem_str in poem:
    print("|%s|"%poem_str.rjust(10," "))
------------------------------------------------------------

|   登鹳雀楼   |
|   王之涣    |
|  白日依山尽   |
|  黄河入海流   |
|  欲穷千里目   |
|  更上一层楼   |
|登鹳雀楼      |
|王之涣       |
|白日依山尽     |
|黄河入海流     |
|欲穷千里目     |
|更上一层楼     |
|      登鹳雀楼|
|       王之涣|
|     白日依山尽|
|     黄河入海流|
|     欲穷千里目|
|     更上一层楼|

去除空白字符

poem = ["\t\n登鹳雀楼",
        "王之涣",
        "白日依山尽\t\n",
        "黄河入海流",
        "欲穷千里目",
        "更上一层楼"]

for poem_str in poem:
    # 先用.strip() 去除空白字符
    print("|%s|"%poem_str.strip().rjust(10," "))
--------------------------------------------------------------------------------------------------------------
|      登鹳雀楼|
|       王之涣|
|     白日依山尽|
|     黄河入海流|
|     欲穷千里目|
|     更上一层楼|

猜你喜欢

转载自www.cnblogs.com/btschang/p/9392161.html