python Notes (1) - and the underlying input cycle

This section provides a summary knowledge:

  • Entry
  • Note
  • cycle
  • String formatting
  • Operators
  • coding

In this section:

1. Enter

user_name = input("请输入你的姓名:")
message = user_name + "进入"
print(message)

note:

  • After the input is always the input string to get content
  • py version differences:
    • py2: name = raw_input ( 'Enter')
    • py3: name = input ( 'Enter')
user_name = input("请输入你的姓名:")
password = input("请输入你的密码:")

#用户名和密码拼接
content = "你的用户名是:" + user_name + ";你的密码是:" + passwod

#输出用户名和密码拼接的结果
print(content)

2. Comment

# 单行注释

"""
多行注释
"""

3. Cycle

3.1 if..else

Primary conditional statement:

gender = input("请输入性别:")
"""
如果是男生:打印再见
否则:打印来呀
"""

if gender == "男"
    print("再见")
else:
    print("来呀")

2.elif conditions

gender = input("请输入性别:")
"""
如果是男生:打印再见
如果是女生:打印来呀
如果是人妖:打印找别人去
否则:打印滚
最后加end
"""

if gender == "男"
    print("再见")
elif gender == "女"
    print("来呀")
elif gender == "人妖"
    print("找别人去")
else:
    print("滚")
print("end")

3. Exercises

#让用户输入一个数字,猜:如果数字 > 50,则输出:大了;如果数字 <= 50,则输出:小了。
num = input("请输入一个数字:")
number = int(num)
if number > 50:
    print("大了")
else:
    print("小了")
    
#用户名密码登录
username = input('请输入用户名:')
password = input('请输入密码:')
if username == "alex" and password == "123":
    print("欢迎")
else :
    print("用户名密码或错误")

note:

  • After the input is a string, the string can not be compared, pay attention to the type of conversion

3.2 if nested

message = """欢迎致电10086
1.话费查询;
2.流量服务;
3.业务办理;
4.人工服务"""
print(message)

index = input("请选择业务类型:")
index = int(index)
if index == 1:
    print("话费查询")
elif index == 2:
    print("流量服务")
elif index == 3:
    content = """业务办理
    1.修改密码
    2.修改套餐
    3.停机
    """
    print(content)
    value = input("请选择所需的业务办理类型")
    value = int(value)
    if value == 1:
        print("修改密码")
    elif value == 2:
        print("修改套餐")
    elif value == 3:
        print("停机")
    else:
        print("输入错误")
elif index == 4:
    print("人工服务")
else:
    print("输入错误")

3.3 while loop:

After 3.3.1 while adding conditions

count = 1
while True:
    print(count)
    count = count + 1    #结果为count每次加1,无限循环
while True:
    count = 1
    print(count)
    count = count + 1    #结果为1,无限循环
count = 1
while count <= 10:
    print(count)
    count = count + 1   #结果为1-10

Exercise: Print 10, no 7

count = 1
while count <= 10:
    if count != 7:
        print(count)
    count = count + 1                       #方法一:使用不等于!=
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
count = 1
while count <= 10:
    if count == 7:
        pass
    else:
        print(count)
    count = count + 1                       #方法二:使用不等于pass

3.3.2 Keywords: break

Note: break to end while the entire cycle

3.3.3 Keyword: continue

Note: continue to re-start this cycle

3.3.4 loop while else (not used)

count = 1
while count <= 10:
    print(count)
    count = count + 1
else:                    #不在满足while循环条件后触发,或条件等于False
    print("else语句")
print("结束")
#结果:打印1-10 else语句 结束

3.3.5 while summary:

  • body of the while (+ after determination condition)
  • break ends the while loop
  • continue the end of this cycle, the cycle begins again
  • while else loop condition is not met after the trigger, or condition equal to False

2. string formatting:

% S is a placeholder, parameters separated by commas

name = input("请输入你的名字:")
do = input("在干什么:")

template = "%s在教室,%s."%(name,do)
print(template)
name = 'alex'
template = "%s现在手机的电量是100%%"%(name)    #参数内如果有%,使用两个%%可以不进行占位符
print(template)

Knowledge points:

  • % S placeholder, the placeholder may represent all
  • % D placeholder numeric placeholder
  • %% placeholder canceled, the output is a%
name = input("请输入姓名:")
age = input("请输入年龄:")
age = int(age)
job = input("请输入工作:")
hobby = input("请输入爱好:")
msg = '''
------------info------------
Name  :   %s
age   :   %d
job   :   %s
hobby :   %s
------------end-------------
'''
data = msg %(name,age,job,hobby)
print(data)

3. Operator

+ Plus - minus * multiplication, / division sign,% remainder, a power of ** // rounded

###打印1-100内的奇数
count = 1
while count <= 100:
    val = count % 2
    if val == 1:
        print(count)
    count=count + 1     #方法1
###################
count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count=count + 1     #方法2
###练习题,1-100以内所有数字相加
total = 0
count = 1
while count <= 100:
    total = total + count
    count=count + 1
print(total)

Conversion:

  • String (str) to Digital (int)
  • Digital (int) transfected string (str)
  • Null string ( "") and digital (0) after the turn Boolean is False, True all remaining

or * Note: value will first determine whether True, the output value of True

value = 1 or 9
print(value)     #会先判断1和9是否为True,1为True,9就不做判断,输出结果为1

value = 0 or 9
print(value)     #会先判断0和9是否为True,0为False,再判断9为True,输出结果为9

value = 0 or ""
print(value)     #0和“”都为False,输出结果为空,输出最后一个结果

and * Note: first determine whether to False, output False, if all is True, the last output value

v1 = 1 and 9            #会先判断1,为True,再判断9,为True。输出9
v2 = 1 and 0            #会先判断1,为True,再判断0,为False。输出0
v3 = 0 and 7            #会先判断0,为False,输出0
v4 = 0 and ""           #会先判断0,为False,输出0
v5 = 1 and 7 and ""     #会先判断1和7,输出7,再判断7和“”,输出空值

Comprehensive:

v6 = 1 and 9 or 0 and 6 #会先判断and,1 and 9,结果是9;再判断0 and 6,结果为0,再判断9 or 0,输出9

A logical operation: in the absence () is, not higher than the priority and, above or, as the relationship ()> not> and> or, the same priority is calculated from left to right

4. encoding

  • Coding extension
    • ascii
    • Unicode
      • ecs2 beginning out with
      • ecs4 later began to use
    • utf-8, Chinese three bytes, (for global use) must be encoded using the utf-8
    • utf-16
    • gbk, Chinese use two bytes (Asia use)
    • gb2312, Chinese use two bytes (Asia use) gbk extension

Coding units:

  • String: "China" "hello"

  • Character: "in the" "e"

  • Byte: encoding unicode (a character of 4 bytes), encoding utf-8 (3 bytes for the Chinese words, the English alphabet is a single byte)

  • Place: 01010101

    Unit conversion:

    - 8bit(位)=1byte(字节)
    - 1024byte(字节)=1KB
    - 1024GB=1TB
    - 1024TB=1PB
    - 1024PB=1EB

ASCII code to add

Coding usage scenarios:

  • Data calculation computer memory, data length will match the sum will be used unicode.
  • Byte using a relatively small data transmission coding, e.g. utf-8

Guess you like

Origin www.cnblogs.com/lynlearnde/p/11579967.html