Python Basic Grammar Learning (2)

6 if judgment

6.1 Basic format of if statement

if judgment condition:
    The action to execute when the condition is true
else:
    Action to execute when the condition is not true

6.2 Precautions for if statement

  1. Judgment condition result must be Boolean type
  2. You need to add ":" after the judgment condition
  3. Use 4 spaces as the hierarchical relationship under the if statement

6.3 Use of elif statement

if Judgment condition 1:
    Action to execute when condition 1 is true
elif judgment condition 2:
    Action to execute when condition 2 is true
else:
    Actions to execute when conditions 1 and 2 are false

6.4 Nesting of judgment statements

if Judgment condition 1:
    if Judgment condition 2:
        Execute action when condition 1 and condition 2 are true
    else:
        The execution action when condition 1 is satisfied and condition 2 is not satisfied
else:
    Action to be executed when condition 1 is not true

7 loop statement

7.1 while loop

age = 10
while age <= 18:
    print(f'my age is {age}')
    age += 2

 7.2 for loop

 forLoops are used to iterate over sequences (i.e. lists, tuples, dictionaries, sets or strings)

for temporary variables in pending datasets:
    loop action
# loop range type
for i in range(3):
    print(i)

# loop string type
for i in 'python':
    print(i)

 

# loop list type
for i in ['hello','python']:
    print(i)

# Loop through zet types
for i in {'hello','python'}:
    print(i)

 

7.3 break和continue

        The functions of break and continue are the same as those of other languages. Break directly interrupts and does not continue to execute the subsequent loop. Continue only exits the current loop, and the subsequent loop can still continue to execute.

8 Python functions

8.1 Definition of functions

Functions are defined using the keyword def.

def my_func(x,y):
    """
    function description
    :param x:
    :for me:
    :return:
    """
    return x + y

The function supports multiple return values, which are separated by commas, and two variables are also separated by commas when receiving.

def my_func(x, y):
    """
    function description
    :param x:
    :for me:
    :return:
    """
    sum = x + y
    sub = x - y
    return sum, sub
re_sum, re_sub = my_func(10, 6)
print(re_sum, re_sub)

8.2 List common functions

The List list is actually an array, and its definition and operation are consistent with the array.

index() returns the first index of the specified value

append() appends elements to the end of the list

clear() clears all elements of the list

copy() copies the list

count() counts the number of list elements

extend() splices a list to the end of this list

insert() adds an element at the specified position

pop() removes the element at the specified position

remove() removes the first value with the specified value

sort() sorts the values ​​of a list

reverse() reverses the order of the list

8.3 lambda anonymous function

lambda parameter 1, parameter 2: function body

  • Use lambdas as keywords
  • Parameters can be multiple
  • The function body can only be one line

Guess you like

Origin blog.csdn.net/weixin_64940494/article/details/126365835