python-02 (Introductory Basics 2 - Basic Common Grammar)
- 1. Logical judgment words
- 2. for statement
- 3. if statement
- 4. while statement
- 5. Other
1. Logical judgment words
1.1 Boolean type
1.1.1 When python is False
- Cases where the object is None and False,
- Examples are as follows:
- It should be noted that in Python, there is a constant "None", which represents a null value, not 0, nor an empty string.
- Examples are as follows:
- Empty strings and empty sequences or sets (ie: empty list[], empty dictionary{}, empty tuple()),as follows:
- Numeric type with value 0,as follows:
1.2 Logical judgment word not
- as follows:
2. for statement
2.1 Grammatical structure
- as follows:
for 变量 in 序列: 循环要执行的动作
2.2 Example
2.2.1 Example 1 - Loop iterating over strings
- as follows:
for item in "love": print(item) l o v e
strs = "love" for i in strs: print(i,end=' ') l o v e
2.2.2 Example 2 - Performing numerical loops
2.2.2.1 Simple loop (combined with range function)
-
Note that the for loop cannot iterate numeric types, int types, for example: 12345 belongs to a number, a whole, and counts as an element, as follows:
-
If you want to print numbers in a for loop, you need to use the range function, as follows:
for i in range(5): print (i)
for i in range(5): print (i,end = ' ')
sum = 0 for i in range(5): sum = sum +i print (sum)
2.2.2.2 Other uses of range
-
range(m,n)
In the form, as follows:for i in range(1,5): print(i,end = ' ')
-
range(m,n,i)
In the form, as follows:
2.2.2.3 range summary
- The range function can get an integer within a range
range(n)
: The default starts from 0, and the range of numbers is[0,n-1]
;range(m,n)
:Close left and open right(Including the left side but not the right side), that is, the range of numbers is[m,n-1]
- range(m,n,i) Integer from m to n, each i is accessed once, the range is
[m,n-1]
, wherei 为步长
.
2.2.3 Example 3 - Circular List
- as follows:
dogs = ['麦兜','泡泡','大牙','贝塔'] for dog in dogs: print(dog)
nums = [1,2,3,4,5] for num in nums: print(num)
2.2.4 Example 4 - Looping Tuples
- as follows:
d = ("I","love","you") for i in d: print(i,end = ' ')
2.2.5 Example 5 - Looping through the dictionary
- Writing method 1:
e = { '麦兜': 3,'泡泡': 5} for key in e: print(key,'===>',e[key]) 麦兜 ===> 3 泡泡 ===> 5
- Writing method 2:
e = { '麦兜': 3,'泡泡': 5} for k,v in e.items(): print("{0}===>{1}".format(k,v)) 麦兜===>3 泡泡===>5
- Writing method 3:
e = { '麦兜': 3,'泡泡': 5} for k,v in e.items(): print(k,':::::',v) 麦兜 ::::: 3 泡泡 ::::: 5
2.2.6 Example 6 - Looping through set
- as follows:
dogs = { '麦兜','泡泡','大牙'} print(type(dogs)) <class 'set'> for i in dogs: print(i,end = ',') 大牙,泡泡,麦兜,
3. if statement
3.1 Several common usages
- as follows:
if … else… if …elif…else… if not … if … not …
3.2 Example
3.2.1 if … else …
- Writing method 1 is as follows:
n = 5 if n > 3: print('n>3') else: print('n<=3')
- Writing method 2 is as follows:
print('n>3') if n >3 else print('n<3')
3.2.2 if … elif … else…
- as follows:
score = 86 if score >= 95: print('A+') elif score >= 90: print('A') elif score >= 85: print('B+') elif score >=80: print('B') elif score >= 70: print('C') elif score >= 60: print('D') else: print('E')
3.2.3 if not …
- as follows:
a = 3 b = 9 c = (a>b) if not c: print('a<=b') else: print('a>b')
3.2.4 if … is not …
- In this case, not is usually used together with is, and is not can be understood literally. Understood as if x (is not) ''.
a = '' if a is not '': print('a 非空') else: print('a 为空')
4. while statement
4.1 Grammar
- as follows:
orwhile 判断条件(condition): 执行语句(statements)……
while <expr>: <statement(s)> else: <additional_statement(s)>
4.2 Example
4.2.1 Example 1
- as follows:
n = 10 i = 1 sum = 0 while i <= n: sum = sum + i i += 1 print(sum)
4.2.2 Example 2
- as follows:
i = 1 n = 10 sum = 0 while i <= n: sum = sum + i i = i+1 else: print("求和结束,此时,sum = ",sum) 求和结束,此时,sum = 55
5. Other
5.1 The difference between import and from...import in python
5.1.1 Theoretical Differences
- import module: Import a module; Note: It is equivalent to importing a folder, which is a relative path.
- from...import: imported into a modulea function; Note: It is equivalent to importing a file in a folder, which is an absolute path.
5.1.2 Differences in usage
- import
模块.函数
, as follows:a = decimal.Decimal(3.456)
直接使用函数名
Just use from...import , as follows:c = Decimal(5.6)
5.1.3 Examples
-
as follows:
import decimal a = decimal.Decimal(3.456)
from decimal import Decimal a = Decimal(2.3)