week 2(python)
目录
一、算术运算符:+ - * / // % **
二、赋值运算符:= += -= ...
三、关系运算符:== > < >= <= != <>
四、逻辑运算符:and or not
and:从右往左解析,0为False,1为True。
or:从左往右依次解析,找到0了,看下一个数。
五、三目运算符:if else 格式:value1 if 表达式 else value2
输入一个秒数,将它折成小时,分,秒格式化输出
六、切片操作[start:end:step],[start,end]
七、字符串: '' "" ''''''
八、字符串常用API
1、strip() 去掉前后两段的空格 2、lstrip() 去掉前面的空格 3、rstrip() 去掉后面的空格
4、split() 列表
5、join() 拼接字符串
6、replace() 替换子串
一、算术运算符:+ - * / // % **
result = 'zs3' + str(5)
print(result)
result = '3' + '5'
print(result)
result = 3 + 5
print(result)
result = "-" * 5
print(result)
result = 6 / 2
print(result, type(result))
result = 6 // 2
print(result, type(result))
result = 6.8 // 2 # 整除取前面的
print(result, type(result))
zs35
35
8
-----
3.0 <class 'float'>
3 <class 'int'>
3.0 <class 'float'>
# 假设今天是星期五,问100天后是星期几
today = 4
result = (4 + 100) % 7
print("星期", result + 1)
星期 7
二、赋值运算符:= += -= ...
a = 4
a, b = 3, 4
print(a, b)
a, b = b, a # 交换值
a += 2 # a=a+2
print(a, b)
3 4
6 3
三、关系运算符:== > < >= <= != <>
# ==和 is区别:==比较的值,is比较的是id
a = 3
b = 3
result1 = a == b
result2 = a is b
print(result1, result2)
True True
# in: 判断成员是不是属于某个序列,not in
result = 's' in 'jasjnn'
print(result)
result = 'k' in 'jasjnn'
print(result)
result = 'k' not in 'jasjnn'
print(result)
result = '1' in 'jasjnn1'
print(result)
True
False
True
True
四、逻辑运算符:and or not
参考:http://t.csdn.cn/zjJjV
and:从右往左解析,0为False,1为True。
print (0 and 5) #0
print (1 and 5) #5
print (5 and 0) #0
print (10 and 0 and 5 and 0)#0
print (6 and 4 and 3)#3
print (1 and 4 and 2 and 6) #6
or:从左往右依次解析,找到0了,看下一个数。
print (0 or 10) #10
print (1 or 2) #1
print (1 or 0) #1
print (7 or 5 or 5)#7
print (9 or 5 or 6 or 0 or 8)#9
a = 3
b = 4
result = a > 2 and b > 6
print(result)
result = a > 2 or b > 6
print(result)
False
True
result = 3 and 4
print(result)
result = 3 or 4
print(result)
result = not (3 or 4)
print(result)
4
3
False
'''非空的值为真'''
if '':print("y")
else:print("no")if ' ':print("y")
else:print("no")if None:print("y")
else:print("no")
print(type(None))
no
y
no
<class 'NoneType'>
result = 3 < 4 < 6
print(result)
True
五、三目运算符:if else 格式:value1 if 表达式 else value2
# 求两个数的最大值
a = 3
b = 4
max = a if a > b else b
print(max)
4
# 求三个整数的最大值
a = 11
b = 4
c = 7
max = a if a > b else c
max = max if max > b else b
print(max)
11
(三目运算符http://t.csdn.cn/w2l2C)
# if else 嵌套
result = a if b < c else c if b > a else b
print(result)
11
输入一个秒数,将它折成小时,分,秒格式化输出
# 输入一个秒数,将它折成小时,分,秒格式化输出
a = int(input())
b = a // 3600
c = (a - b * 3600) // 60
d = a - b * 3600 - c * 60
print(b, "时", c, "分", d, "秒")
3800
1 时 3 分 20 秒
六、切片操作[start:end:step],[start:end]
mystr = 'baidu.com'
result = mystr[1:4:1]
print(result)
result = mystr[1:4:2]
print(result)
result = mystr[1:4]
print(result)
result = mystr[2:5]
print(result)
aid
ad
aid
idu
result = mystr[5:2:-1]
print(result)result = mystr[-2:-5:-1]
print(result)
.ud
oc.
result = mystr[2:]
print(result)result = mystr[-1::-1]
print(result)result = mystr[::-1]
print(result)result = mystr[:]
print(result)
idu.com
moc.udiab
moc.udiab
baidu.com
七、字符串: '' "" ''''''
# E:\date\nate\photo\pic.jpg
path = 'E:\\date\\nate\\photo\\pic.jpg' # 转义字符
print(path)
path = r'E:\date\nate\photo\pic.jpg' # 加前缀字符r 当作普通字符处理
print(path)test = "I'm a boy!"
print(test)
E:\date\nate\photo\pic.jpg
E:\date\nate\photo\pic.jpg
I'm a boy!
八、字符串常用API
1、strip() 去掉前后两段的空格
2、lstrip() 去掉前面的空格
3、rstrip() 去掉后面的空格
mystr = ' welcome to my web! '# strip() 去掉前后两段的空格
print(mystr.strip())# lstrip() 去掉前面的空格
print(mystr.lstrip())# rstrip() 去掉后面的空格
print(mystr.rstrip())mystr = '##welcome to my web!###'
print(mystr.strip('#'))
welcome to my web!
welcome to my web!
welcome to my web!
welcome to my web!
4、split() 列表
# split() 列表
mystr = 'welcome to my web!'
print(mystr.split(' '))
mydate = '34,56,78,39,48'
print(mydate.split(","))
['welcome', 'to', 'my', 'web!']
['34', '56', '78', '39', '48']
5、join() 拼接字符串
mystr = 'welcome to my web!'
print('~'.join(mystr))mystr = 'welcome to my web!'
print(mystr.split(' '))
print('~'.join(mystr.split(' ')))
w~e~l~c~o~m~e~ ~t~o~ ~m~y~ ~w~e~b~!
['welcome', 'to', 'my', 'web!']
welcome~to~my~web!
6、replace() 替换子串
# replace() 替换子串
mydate = '34,56,78,39,48'
print(mydate.replace(',', ' ')) # 把逗号换成空格
34 56 78 39 48
week 2(python)相关推荐
- Github配置(git+vscode+python+jupyter)
①下载git 打开 git bash 工具的用户名和密码存储 $ git config --global user.name "Your Name" $ git config -- ...
- 【实验楼】python简明教程
①终端输入python进入 欣赏完自己的杰作后,按 Ctrl + D 输入一个 EOF 字符来退出解释器,你也可以键入 exit() 来退出解释器. ②vim键盘快捷功能分布 ③这里需要注意如果程序中 ...
- 【Kaggle Learn】Python 5-8
五. Booleans and Conditionals Using booleans for branching logic x = True print(x) print(type(x))''' ...
- 【Kaggle Learn】Python 1-4
[Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...
- 使用python愉快地做高数线代题目~
今天接触到了python,发现真是极易上手啊!对比c语言是什么鬼东西= = 诶,等下,看完教学文章发现TA在下面写了这句话 如果做了前面的内容你可能已被吸引了,觉得c语言真的是废材! 不...不是的. ...
- python 位运算与等号_Python 运算符
和大多数语言一样,Python也有很多运算符,并且运算符跟其他语言的运算符大同小异接下来一一介绍: 算术运算符: 运算符描述实例 +加 - 两个对象相加a+b的输出结果是30 -减 - 得到复数或者一 ...
- python减小内存占用_如何将Python内存占用缩小20倍?
当程序执行过程中RAM中有大量对象处于活动状态时,可能会出现内存问题,特别是在对可用内存总量有限制的情况下. 下面概述了一些减小对象大小的方法,这些方法可以显著减少纯Python程序所需的RAM数量. ...
- python中排序英文单词怎么写_Python实现对文件进行单词划分并去重排序操作示例...
本文实例讲述了Python实现对文件进行单词划分并去重排序操作.,具体如下: 文件名:test1.txt 文件内容: But soft what light through yonder window ...
- python程序如何执行死刑图片_如何判断对象已死
已死的对象就是不可能被任何途径使用的对象,有以下几种方法判断一个对象是否已经死了: 引用计数 给对象添加一个引用计数器,每当有一个地方引用他,计算器就加 1:当引用失效时,计数器减 1:任何时刻计数器 ...
- Python gRPC 安装
1. 安装依赖库 sudo pip3 install grpcio sudo pip3 install protobuf sudo pip3 install grpcio_tools 2. 生成对应文 ...
最新文章
- java 右键卡死_为什么右键单击不适用于Java应用程序?
- android getprop 分辨率,Android getprop 读取的属性哪里来的?
- [探索 .NET 6]01 揭开 ConfigurationManager 的面纱
- bzoj2286 [Sdoi2011]消耗战 单调栈+lca
- c语言编写的贪吃蛇代码,刚学C语言,想写一个贪吃蛇的代码
- CentOS 7安装ifconfig
- OpenHarmony 2.0和HarmonyOS发布会快评
- CALL TRANSACTION使用及传参数和权限检查(authority-check)
- 1133_SICP开发环境搭建
- 李嘉诚再次助攻华为 用事实打脸“别让李嘉诚跑了”
- 绑定变量窥视 oracle,不均衡分区和绑定变量窥视导致的查询计划错误
- 安装 window10 系统
- 什么软件能测试电脑能不能玩lol,怎么判断自己的电脑能不能玩lol_电脑配置检测的方法 - 驱动管家...
- C# “贝格尔”编排法
- c语言 数组的抽奖小游戏
- jQuery之datetimepicker控件(时间单位精确到分钟)
- docker安装zookeeper3.4
- web类靶机暴力破解
- OSGEarth模型点击事件
- solidwork2019安装后出现无法获得许可证
热门文章
- C++ for循环中有冒号,for(auto c:s)与for(auto c:s)的用法
- ODOO15中如何让销售工作流程全自动完成?
- 租用直播用服务器要注意的几个方面
- 【历史上的今天】2 月 27 日:UML 之父出生;微软宣布全球望远镜计划;苹果停止支持 Newton OS
- 《拖延心理学》读书笔记
- 图像处理和机器学习有什么关系?
- Hexo+github搭建博客的错误:连接超时port 443: Timed out和OpenSSL错误
- lua 文件系统操作
- 自动驾驶_测试场景技术发展与应用_2020
- 标准差 php,PHP基于方差和标准差计算学生成绩的稳定性示例