Python comparison operators
Python comparison operators | meaning |
---|---|
> | Left is greater than right |
>= | Left is greater than or equal to right |
< | Left is smaller than right |
<= | Left is less than or equal to right |
== | Left is equal to right |
!= | Left is not equal to right |
Conditional branch
Python's conditional branch syntax
if condition:
the operation performed if the condition is true (True)
else condition: the operation performed if the
condition is false (False)
While loop
The program should provide multiple opportunities for users to guess. Professionally, the program needs to run some code repeatedly
Python's while loop syntax
While condition:
The operation performed when the condition is true (True).
This is because the loop is being executed all the time, so the program keeps popping out "buddy, it's big!"
In this way, the loop will end only when the user enters the correct answer, and we hope to have a limit on the number of times. For example, when the user guesses
incorrectly three times in a row, the program will end. The example above is the error, which will be repeated 4 times. It should start at the beginning Just judge, the correct is as follows: at the beginning time<3 execute the loop, because the last time+1, that has reached three loops
Supplement In Python, only the following content will be considered false (copy’s new blogger Diandian love Fengfeng)
[1] (Note that there is nothing in the colon or brackets, not even spaces!): False None 0 “”'' () [] {}
[2] False in the colon is directly recognized as false
[3] 0 and None will be directly regarded as false
and/or logical operator
When the number of guesses above is limited to no more than three times, the and logical operator is
used. The and logical operator can be used to connect any expressions together and get
the priority of a Boolean type (True/Flase) value comparison operator It is higher than
the content in the logical operator (),
and the logical operator is executed first . Only when the left and right sides are True at the same time, the result is True
or the logical operator has one on the left and the right is True, and the result is True
Introduce foreign aid
Since the standard answer of the above program is fixed, then A will tell B if it knows it, then this game is meaningless. At this time, foreign aid needs to be introduced to make the answer generated every time the program is run.
random module
What is a module? The program we write each time is actually an independent module. Python provides many modules that can be imported into the
random module. There is a function called randint(a,b) . Ta will return an ab random integer.
First, import it at the top of the program. Foreign aid, use import
Task
1. How many times will the following code print "I love pdd"?
while 'C':
print('我爱pdd')
A: Countless times, because the loop condition CTRL + C (forced end) is not limited
, the reason for the infinite loop is that the condition after the while is always true (True). From the perspective of Python, only the following content will be regarded as false
while '':
print('进入循环')
print('退出循环')
while False:
print('进入循环')
print('退出循环')
while 0:
print('进入循环')
print('退出循环')
This explains why the while loop can be used directly like this: not executed when i=i-1 is zero
while i:
2. How many times will the following code print "I love pdd"?
i = 10
while i:
print('我爱pdd')
i = i - 1
A: It will be printed 10 times
3. Please write an expression equivalent to 10 <cost <50
A: cost>10 and cost<50
4. In Python3, can multiple statements be written in one line?
Yes, you can use semicolons to separate statements , such as >>> print('I love fishc');print('very much!')
5. In Python3, can a statement be written in multiple lines?
Yes, a long statement can be broken down into several lines using backslashes or parentheses
>>> 3 > 4 and \
1 < 2
>>> ( 3 > 4 and
1 < 2 )
6. What is the difference between Python's and operator and C language && operator? [This question is aimed at friends with C or C++ foundation ]
7. Have you heard of "short-circuit logic"?
Logical operators have an interesting feature: they do not perform operations when they do not need to be evaluated. It may be more "advanced" to say that, for example, the expressions x and y require both variables x and y to be true at the same time, the result is true. Therefore, if the x variable is known to be False, the expression will immediately return False, regardless of the value of the y variable.
This behavior is called short-circuit logic or lazy evaluation . This behavior is also applied to the or operator.
In fact, Python’s approach is that if x is false, the expression will return The value of x (0), otherwise it will return the value of y.
There are still doubts about the and logical connector. For example, 1 and 3 output 3 because of comparison?
True=1, False=0
8. Try to achieve The following screenshot function
Error example:
Analysis: Strings and numbers cannot be compared; the understanding of the while loop is wrong, the code improved by myself:
ask = input('请输入一个整数:')
asks = int(ask)
i = 1
i = int(i)
while i <= asks:
print(i)
i = i + 1
i = int(i)
Fish c code:
temp = input('请输入一个整数:')
number = int(temp)
i = 1
while number:
print(i)
i = i + 1
number = number - 1
9. Use the code to achieve the following screenshot function
My code: (it is wrong, because there is no indentation)
temp = input('请输入一个整数:')
ask = int(temp)
i = 1
i = int(i)
while i <= ask:
out = "*" * ask
print(out)
ask = ask - 1
After improving my own code (adding spaces): it is still wrong, because the number of spaces before * is one less than the number of ✳
temp = input('请输入一个整数:')
ask = int(temp)
i = 1
i = int(i)
while i <= ask:
out = "*" * ask
blank = " " * ask
print(blank + out)
ask = ask - 1
Improved code again:
temp = input('请输入一个整数:')
ask = int(temp)
i = 1
i = int(i)
while i <= ask:
num = ask - 1
out = "*" * ask
blank = " " * num
print(blank + out)
ask = ask - 1
Fish c code:
temp = input('请输入一个整数:')
number = int(temp)
while number:
i = number - 1
while i:
print(' ', end = '')
i = i - 1
j = number
while j:
print('*', end = '')
j = j - 1
print()
number = number - 1
Question code:
print(' ', end = '')
print()