python--运算符,字符编码

简介: python--运算符,字符编码

运算符


a += 20 # a = a+20

逻辑运算:

  • and 并且的意思. 左右两端的值必须都是真. 运算结果才是真
  • or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假
  • not 非的意思. 原来是假. 现在是真. 非真即假, 非假既真
  • 先算括号,然后算not, 然后算and ,最后算or
print(3 > 4 or 4 < 3 and 1 == 1)  # False
print(1 < 2 and 3 < 4 or 1 > 2)  # True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # True
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # False
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # False
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # False

x or y 如果x==0 那么就是y, 否则是x

x and y, x为真,值是y,x为假,值是x

false相当于0

print(1 or 2)  # 1
print(2 or 3)  # 2
print(0 or 3)  # 3
print(0 or 4)  # 4
print(0 or 1 or 3 or 0 or 5)  # 1
print(1 and 2)  # 2
print(2 and 0)  # 0
print(0 and 3)  # 0
print(0 and 4)  # 0
print(0 or 4 and 3 or 7 or 9 and 6)  # 3
print(2 > 3 and 3)  # False false相当于0
print(2 < 1 and 4 > 6 or 3 and 4 > 5 or 6)  # 6


字符编码


a = "你好"
a_new = a.decode("utf-8").encode("gbk")  # 第一个里填原来字符串的编码格式(原来的为utf-8),第二个填要转为的格式

decode 解码 encode 编码

abc = '西安'
print(abc.encode('utf-8'))
print('邹邹'.encode('utf-8'))  # 转换为二进制
print(b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf-8'))  # 转换为字符串

结果:

b'\xe8\xa5\xbf\xe5\xae\x89'
b'\xe9\x82\xb9\xe9\x82\xb9'
你好

如果将字符串赋给了abc,则输出时不用引号,没赋值时需要引号。Utf-8必须有引号。二进制转换为字符串不要引号。


相关文章
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
726 3
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
540 4
|
人工智能 Python
Python 中的 `and`, `or`, `not` 运算符
本文介绍了 Python 中的逻辑运算符 `and`、`or` 和 `not` 的基本用法及其特性。这些运算符主要用于布尔运算,特别是在条件判断和循环中非常有用。文章详细解释了每个运算符的功能,例如 `and` 检查所有表达式是否为真,`or` 检查是否有任意一个表达式为真,`not` 用于取反。此外,还提到这些运算符支持短路特性,并可应用于非布尔值场景。掌握这些运算符有助于编写更高效、简洁的代码。
1098 11
|
Python
【10月更文挑战第7天】「Mac上学Python 14」基础篇8 - 运算符详解
本篇将详细介绍Python中的运算符,包括数学运算、关系运算、逻辑运算、赋值运算和成员运算等内容。同时会说明运算符的优先级和运算顺序,帮助用户理解和掌握Python中的运算符使用规则。
370 3
【10月更文挑战第7天】「Mac上学Python 14」基础篇8 - 运算符详解
|
Python
Python运算符优先级
Python运算符优先级。
395 2
|
知识图谱 Python
Python入门:4.Python中的运算符
Python是一间强大而且便捷的编程语言,支持多种类型的运算符。在Python中,运算符被分为算术运算符、赋值运算符、复合赋值运算符、比较运算符和逻辑运算符等。本文将从基础到进阶进行分析,并通过一个综合案例展示其实际应用。
|
Python
Python成员运算符
Python成员运算符
279 2
|
Python
Python赋值运算符
Python赋值运算符。
261 2
|
Python
Python比较运算符
Python比较运算符。
319 2
|
Python
Python身份运算符
Python身份运算符。
299 1

推荐镜像

更多