一、变量、运算符与数据类型
1.1 注释的方式
1.1.1 单行注释
在Python中,#是表示注释,作用与整行
1.1.2 多行注释
‘’‘注释内容’’'或者""“注释内容”""表示注释
如:
'''
我被注释了!
(多行注释)
'''
# ↑注释(我是单行注释)
x = '我才是代码!'
# ↓注释(我也是单行注释)
"""
我也被注释了!
(多行注释)
"""
1.2 运算符
运算符就很easy了
操作符
名称
+
加
-
减
*
乘
/
除
//
整除
%
取余
**
幂
1.2.1 比较运算符
同样比较运算符也是非常简单的!
操作符
名称
示例
>
大于
3 > 1
<
小于
1 < 3
>=
大于等于
3 >= 1
<=
小于等于
1 <= 3
==
等于
3 == 3
!=
不等于
1 != 3
1.2.2 逻辑运算符
逻辑运算符高中应该学过“或”、“与”、“非”。
操作符
名称
示例
and
与
(2 > 1)and (3 >7)
or
或
(1 > 3) or (2 < 9)
not
非
not(1 < 2)
print((2 > 1) and (3 >7)) #False 3>7不成立
print((3 > 1) and (5 > 3)) #True 两个条件均成立
print((1 < 10) or (10 < 1)) #True 1 < 10成立
1.2.3 位运算
①原码、反码和补码
二进制有三种不同的表示形式:原码、反码和补码,计算机内部使用补码来表示。
原码
:就是其二进制表示(注意,最高位是符号位)。
如:
00 00 00 11 -> 3
10 00 00 11 -> -3
反码
:正数的反码是其本身,负数的反码是在其原码的基础上, 符号位不变,其余各个位取反。
00 00 00 11(原码)= 00 00 00 11(反码) -> 3
10 00 00 01(原码)= 11 11 11 10(反码)-> -1
可见如果一个反码表示的是负数, 人脑无法直观的看出来它的数值. 通常要将其转换成原码再计算.
补码
补码的表示方法是:
正数的补码就是其本身
负数的补码是在其原码的基础上, 符号位不变, 其余各位取反, 最后+1. (即在反码的基础上+1)
如
00 00 00 01 (原)=
00 00 00 01 (反)=
00 00 00 01 (补) = +1
--------------
10 00 00 01(原)=
11 11 11 10(反)=
11 11 11 11(补)= -1
对于负数, 补码表示方式也是人脑无法直观看出其数值的. 通常也需要转换成原码再计算其数值.
② 位运算符
操作符
名称
示例
~
按位取反
&
按位与
|
按位或
^
按位异或
<<
左移
>>
右移
具体可查看
link
③ 三元运算符
x,y = 4,5
if x < y:
small = x
else:
small = y
print(small) #4
有了这个三元操作符的条件表达式,你可以使用一条语句来完成以下的条件判断和赋值操作。
二、变量和赋值
在使用变量之前,需要对其先赋值。
变量名可以包括字母、数字、下划线、但变量名不能以数字开头。
Python 变量名是大小写敏感的,foo != Foo。
Python在这一点比c++要简单一些,不用繁杂的变量定义
teacher = "老马的程序人生"
print(teacher) # 老马的程序人生
myTeacher = "老马的程序人生"
yourTeacher = "小马的程序人生"
ourTeacher = myTeacher + ',' + yourTeacher
print(ourTeacher) # 老马的程序人生,小马的程序人生
三、 数据类型与转换
类型
名称
示例
int
整型
-10,10
float
浮点型
3.1415,11.11
bool
布尔型
True,False
整型
a = 1031
print(a, type(a))
# 1031
Python 里面万物皆对象(object),整型也不例外,只要是对象,就有相应的属性 (attributes) 和方法(methods)。
b = dir(int)
print(b)
# ['__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']
对它们有个大概印象就可以了,具体怎么用,需要哪些参数 (argument),还需要查文档。看个bit_length()的例子。
【例子】找到一个整数的二进制表示,再返回其长度。
a = 1031
print(bin(a)) # 0b10000000111
print(a.bit_length()) # 11
浮点型
print(1, type(1))
# 1
print(1., type(1.))
# 1.0
a = 0.00000023
b = 2.3e-7
print(a) # 2.3e-07
print(b) # 2.3e-07
如果想保留浮点型的小数点后 n 位。可以用 decimal 包里的 Decimal 对象和 getcontext() 方法来实现。
import decimal
from decimal import Decimal
Python 里面有很多用途广泛的包 (package),用什么你就引进 (import) 什么。包也是对象,也可以用上面提到的dir(decimal) 来看其属性和方法。比如getcontext() 显示了 Decimal 对象的默认精度值是 28 位( prec=28 ),展示如下:
a = decimal.getcontext()
print(a)
# Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0,
flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
当然也可以用 getcontext().prec 来调整精度。
布尔型
布尔 (boolean) 型变量只能取两个值, True 和 False 。当把布尔变量用在数字运算中,用 1 和 0 代表 True和 False 。
print()函数
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按str()方式进行转换为字符串输出;
关键字参数sep是实现分隔符,比如多个参数输出时想要输出中间的分隔字符;
关键字参数end是输出结束时的字符,默认是换行符\n;
关键字参数file是定义流输出的文件,可以是标准的系统输出sys.stdout,也可以重定义为别的文件;
关键字参数flush是立即把内容输出到流文件,不作缓存。
【例子】没有参数时,每次输出后都会换行。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed without 'end'and 'sep'.")
for item in shoplist:
print(item)
# This is printed without 'end'and 'sep'.
# apple
# mango
# carrot
# banana
【例子】每次输出结束都用end设置的参数&结尾,并没有默认换行。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'end='&''.")
for item in shoplist:
print(item, end='&')
print('hello world')
# This is printed with 'end='&''.
# apple&mango&carrot&banana&hello world
【例子】item值与’another string’两个值之间用sep设置的参数&分割。由于end参数没有设置,因此默认是输出解释后换行,即end参数的默认值为\n。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'sep='&''.")
for item in shoplist:
print(item, 'another string', sep='&')
# This is printed with 'sep='&''.
# apple&another string
# mango&another string
# carrot&another string
# banana&another string