0
点赞
收藏
分享

微信扫一扫

Python一行流 基础数据结构

q松_松q 2022-03-21 阅读 77
python

1-1 数值数据类型

## Arithmetic Operations
x,y = 3,2
print(x + y) # = 5
print(x - y) # = 1
print(x * y) # = 6
print(x / y) # = 1.5
print(x // y) # = 1
print(x % y) # = 1
print(-x) # = - 3
print(abs(-x)) # = 3
print(int(3.9)) # = 3
print(float(x)) # = 3.0
print(x ** y) # = 9

1-2 布尔值Flase和True

x = 1 > 2
print(x)
#False

y = 2 > 1
print(y)
#True

1-3 关键字and,or和not

x,y = True,False
print((x or y) == True)
#True

print((x and y) == False)
#True
print((not y) == True)
#True

1-4 布尔数据类型

##1、布尔运算
x,y = True,False

print(x and not y)
#True
print(not x and not y or x)
#True

##2.if条件判断得到的是False
if None or 0 or 0.0 or '' or [] or {} or set():
	print("Dead code") #没执行到这

1-5 字符串数据类型

##最重要的字符串方法
y = "This is lazy\t\n "

print(y.strip())
#去掉两头的空白字符:'This is lazy'

print("DrDre".lower())
#转为小写:'drdre'

print("attention".upper())
#转为大写:'ATTENTION'

print("smartphone".startswith("smart"))
#用传入的参数作为前缀去匹配,结果为True

print("smartphone".endswith("phone"))
#用传入的参数作为前缀去匹配,结果为True

print("another".find("other"))
#查找匹配到的索引值为2
print("cheat".replace("ch","m"))
#把所有出现第一个参数的值的地方替换为第二个参数

print(','.join(["F","B","I"]))
#使用指定的分隔符把列表中所有元素拼成一个字符串:F,B,I

print(len("Rumpelstiltskin"))
#字符串长度15

print("ear" in "earth")
#检查结果是否包含,结果为True

1-6 使用关键字None

def f():
	x = 2

#关键字'is'接下来介绍
print(f() is None)
#True

print("" == None)
#False

print(0 == None)
#False
举报

相关推荐

0 条评论