当前位置:网站首页>[foundation 3] - structure and function

[foundation 3] - structure and function

2022-07-23 06:16:00 terrific51

One 、 Selection structure

1. if……elif…………else

score =int (input(" Please enter the student's score :( branch )"))# Force type to int type 
if score>=90:
    print(" good ")
elif score>=80:
    print(" good ")
elif score>=60:
    print(" pass ")
else:
    print(" Bad ")

Two 、 Loop structure

1. for^^in

# Calculation 0--100 And 
sum=0
for x in range(101):#range(101) Can generate 0-100 Integer sequence of 
    sum=sum+x
print(sum)

range(101) Can generate 0-100 Integer sequence of

2.while

#  Calculation 100 The sum of all odd numbers in 
 sum=0
 n=99
 while n>0:
    sum=sum+n
    n=n-2
 print(sum)
  • In a loop statement continue Use
# Print 1-10 Odd numbers in the middle 
n=0
while n<10:
    n=n+1
    if n%2==0:
        continue#continue End the cycle ahead of time , And start the next cycle directly 
                #continue Statement will go straight to the next loop , Follow up print() Statement will not execute 
    print(n)

3、 ... and 、 function

1. Common built-in functions

abs(a): Find the absolute value
max(list)/min(list)
sum(list)
sorted(list): Sort
len(list): To obtain the length of the
divmod(a,b): Get quotient and remainder
pow(a,b): Get the power
round(a,b): Gets the number of decimal places specified .a For floating-point numbers ,b Represents the number to be retained . for example :round(3.1415926,2)>>>3.14
range(a,b): Generate a a To b Sequence , Left closed right away .range(1,10)>>>[1,2,3,4,5,6,7,8,9]
type (int/float)(^^): Forced conversion type

2. Custom function

stay Python in , Define a function to use def sentence , Write the function names in turn 、 Brackets 、 Parameters and colons in brackets :, then , Write function bodies in indented blocks , The return value of the function is return Statement returns .

# Custom function 
#1. Defined function 
def function1():
    print(" function function1 Carried out !")
    print("---")

3. Call function

Python Built in a lot of useful functions , We can call .
We can also call our own custom functions

#2. Call function 
function1()
# The function is called in the loop 
for x in range(101):
    function1()

4. Function definition and call

# Definition 
def getSum(a,b):# Shape parameter 
    result=a+b
    print(" The result of the addition is ",result)
# call 
getSum(4,5)# Actual parameters 
getSum(3,5)
# The number of arguments must be consistent with the formal parameters 

5. Return value

A function can return a calculation result , You can also return a function .
When returning a function , Keep in mind that this function does not execute , Do not refer to any variable that may change in the return function .

def fun1(lista):
    r=sum(lista)/len(lista)
    print(" Average ",r)
    return r

s=fun1([9,959,95,95,66,54,4584,58,54,848,64,4])# Receive return value 
print(s)
# Return multiple values 
def fun2():
    print("fun2 Carried out !")
    c=11
    d=22
    e=33
    return c,d,e

r1,r2,r3=fun2()
print(r1,r2,r3)

example

# example -- Juicer 
def juicer(f1):
    print(" The juicer starts to work ,,,")
    juice=f1+" juice "
    return juice

j=juicer(" Apple ")
print(" Squeeze out a cup :",j)
原网站

版权声明
本文为[terrific51]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221757281816.html