一、操作符

format 方法

#输入
age = 25
name = 'Swaroop'
print('{0} is {1} years old'.format(name, age))
print('Why is {0} playing with that python?'.format(name))
#输出
Swaroop is 25 years old
Why is Swaroop playing with that python?

原理: 一个字符串能使用确定的格式,随后,可以调用format 方法来代替这些格式,参数要与format 方法的参数保持一致。

#输入
>>> '{0:.3}'.format(1/3) # decimal (.) precision of 3 for float
#输出
'0.333'
#输入
>>> '{0:_^11}'.format('hello') # fill with underscores (_) with the
text centered (^) to 11 width
#输出
'___hello___'

标识符(变量命名)

  • 标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘_’)。
  • 标识符名称的其他部分可以由字母(大写或小写)、下划线(‘_’)或数字(0-9)组成。
  • 标识符名称是对大小写敏感的。

代码的缩进

不要混合使用制表符和空格来缩进,因为这在跨越不同的平台的时候,无法正常工作。我强烈建议你在每个缩进层次使用单个制表符或两个或四个空格。选择这三种缩进风格之一。更加重要的是,选择一种风格,然后一贯地使用它,即只使用这一种风格。

十进制转二进制

1.将整数除以2,取余数,并将商除以2,取余数,再除以2……
2.将余数按倒序排列,高位补零

(53)_10 =
(110101)_2

按位与 :

按位与运算符"&"是双目运算符。其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1 ,否则为0。参与运算的数以补码方式出现。

按位或 :

按位或运算符“|”是双目运算符。其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就为1。参与运算的两个数均以补码出现。

按位或与 :

按位异或运算符“^”是双目运算符。其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。参与运算数仍以补码出现,

原码:将最高位作为符号位(0表示正,1表示负),其它数字位代表数值本身的绝对值的数字表示方式。
反码:如果是正数,则表示方法和原码一样;如果是负数,符号位不变,其余各位取反,则得到这个数字的反码表示形式。
补码:如果是整数,则表示方法和原码一样;如果是负数,则将数字的反码加上1(相当于将原码数值位取反然后在最低位加1)。

运算符

python pymupdf 处理ofd 报错 eof error python_与运算


python pymupdf 处理ofd 报错 eof error python_数据分析_02


python pymupdf 处理ofd 报错 eof error python_与运算_03

简便表示
a = 2; a = a * 3
#可以写成
a = 2; a *= 3

建议使用括号来表示优先级
例子:

length = 5
breadth = 2
area = length * breadth
print('Area is', area)
print('Perimeter is', 2 * (length + breadth))
#输出
Area is 10
Perimeter is 14 #python自动添加了空格



二、控制流——if、for 和while

1. if语句

number = 23
guess = int(input('Enter an integer : ')) #input() 函数取得用户猜测的数字
if guess == number:
    print('Congratulations, you guessed it.') # New block starts here
    print('(but you do not win any prizes!)') # New block ends here
elif guess < number:
    print('No, it is a little higher than that') # Another block# You can do whatever you want in a block ...
else:
    print('No, it is a little lower than that')# you must have guess > number to reach here
print('Done')

注意我们使用了缩进层次来告诉Python 每个语句分别属于哪一个块。
这就是为什么缩进在Python 如此重要的原因。
注意if 语句在结尾处包含一个冒号
我们通过它告诉Python 下面跟着一个语句块。

2. while 语句

只要在一个条件为真的情况下, while 语句允许你重复执行一块语句。while 语句是所谓循环语句的一个例子。while 语句有一个可选的else 从句。

number = 23
running = True
while running:
	guess=int(input("Enter an integer:"))
	if guess == number:
		print("Congratulation, you guessed it.")
		running=False #this causes the while loop to stop
	elif guess < number:
		print("No, it is a little higher.")
	else:
		print("No, it is a little lower.")
else:
	print("the while loop is over.")# Do anything else you want to do here
print("done")

注意制表符的运用

3. for 循环

for…in 是另外一个循环语句

for i in range ( 1 , 5 ):  #i是一个局部变量,名称任意。也可以使用items
	print (i)
else :
	print ( 'The for loop is over' )
#输出
1
2
3
4
The for loop is over

else 部分是可选的
for后的变量名是任意的,for循环就是让变量依次取各个值,再带入for中的语句。

range 返回一个序列的数。这个序列从第一个数开始到第二个数为止。
例如, range(1,5) 给出序列[1, 2, 3, 4]。默认地, range 的步长为1。如果我们为range 提供第三个数,那么它将成为步长。例如,range(1,5,2) 给出[1,3]。
记住,range 向上延伸到第二个数,即它不包含第二个数

4. break 语句

break 语句是用来终止循环语句的,哪怕循环条件没有变为False 或序列还没有被完全迭代结束,也停止执行循环语句。

while True:
	s = (input('Enter something : '))
	if s == 'quit':  #如果符合条件,则执行break,否则跳过,没有else语句
		break
	print('Length of the string is', len(s))
print('Done') #注意制表符的使用,每一层对应

break 语句也可以用在for 循环语句中。

5. continue 语句

跳过当前循环块中的剩余语句,然后继续进行下一轮循环。

while True:  #循环内有四句并列,s、if、if、print。
	s = raw_input('Enter something : ')
	if s == 'quit':
		break
	if len(s) < 3:
		print('Too small')
		continue#属于第二个if内的语句
	print('Input is of sufficient length')
# Do other kinds of processing here...

最后一行print语句属于循环内,与if并列
当两个if都不满足时,表明长度大于3,则打印出那一句。
如果没有continue,则当字符数小于3是也会在执行print