本节内容:
一、python运算符: 算术、赋值、比较运算符、逻辑运算符。
二、python 字符串操作:
a、索引取值、切片
b、拼接、转义
c、常见操作
d、格式化输出
1a、算术运算 + — * / %
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/22 21:56 a = 15 b = 2 c = 3.1415 # 算术运算——加法 print(a + b) print(a + c) # 算术运算——减法 print(a - b) # 算术运算——乘法 print(a * b) # 算术运算——除法 print(a / b) # 算术运算——取模运算 print(a % b)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py 17 18.1415 13 30 7.5 1 Process finished with exit code 0View Code
# -*- coding:utf-8 -*- # @Author: Sky # @Email: 2780619724@qq.com # @Time: 2021/7/22 21:56 # a = 15 # b = 2 # c = 3.1415 # # # 算术运算——加法 # print(a + b) # print(a + c) # # # 算术运算——减法 # print(a - b) # # # 算术运算——乘法 # print(a * b) # # # 算术运算——除法 # print(a / b) # # # 算术运算——取模运算 # print(a % b) a = 15 b = 2 c = 3.1415 # = 赋值运算 d = 15 % 2 print(d) # += 累加赋值 b += a print(b) # -= 累减赋值 a -= b print(a) # *= 累乘赋值 c *= b print(c) e = 7 f = 2 e %= f print(e)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py 1 17 -2 53.4055 1 Process finished with exit code 0View Code
# 比较运算符 == 、 !=、 > 、 >= 、 < 、 <= a = 3 b = 4 c = 'hello' d = 'world' print(a == b) print(c == d) print(b == c) print(a != b) print(a > b) print(a >= b) print(a < b) print(a <= b)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py False False False True False False True True Process finished with exit code 0View Code
不同数据类型可以 == 比较, > 或 < 比较 报错 。
a = 12 b = 'hello' print(type(a)) print(type(b)) c = (a == b) d = (a > b) print(c)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py <class 'int'> <class 'str'> Traceback (most recent call last): File "D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py", line 89, in <module> d = (a > b) TypeError: '>' not supported between instances of 'int' and 'str' Process finished with exit code 1View Code
# and(与) 、 or(或)、 not(非) a = True b = False print(a and b) print(a or b) print(not a)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py False True False Process finished with exit code 0View Code
字符串切片,[1:3] 包头不包尾 [1:3)
var1 = 'Hello World!' var2 = "Runoob" # 索引值以 0 为开始值,-1 为从末尾的开始位置。 print(var1[:3]) print(var1[0:3]) print(var1[1:]) print(var1[1:20]) print(var1[1::2]) # 截取步长 print(var1[::-1]) # 逆序 print(var1[:]) # 获取所有 print(var1[-1]) # 取最后一个字符View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py Hel Hel ello World! ello World! el ol! !dlroW olleH Hello World! ! Process finished with exit code 0View Code
全部大写: upper()
全部小写: lower()
查找: find(子字符串) 返回匹配到的第一个索引 ,未查到返回 -1
替换: replace(旧,新,[替换的次数])
index(): 未查到报错, 与 find() 不一样
var1 = 'Hello World!' var2 = 'Hello World! Hello World! Hello World!' print(var1.upper()) print(var2.lower()) print(var1.find('w')) print(var1.find('W')) print(var2.replace('World', 'java', 2)) # 替换2个 print(var1.index('W')) # print(var1.index('w')) # 报错 ValueError: substring not foundView Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py HELLO WORLD! hello world! hello world! hello world! -1 6 Hello java! Hello java! Hello World! 6 Process finished with exit code 0View Code
# 数据类型强制转换 price = float(input('请输入价格:')) weight = float(input('请输入重量:')) result = price*weight print(result)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py 请输入价格:12.5 请输入重量:5.7 71.25 Process finished with exit code 0View Code
str1 = 'python cainiao 666' # 找出第5个字符 # 复制一份字符串,保存为 str_two(使用赋值运算符) print(str1[4]) str_two = str1 print(str_two)View Code
执行结果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day06/test_demo/test_06.py o python cainiao 666 Process finished with exit code 0View Code