当前位置:网站首页>[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
continueUse
# 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.14range(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)
边栏推荐
- 编码器-解码器(seq2seq)
- 【基础3】——结构与函数
- Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util
- Chapter6 卷积神经网络(CNN)
- Chapter7 recurrent neural network-1
- 【基础8】——进程和线程
- Ia note 1
- 递归级联网络:基于无监督学习的医学图像配准
- Remember a way to connect raspberry pie wirelessly without a display screen and can't find IP
- 视频直播源码,重置当前密码的相关改动
猜你喜欢

C language knowledge points (pointer knowledge type)

初学者备战蓝桥杯历程(大学编程学习历程记录,题目思路献给需要备考蓝桥杯的同学)

DB207-ASEMI整流桥一般用在什么地方,DB207参数尺寸

PWN stack overflow basic exercise - 2

Stack overflow basic exercise - 5 (string vulnerability)

hcip--复习第二天作业

第一个PWN 栈溢出简单题

星策社区发起人谭中意:用开源方式推进企业智能化转型

Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util

Stack overflow basic exercise question - 3 (with a comparison of 32 and 64 bit differences)
随机推荐
Buuctf miscellaneous - QR code
win11任务管理器怎么打开?win11任务管理器打开的技巧方法
Enter two strings STR1 and STR2, and count the number of times that the string STR2 appears in STR1.
栈溢出基础练习题——6(字符串漏洞64位下)
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
2019_ ACL_ Multimodal Transformer for Unaligned Multimodal Language Sequences
2019_AAAI_Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
PWN --- ret2shellcode
The simplest scull device driver
codeforce:D2. Remove the Substring (hard version)【贪心的字符串 + 子序列】
Chapter7 recurrent neural network-2
C语言知识点(指针知识类型)
Ropgadget -- ret2syscall
Understanding the application steps of machine learning development
Pytoch realizes text emotion analysis
2020_ ACM MM_ MISA: Modality-Invariant and -Specific Representations for Multimodal Sentiment Analysis
Using "hifolw" to quickly create the information generation of College Students' return list
激活函数(sigmoid、tanh、ReLU、softmax)
Stack overflow basic exercise - 6 (string vulnerability under 64 bits)
1.从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A;80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;sco