A program interacts with users
1.1. What is user interaction
User interaction is when people input/input data to a computer, and the computer print s/outputs results
1.2. Why should I interact with users?
Illustration: Fig. 12
To enable computers to communicate with users like people
For example, in the past, when we went to the bank to collect money, the user needed to tell the counter the account password. Now, the counter is replaced by the ATM machine, which is a computer, so the user also needs to tell the computer the account password, so we must have a corresponding mechanism in our program to control the computer to receive user input and output results.
1.3. How to interact with users
The essence of interaction is input and output
#The input function in python3 waits for input from the user, who enters anything, saves it as a string type, and assigns it to the variable name to the left of the equal sign > username=input ('Please enter your username:') Please enter your user name: jack # username = "jack" > password=input ('Please enter your password:') Please enter your password: 123 # password = "123" #Learn about knowledge: # 1, there is a raw_input function in Python 2 that is identical to the input function in Python 3 # 2. There is also an input function in Python 2 that requires the user to enter a specific data type, which is what type to enter. >> l=input ('Type in what type exists:') Type as entered: [1,2,3] >>> type(l) <type 'list'>
1.3.2 Output print:
>>> print('hello world') # Output only one value hello world >>> print('first','second','third') # Output multiple values at once, separated by commas first second third # The default print function has an end parameter whose default value is "\n" (for line breaks), which can be changed to any other character print("aaaa",end='') print("bbbb",end='&') print("cccc",end='@') #The overall output is aaaabbbb&cccc@
Formatted Output of 1.3.3 Output
(1) What is formatted output?
Formatted output is the output that replaces something in a string and outputs it later.
(2) Why format the output?
We often output content in a fixed format, such as:'Hi, dear xxx!Your XXX monthly call is XXX and your balance is xxx'. All we need to do is replace XXX with something specific.
Illustration: Hoax Fig. 13
(3) How to format the output?
This uses placeholders, such as:%s,%d:
# %s placeholder: can accept any type of value # %d placeholder: can only receive numbers >>> print('Dear%s Hello!you%s Monthly call is%d，The balance is%d' %('tony',12,103,11)) //Hello, dear tony!Your December call is 103 and your balance is 11 # Exercise 1: Receive user input and print to specified format name = input('your name: ') age = input('your age: ') #User input 18, saved as string 18, cannot pass to%d print('My name is %s,my age is %s' %(name,age)) # Exercise 2: Users enter names, ages, jobs, hobbies, and then print them in the following format ------------ info of Tony ----------- Name : Tony Age : 22 Sex : male Job : Teacher ------------- end -----------------
Two Basic Operators
2.1 Arithmetic Operator
Pthon's supported arithmetic operators are consistent with the use of mathematically calculated symbols, which are described in turn using x=9 and y=2 as examples
2.2 Comparison Operator
The comparison operation compares two values and returns Boolean values of True or False, which are described in turn in x=9 and y=2
2.3 Assignment Operator
In addition to the simple assignment operation of = sign, python syntax supports incremental assignment, chain assignment, cross-assignment, and decompression assignment, all of which exist to make our code look simpler.Let's start with x=9 and y=2 to introduce incremental assignment
###2.3.1 incremental assignment
###2.3.2 Chain Assignment
If we want to assign the same value to multiple variable names at the same time, we can do so
>>> z=10 >>> y=z >>> x=y >>> x,y,z (10, 10, 10)
Chain assignment means you can do this in one line of code
>>> x=y=z=10 >>> x,y,z (10, 10, 10)
Illustration: Joke Fig. 14
We define two variables m and n
>>> m=10 >>> n=20
If we want to swap m with n, we can do this
>>> temp=m >>> m=n >>> n=temp >>> m,n (20, 10)
Cross-assignment refers to a line of code that does this
>>> m=10 >>> n=20 >>> m,n=n,m # Cross-assignment >>> m,n (20, 10)
Illustration: Joke Fig. 15
2.3.4 Decompression assignment
If we want to take multiple values out of the list and assign them to multiple variable names in turn, we can do this
>>> nums=[11,22,33,44,55] >>> >>> a=nums >>> b=nums >>> c=nums >>> d=nums >>> e=nums >>> a,b,c,d,e (11, 22, 33, 44, 55)
Unzip assignment refers to a line of code that does this
>>> a,b,c,d,e=nums # nums contains multiple values, like a compressed package, so the decompression assignment gets its name >>> a,b,c,d,e (11, 22, 33, 44, 55)
Illustration: Joke Fig. 16
Note that for the above decompression assignment, the number of variable names on the left side of the equal sign must be the same as the number of values on the right side, otherwise an error will occur
#1. The variable name is missing >>> a,b=nums Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: too many values to unpack (expected 2) #2. A lot of variable names >>> 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 only want to start and end with a few values, we can match with *_
>>> a,b,*_=nums >>> a,b (11, 22)
ps: String, dictionary, tuple, collection type all support decompression assignment
Illustration: Joke Fig. 17
2.4 Logical Operators
Logical operators are used to join multiple conditions, make association judgments, and return Boolean values of True or False
2.4.1 Consecutive and
You can use and to connect multiple conditions, which will be judged in order from left to right. Once a condition is False, you can immediately decide that the final result is False without any further judgment to the right. Only if the results of all conditions are True, the final result is True.
>>> 2 > 1 and 1 != 1 and True and 3 > 2 # When the second condition is judged, it ends immediately and the final result is False False
2.4.2 consecutive or
You can use or to connect multiple conditions, which will be judged in order from left to right. Once a condition is True, you do not need to judge to the right anymore. You can immediately determine that the final result is True. Only if all conditions are False, the final result is False.
>>> 2 > 1 or 1 != 1 or True or 3 > 2 # When the first condition is judged, it ends immediately and the final result is True. True
2.4.3 Mixed and, or, not
# If and, or, not are mixed, there is a priority, but in daily development we do not need to remember the priority, we should use () to distinguish the priority, improve the readability of the program >>> (3>4 and 4>3) or ((1==3 and 'x' == 'x') or 3 >3) False
2.5 Member Operator
Note: Although the following two judgments can achieve the same effect, we recommend the second format because the not in semantics are more explicit
>>> not 'lili' in ['jack','tom','robin'] True >>> 'lili' not in ['jack','tom','robin'] True
2.6 Identity Operator
It should be emphasized that: ==The double sign compares whether the value s are equal, while is compares whether the IDs are equal.
#1. The ID is the same, the memory address must be the same, meaning that the type and value must be the same #2. value must be the same type, but id may be different, as follows >>> x='Info Tony:18' >>> y='Info Tony:18' >>> id(x),id(y) # The id of x and y is different, but they have the same value (4327422640, 4327422256) >>> x == y # The equal sign compares value True >>> type(x),type(y) # Value must be the same type (<class 'str'>, <class 'str'>) >>> x is y # is compares id s, where x and y are equal but IDS can be different False