Keywords and bit operations
1.continue
- continue is a keyword. If you encounter continue, the current loop will end immediately, and immediately enter the judgment of the next loop (when it encounters continue, the loop will end)
Exercise: The sum of all numbers from 1 to 100 that are not divisible by 3
method one:
a = 0
for x in range(1, 101):
if x % 3 == 0:
continue
a += x
print(a)
num = 0
for x in range(1, 101):
if x % 3 != 0:
num += x
print(num)
2.break
break is a keyword and can only be used in the loop body
When the body of the loop is executed, if a break is encountered, the entire loop ends directly
for x in range(4):
print('-----------')
print('++++++++++')
break # 执行到了break,直接结束了循环
print('=========')
print('//')
3. Use while loop and break together
while True:
Need to repeat operations
The condition for the end of the if loop:
break
Application: Use while to calculate 1+2+3+. . . +100
num = 0
a = 1
while True:
num += a
a += 1
if a > 100:
break
print(num, a)
Exercise: Find the first number above 1000 that is divisible by 3
num = 1000
while True:
if num % 3 == 0:
break
num += 1
print(num)
4.else
Complete for loop in python;
for variable in sequence:
loop body
else:
code segment
Complete while loop in python;
for conditional statement:
loop body
else:
code segment
Adding the else structure after the for loop or while loop will not affect the execution of the original loop.
If the loop ends normally, the loop will end, and the code under else will be executed. If the break ends, the code segment
under else will not be executed (you can judge whether the code segment after the else is executed or not to determine that the loop did not encounter break during execution. )
- Exercise: Determine whether a number is prime:
num = int(input('输入数字:'))
# for x in range(2, num): # 方法1
for x in range(2, int(num**1/2+1)): # 方法2
if num % x == 0:
print('不是素数')
break
else:
print(num, '是素数')
Data storage and bit operations
Calculators store data, only binary data (computers only have the ability to store numbers, and they store the two's complement of this number)
1. Base
1) Decimal
Base: 0,2,3,...9 Carrying
: Every decimal
point Power: 10 to the nth power (starting from 0)
Representation in the program: write directly
2) Binary
Base: 0,1 (001,110,101001)
Carry: Every binary enters one 100 + 100 +>
Bit weight: 2 to the nth power (starting from 0)
Representation in the program: prefix /0b
3) Octal
Base: 0, 1, 2, 3. . . , 7 (134, 276, 137)
Carry: Carry for every eighth
Bit power: 8 to the nth power (starting from 0)
Representation in the program: write directly
4) Hexadecimal
Base: 0 9, A F
Carry: Every F Carry One
Bit Weight: 16 to the nth power (n starts from 0)
Representation in the program: prefix, 0x
2. Base conversion
1) Convert other bases to base 10: base multiplied by the sum of bit weight
0B011 ->20+21+23==11
0o56 ->68**0,58**1
2) Decimal conversion (remainder method)
Binary conversion method, bin-convert other bases into binary, convert
octal method, oct-convert other bases into octal, convert
hexadecimal method, hex-convert other bases into hexadecimal
3. Original code, inverse code and complement
- The original code, one's complement and the complement of a positive number are the same
1) Original code: sign bit + true value
The truth value is the binary of the absolute value of the number. The characteristic of the sign bit is that 0 represents a positive value, and 1 represents a negative value
. The truth value of 10: 1010 -10 True value: 1010
Original code of 10: 01010, Original code of -10:
The original code of 10 in 11010 computer: 00001010 The original code of -10: 10001010
2) Inverse code-the sign bit remains unchanged, the other bits are inverted
The complement of -10 (1 byte): 11110101
3) One's complement-one's complement plus 1
-10's complement: 11110110
Bit operation:
& (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise inversion), << (Left shift), >> (Right shift)
Features of bit operation: operation efficiency High, low memory consumption; but difficult to complete complex calculations
- 1) Quickly judge the parity of the number: the number & 1 directly manipulate the bottom layer to determine whether the last digit is 0 or 1, 0. is an integer, and 1 is an odd number
print(8 & 1) - 2) Quickly multiply an integer by 2 or divide by 2 operand number <<2
print(8 << 1)