Yesterday we learned about the output rules of some Python characters, the naming rules of variables, etc...
And today we're going to deepen a little bit. Learn about Python's basic syntax.
Before we do that, let's just say, two categories of variables in Python
(1. Local variables)
What is a local variable?
A: In part of the code is valid, the popular point is that in a certain range of valid variables.
(2. Global variables)
What is a global variable?
A: Global variables are valid in all code.
Then say the or in Python: or, and: and.
1.or: Or
?
1
Question 1: How to tell if a number is a multiple of 5 or a multiple of 6
?
1
2
3
4
5
6
7
8
9
10
If num% 5 = = 0:
If num% 6 = = 0:
Print (' 5 is also a multiple of 6 ')
Else
Print (' is a multiple of 5 is not a multiple of 6 ')
else:
If num% 6 = = 0:
Print (' Only multiples of 6 ')
else:
Print (' Neither 5 nor multiples of 6 ')
?
1
2
If num% 5 = = 0 or num% 6 ==0:
Print (multiples of ' 5 or 6 ')
First of all, we look at the two ends of the code, is not the first more, and it is relatively simple, this is the role of OR
When we have a lot of conditions and we just need to satisfy one condition, we can output a result, which is the function of OR.
?
1
2
<strong> Question 2: How to tell if a number is both a multiple of 3 and a multiple of 4
</strong>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
num = 123122
If num% 12 = = 0:
Print (' Multiples of 3 is also a multiple of 4 ')
else:
Print (' Not multiples of 3 and 4 ')
If num% 3 = = 0:
If num%4 = = 0:
Print (' is a multiple of 3 and 4 ')
Else
Print (' Not a multiple of 4 ')
else:
Print (' Not a multiple of 3 ')
?
1
2
If num% 3 = = 0 and num% 4 ==0:
Print (' is a multiple of 3 and 4 ')
And we are also used to segment code as a demonstration, the first one is not joined and, the second is joined and.
We need to use and when we have multiple conditions that need to be judged, and when multiple conditions are required.
Here I make a list to make it easy for you to understand OR and
1.and
True Result: true
True and False results: false
False false Result: false
(1) If two conditions are true, the result is true
(2) If one of the conditions is true and a condition is false, the result is false
(3) If two conditions are false, the result is false
2.or
True Result: true
True and false results: real
False false Result: false
(1) If two conditions are true, the result is true
(2) If one of the conditions is true and a condition is false, the result is true
(3) If two conditions are false, the result is false
Basic syntax Learning for Python