04-4 python Grammar of interaction with the user, operator

[TOC]

A program interacts with the user

1.1 What is the interaction with the user

User interaction is the person to the computer input / input data, the computer print / output

1.2, why should interact with the user?

Illustration: spoof 12

04-4 python Grammar of interaction with the user, operator

In order to allow computers to communicate with the user like humans

For example, in the past we go to the bank to withdraw money, you need to tell the teller account password, and now, to be replaced by ATM teller machine, ATM machine is a computer, so the user will also have to tell the computer account password, so we must have a program appropriate mechanism to control a computer to receive user input, and outputs the result

1.3, how to interact with the user

04-4 python Grammar of interaction with the user, operator

The nature of the interaction is the input, output

1.3.1 Input input:

# 在python3中input功能会等待用户的输入,用户输入任何内容,都存成字符串类型,然后赋值给等号左边的变量名
>>> username=input('请输入您的用户名:') 
请输入您的用户名:jack # username = "jack"
>>> password=input('请输入您的密码:') 
请输入您的密码:123 # password = "123"

# 了解知识:
# 1、在python2中存在一个raw_input功能与python3中的input功能一模一样
# 2、在python2中还存在一个input功能,需要用户输入一个明确的数据类型,输入什么类型就存成什么类型
>>> l=input('输入什么类型就存成什么类型: ')
输入什么类型就存成什么类型: [1,2,3]
>>> type(l)
<type 'list'>

1.3.2 Output print:

>>> print('hello world')  # 只输出一个值
hello world
>>> print('first','second','third')  # 一次性输出多个值,值用逗号隔开
first second third

# 默认print功能有一个end参数,该参数的默认值为"\n"(代表换行),可以将end参数的值改成任意其它字符
print("aaaa",end='')
print("bbbb",end='&')
print("cccc",end='@')
#整体输出结果为:aaaabbbb&cccc@

1.3.3 the output of the formatted output

(1) What is the formatted output?

The something inside a string and then replaced after the output is formatted output.

(2) Why do you want to format the output?

We often content with some fixed output formats, such as: 'Dear xxx Hello! Your monthly bill is xxx xxx, the balance is xxx ', we need to do is to replace the xxx specific content.

Illustration: spoof 13
04-4 python Grammar of interaction with the user, operator

(3) how to format the output?

This uses a placeholder, such as:% s,% d:

# %s占位符:可以接收任意类型的值
# %d占位符:只能接收数字
>>> print('亲爱的%s你好!你%s月的话费是%d,余额是%d' %('tony',12,103,11))
亲爱的tony你好!你12月的话费是103,余额是11

# 练习1:接收用户输入,打印成指定格式
name = input('your name: ')
age = input('your age: ') #用户输入18,会存成字符串18,无法传给%d
print('My name is %s,my age is %s' %(name,age))

# 练习2:用户输入姓名、年龄、工作、爱好 ,然后打印成以下格式
------------ info of Tony -----------
Name  : Tony
Age   : 22
Sex   : male
Job   : Teacher 
------------- end -----------------

Two basic operators

2.1 Arithmetic Operators

The symbols used are consistent calculation of the arithmetic operators python support and Mathematics, we have x = 9, y = 2 as an example to introduce them sequentially

04-4 python Grammar of interaction with the user, operator

2.2 Comparison Operators

Comparison operation to compare two values, it returns Boolean True or False, we x = 9, y = 2 as an example to introduce them sequentially
04-4 python Grammar of interaction with the user, operator

2.3 Assignment operators

In addition to the python syntax = number This simple assignment operator, but also supports incremental assignment, the assignment chain, cross assignment, extract assignment, meaning the existence of these assignment operators are to make our code look more streamlined . We have x = 9, y = 2, for example first to introduce augmented assignment

### 2.3.1 Augmented assignment
04-4 python Grammar of interaction with the user, operator

### 2.3.2 Chain assignment

If we want to assign the same value simultaneously to multiple variable names, you can do

>>> z=10
>>> y=z
>>> x=y
>>> x,y,z
(10, 10, 10)

Chain assignment means that you can get it with a single line of code

