ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

004、python 运算符 和 字符串操作

2021-07-23 01:00:33  阅读:201  来源: 互联网

标签:Temp SkyWorkSpace python day06 运算符 test Pytest 004 print


 

本节内容:

一、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 0
View Code

 

1b、赋值运算:   =      +=      -=     *=    %=

# -*- 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 0
View Code

 

 1c、比较运算符     ==   、 !=、  > 、 >=  、 < 、 <=

#  比较运算符     ==   、 !=、  > 、 >=  、 < 、 <=
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 0
View 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 1
View Code

 

 1d、逻辑运算符   and(与) 、 or(或)、  not(非)

#  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 0
View Code

 

 

2a、字符串切片

   字符串切片,[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 0
View Code

 

2b、字符串常见操作

  全部大写:  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 found
View 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 0
View 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 0
View 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 0
View Code

 

标签:Temp,SkyWorkSpace,python,day06,运算符,test,Pytest,004,print
来源: https://www.cnblogs.com/qq-2780619724/p/15046613.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有