Share knowledge of Python fourth day

Section python

1.while cycle

Python programming while statement for executing a program loop, i.e., under certain conditions, implementation of certain program loop, the same processing task needs to repeat the process. The basic form:

while 条件:
    循环体

1.1 infinite loop example

while True:
    print("我会一直执行")

Example 1.2 finite loop (using the break out of the loop)

while True:
    print("我会一直执行")
    print("一直执行")
    print("直到遇到break")
    break

1.3 control cycles (while used in conjunction with a break)

count = 0
while True:
    print("条件成立我会一直执行")
    print("一直执行")
    print("控制为3次循环")
    count += 1
    if count == 3:
        break

1.4 break与continue

while the break statement with recycling break completely finished for one cycle, out of the back of the loop execution cycle

continue only terminate the present cycle, and then later further execution cycle

# break代码示例
print("我一定会执行")
while 3 > 1:
    print("满足条件我会执行")
    break
    print("上面使用了break终止了循环,因此我不会执行")

# continue代码示例
print("我一定会执行")
while 3 > 1:
    print("满足条件我会执行")
    print("满足条件我一样执行")
    continue
    print("上面使用了continue,因此我不会执行")

Using a cyclic accumulation 1.5

count = 1
num = 0
while count < 16:
    num = num + count
    count += 1
print(num)

1.6 while else

while 3 < 1:
    print(1111)
    break
else:
    print(3333)

2. Format

2.1 Use placeholders

% S represents a string is a placeholder, in addition, there% d, digital placeholder

% () Representing the time to give the filling position, inside one correspondence variables

name = input("你输入的名字:")
age = int(input("你的年龄:"))
msg = """
----------------card--------------
name:%s
age:%d
----------------end---------------
"""
print(msg % (name, age))

Note: If you use% this format, you want to be written in simple output%%%

name = input("名字:")
num = input("进度:")
msg = "姓名:%s当前的进度为%s%%" % (name, num)
print(msg)

2.2 f, the following format:

name = input("你输入的名字:")
age = int(input("你的年龄:"))
msg = f"""
----------------card--------------
name:{name}
age:{age}
----------------end---------------
"""
print(msg)

3. Operator

Arithmetic operators 3.1

a = 9
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

3.2 Assignment Operators

a = 9
b = 3
a += b
print(a)
a -= b
print(a)
a *= b
print(a)
a /= b
print(a)
a //= b
print(a)
a %= b
print(a)
a **= b
print(a)

3.3 comparison operator

print(3 > 2)
print(3 < 2)
print(3 >= 2)
print(3 <= 2)
print(3 == 2)
print(3 != 2)

3.4 member operator

in

not in

3.5 Logical Operators

and

or

not

4. encoding acquaintance

ascii : 一个英文占一个字节,不支持中文
gbk   : 一个英文占一个字节,一个中文占2个字节
unicode : 英文和中文 都占4个字节
utf-8 : 一个英文占一个字节,欧洲2个字节,亚洲3个字节
# 单位转换:
1字节 = 8位

Guess you like

Origin www.cnblogs.com/tianming66/p/11703020.html