>>> x=y=z=10
>>> x,y,z
(10, 10, 10)

Illustration: spoof 14
04-4 python Grammar of interaction with the user, operator

### 2.3.3 Cross assignment

We define two variables m and n

>>> m=10
>>> n=20

If we want to swap the values ​​of m and n is over, you can do so

>>> temp=m
>>> m=n
>>> n=temp
>>> m,n
(20, 10)

Cross assignment refers to the line of code you can get it

>>> m=10
>>> n=20
>>> m,n=n,m # 交叉赋值
>>> m,n
(20, 10)

Illustration: spoof 15
04-4 python Grammar of interaction with the user, operator

2.3.4 decompression assignment

If we want more value in the list in order to take out a number of variables assigned to the name, you can do so

>>> nums=[11,22,33,44,55]
>>> 
>>> a=nums[0]
>>> b=nums[1]
>>> c=nums[2]
>>> d=nums[3]
>>> e=nums[4]
>>> a,b,c,d,e
(11, 22, 33, 44, 55)

Decompression assignment refers to a single line of code can handle it

>>> a,b,c,d,e=nums # nums包含多个值,就好比一个压缩包,解压赋值因此得名
>>> a,b,c,d,e
(11, 22, 33, 44, 55)

Illustration: spoof 16
04-4 python Grammar of interaction with the user, operator

Note that the above extract the assignment, the number of variables must contain the name of the equal sign on the left and on the right the same number of values, otherwise it will error

#1、变量名少了
>>> a,b=nums
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

#2、变量名多了
>>> a,b,c,d,e,f=nums
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 6, got 5)

But if we want to take the head and tail of a few values, you can use * _ match

>>> a,b,*_=nums
>>> a,b
(11, 22)

ps: strings, dictionaries, tuples, collection types support decompression assignment

Illustration: spoof 17
04-4 python Grammar of interaction with the user, operator

2.4 Logical Operators

Logical operators for connecting a plurality of conditions, the association determination, returns Boolean True or False
04-4 python Grammar of interaction with the user, operator

2.4.1 continuous and more

And connecting a plurality of conditions can be used, in turn determines the order from left to right, once a condition is False, the determination no longer right, immediately decides the final result is False, only the results of all the conditions are True case, the end result was to True.

>>> 2 > 1 and 1 != 1 and True and 3 > 2 # 判断完第二个条件,就立即结束,得的最终结果为False
False

2.4.2 multiple or continuous

Or can be connected to a plurality of conditions, determined in turn from left to right in the order, once a condition is True, no longer right determination, it decides the final result immediately is True, only the results of all the conditions are under the False case, the end result was to False

>>> 2 > 1 or 1 != 1 or True or 3 > 2 # 判断完第一个条件,就立即结束,得的最终结果为True
True

2.4.3 混用and、or、not

# and、or、not三者如果混用时,是存在优先级之分的,但在日常开发中我们无需记忆优先级,应该使用()来区分优先级、提升程序的可读性
>>> (3>4 and 4>3) or ((1==3 and 'x' == 'x') or 3 >3)
False 

2.5 member operator

04-4 python Grammar of interaction with the user, operator

Note: Although the following two judges can achieve the same effect, but we recommend using the second form, because not in semantics more explicit

>>> not 'lili' in ['jack','tom','robin']
True
>>> 'lili' not in ['jack','tom','robin']
True

2.6 identity of the operator

04-4 python Grammar of interaction with the user, operator

It is emphasized that: == double equals sign is the value of the comparison are equal, and is comparable is the id are equal

#1. id相同,内存地址必定相同,意味着type和value必定相同
#2. value相同type肯定相同,但id可能不同,如下
>>> x='Info Tony:18'
>>> y='Info Tony:18'
>>> id(x),id(y) # x与y的id不同,但是二者的值相同
(4327422640, 4327422256)

>>> x == y # 等号比较的是value
True
>>> type(x),type(y) # 值相同type肯定相同
(<class 'str'>, <class 'str'>)
>>> x is y # is比较的是id,x与y的值相等但id可以不同
False

04-4 python Grammar of interaction with the user, operator

Guess you like

Origin blog.51cto.com/egon09/2460997