300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python 入门-Task1 变量 运算符 数据类型及位运算

Python 入门-Task1 变量 运算符 数据类型及位运算

时间:2020-01-04 12:09:26

相关推荐

Python 入门-Task1  变量 运算符 数据类型及位运算

# *****变量、运算符与数据类型*******

# 1、注释

# 1.1 单行注释, # 标识注释,作用于整行

# This is a Comment

print("Hello world!")

# 1.2 多行注释 ''' ''' 或者""" """表示区间注释,在三引号之间的所有内容被注释

'''

这是多行注释,用三个单引号

这是多行注释,用三个单引号

这是多行注释,用三个单引号

'''

print("Hello China")

"""

这是多行注释,用三个双引号

这是多行注释,用三个双引号

这是多行注释,用三个双引号

"""

print("hello china")

# 2、运算符

"""

2.1 算术运算符:加(+),减(-),乘(*),除(/),整除(//)又叫地板除, 取余(%),幂(**)

"""

print(1 + 1) # 2

print(2 - 1) # 1

print(3 * 4) # 12

print(3 / 4) # 0.75

print(3 // 4) # 0

print(3 % 4) # 3

print(2 ** 3) # 8

"""

2.2 比较运算符: 大于(>)大于等于(>=) 小于(<) 小于等于(<=) 等于(==) 不等于(!=)

"""

print(2 > 1) # True

print(2 >= 4) # False

print(1 < 2) # True

print(5 <= 2) # False

print(3 == 4) # False

print(3 != 5) # True

"""

2.3 逻辑运算符: 与(and), 或(or),非(not)

"""

print((3 > 2) and (3 < 5)) # True

print((1 > 3) or (9 < 2)) # False

print(not (2 > 1)) # False

"""

2.4 位运算符: 按位取反(~),按位与(&),按位或(|),按位异或(^),左移(<<), 右移(>>)

"""

print(bin(4), bin(5)) # 0b100 0b101

print(bin(~4), ~4) # -0b101 -5

print(bin(4 & 5), 4 & 5) # 0b100 4

print(bin(4 ^ 5), 4 ^ 5) # 0b1 1 异或,不同的位为1,相同的位为 0

print(bin(4 << 2), 4 << 2) # 0b10000 16

print(bin(4 >> 2), 4 >> 2) # 0b1 1

"""

2.5 三元运算符

"""

x, y = 4,5

small = x if x < y else y

print(small) # 4

"""

2.6 其他运算符: 存在(in), 不存在(not in),是(is),不是(is not)

"""

letters = ['A', 'B', 'C']

if 'A' in letters:

print('A' + ' exists') # A exists

if 'h' not in letters:

print('h' + ' not exists') # h not exists

'''

2.6.2 比较两个变量均指向不可变类型

'''

a = "hello"

b = "hello"

print(a is b, a == b) # True, True

print(a is not b, a != b) # False False

'''

2.6.3 比较两个变量均指向可变类型

'''

a = ['hello']

b = ['hello']

print(a is b, a == b) # False True

print(a is not b, a != b) # True False

'''

注意:

is, is not 对比的是两个变量的地址, ==, !=对比的是两个变量的值

比较两个变量, 如果指向的都是地址不可变的类型(str等),则 is, is not 与 ==, != 是完全等价的

如果指向的地址是可变的类型(list, dict等),则两者是有区别的

'''

"""

2.7 运算符的优先级

"""

print(-3 ** 2) # -9 一元运算符优于二元运算符,等价于(-3)** 2

print(3 ** -1) # 0.11111111111 一元运算符优于二元运算符,等价于3 ** (-2)

print(1 << 3 + 2 & 7) # 0 先算术运算,后位移运算,最后位运算 (1 << (3 + 2)) & 7

print(-3 * 2 + 5 / -2 - 4) # -12.5 一元运算符优于二元运算符,二元运算符先乘除,后加减 (((-3)* 2) + (5 / (-2))) - 4

print(3 < 4 and 4 < 5) # True 逻辑运算符优先级最低,等价于(3 < 4)and (4 < 5)

# 3、变量和赋值

"""

变量使用前,要先赋值

变量名可以包含字母,数字,下划线, 数字不能作为开头字符

python变量名对大小写敏感

"""

teacher = "老马的程序人生"

print(teacher)

first, second = 2, 3

third = first + second

print(third)

myTeacher = "人生到处应何似,"

yourTeacher = "却似飞鸿踏雪泥"

ourTeacher = myTeacher + yourTeacher

print(ourTeacher)

# 4、数据类型与转换

"""

4.1 整型(<class 'int'>)int

"""

a = 1031

print(a, type(a)) # 1031 <class 'int'>

print(dir(int)) # Python 里面万物皆对象,整型也不例外。只要是对象,就有相应的属性(attributes)和方法(methods)

#['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

print(a, bin(a), a.bit_length()) # 1031 0b10000000111 11

"""

4.2 浮点型(<class 'float'>) float

"""

print(1, type(1)) # 1 <class 'int'>

print(1., type(1.)) # 1.0 <class 'float'>

a = 0.00000023

b = 2.3e-7

print(a, b) # 2.3e-07 2.3e-07

'''

有时候我们想保留浮点型的小数点后n位,可以用decimal包里的Decimal对象和getcontext()方法来实现

'''

import decimal

print(dir(decimal))

#['BasicContext', 'Clamped', 'Context', 'ConversionSyntax', 'Decimal', 'DecimalException', 'DecimalTuple', 'DefaultContext', 'DivisionByZero', 'DivisionImpossible', 'DivisionUndefined', 'ExtendedContext', 'FloatOperation', 'HAVE_THREADS', 'Inexact', 'InvalidContext', 'InvalidOperation', 'MAX_EMAX', 'MAX_PREC', 'MIN_EMIN', 'MIN_ETINY', 'Overflow', 'ROUND_05UP', 'ROUND_CEILING', 'ROUND_DOWN', 'ROUND_FLOOR', 'ROUND_HALF_DOWN', 'ROUND_HALF_EVEN', 'ROUND_HALF_UP', 'ROUND_UP', 'Rounded', 'Subnormal', 'Underflow', '__builtins__', '__cached__', '__doc__', '__file__', '__libmpdec_version__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'getcontext', 'localcontext', 'setcontext']

print(decimal.getcontext()) # Decimal对象默认精度是28位

from decimal import Decimal

print(Decimal(1) / Decimal(3)) # 0.3333333333333333333333333333

decimal.getcontext().prec = 4 # 保留4位小数

print(Decimal(1) / Decimal(3)) # 0.3333

"""

4.3 布尔型 (<class 'bool'>) bool

bool 型变量只取两个值, True 或者 False。

当把布尔型用在数字运算中, True = 1, False = 0

"""

print(True + True) # 2

print(True + False) # 1

print(True * False) # 0

'''

除了直接复制True, False,还可以用bool(X)创建变量。其中X可以是:

基本类型: 整型,浮点型,布尔型

容器类型:字符串,元组,列表,字典和集合

'''

# bool 作用于基本类型, 若X 是 整型0,浮点0.0, bool(X)就是False, 否则为True

print(type(0), bool(0), bool(1)) # <class 'int'> False True

print(type(10.31), bool(0.00), bool(10.31)) # <class 'float'> False True

print(type(True), bool(False), bool(True)) # <class 'bool'> False True

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。