Logical operators review
Connect multiple conditional operators ,and、or、not
problem : Whether an operation connecting multiple conditions returns a Boolean value or a data value ??
If all the conditions are data values , Then the data value is returned
print(0 and 2 and 5) # 5
print(0 or 1 or 3 or 5) # 1
If all the conditions are expressions , Then the Boolean value is returned
print(1 < 0 and True and 3 > 1) # False
print(1 < 0 or True or 3 > 1) # True
If the condition has both an expression and a data value , The returned result depends on the logical operator of the connection and still or
When encountering logical operators, they are and situations , From front to back, we should check whether the conditions are all true , The last condition is that the data value returns the data value , If it's an expression , Returns a Boolean value .
print(1 and 1 < 2) # True
print(1 and 3 and True and 56) # 56
When encountering logical operators, they are or situations , Look from front to back , When the first condition is true , Just go back to , If it is a data value, it returns the data value , If it is an expression, it returns a Boolean value
print(1 or 1 < 3 or True) # 1
print(3 > 1 or 1 < 2) # True
When multiple operators are mixed , Priority needs to be considered ,()>not>and>or, When a specific data value is encountered, the data value is returned , Encountered expression , Returns a Boolean value
print(1 and 3 > 2 or 5 or False and 3 == 3) # True
print(1 < 0 and 3 or 5 or False and 3 == 3) # 5
not The result of can only return Boolean values
The cycle structure of while Basic grammar
`while` Conditions
The sub code executed after the condition is established
while+break
`while` Conditions :
The sub code executed after the condition is established
break
break Can terminate a loop , If multiple conditional loops are nested ,break The cycle of the layer where he is terminated will not affect other cycles
while+continue
`while` Conditions :
The sub code executed after the condition is established
continue`
continue To skip this loop , Continue with next cycle
while+else
`while` Conditions :
The sub code executed after the condition is established
`else`:
Pre - defined code
else Is normally executed after the end of the loop body , Not an exception ,else And while At the same level
while Loop nesting of
`while` Conditions :
The sub code executed when the condition is true
`whlie` Conditions
The subcode after the condition is established
Loop nesting requires attention to the hierarchical relationship of the code , Indentation of code , Large circulation sets small circulation , You only need to follow the relationship of the same level of the row to execute downward in a sequential structure once , If you encounter while、if Making other judgments
Looping and global flag bits
Dead cycle : Loop body code cannot be a continuous computer task , Try not to use .
Global flag bits : In order to save break, Define a at the beginning True, The end is redefined as False
The cycle structure of for loop
for What a loop can do while Circulation can do it , It can traverse to any sequence of items , For example, a list of 、 Dictionaries 、 character string 、 Tuples 、 aggregate
Iterating through list indexes , One way to execute a loop is through an index , Applicable to list 、 Tuples and sets
fruits = ['banana', 'apple', 'mango']
for i in fruits:
print(' Current fruit : %s' % i)
"""
Current fruit : banana
Current fruit : apple
Current fruit : mango
"""
`for` Variable name `in` Iteratable object :
for Loop body code of loop
for The process of cycling , It will get the given data , Take each data value of this data from front to back and assign it to the previous variable i, First, take out the first data value and bind it to i Execute in the loop body , After the execution, he will return to for Loop and then take out the second data value again to assign to i, Then continue to execute the loop body code , among i Is a dynamically bound , Until all the data values in the data are taken , It will end automatically
Name of variable name : If the data to be named has a clear direction , Then we should do what we mean by seeing the name
Common names are :i、j、k、item
Dictionary value , stay for In circulation , By default, we can only get K value
dict = {'name': 'zhangran', 'age': 23, 'gender': 'women'}
for i in dict:
print(i)
"""
name
age
gender
"""
The value of the string ,for In circulation , The value of a string is a single character
str = 'asdfgh'
for i in str:
print(i)
"""
a
s
d
f
g
h
"""
Expanding knowledge
`for` Variable name in range( Numbers ):
Loop body code
When range Followed by only one number , from 0 Start to range value -1 End, e.g : Range value is 4 When , The result is 0,1,2,3,4
`for` Variable name in range( Numbers 1, Numbers 2):
Loop body code
When range Followed by two numbers , The first is the start and end position , The second is the end position , The data value generated at this time is characterized by ” Head and tail “, That is, the left contains and the right does not contain . for example :(1,6), The result is 1,2,3,4,5
`for` Variable name in range( Numbers 1, Numbers 2, Number of intervals ):
Loop body code
When range Followed by three numbers , The last number is the number of intervals between data ( It can also be understood as an arithmetic sequence ) for example :(1,10,2), The result is 1,3,5,7,9
3. range Method practical operation --- Network crawl out
Web crawler :python The code simulates a network request to get data
base_url = 'https://movie.douban.com/top250?start=%s&filter='
for i in range(0, 250, 25):
print(base_url % i)
"""
https://movie.douban.com/top250?start=0&filter=
https://movie.douban.com/top250?start=25&filter=
https://movie.douban.com/top250?start=50&filter=
https://movie.douban.com/top250?start=75&filter=
https://movie.douban.com/top250?start=100&filter=
https://movie.douban.com/top250?start=125&filter=
https://movie.douban.com/top250?start=150&filter=
https://movie.douban.com/top250?start=175&filter=
https://movie.douban.com/top250?start=200&filter=
https://movie.douban.com/top250?start=225&filter=
"""
for Use with other keywords
for + break
break Force the end of the layer for loop
for + continue
continue Skip this cycle and continue with the next cycle
for + else
else coordination for Use it together When for When the loop is not forced to end Run after the end of the loop body else Subcode