当前位置:网站首页>[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)
边栏推荐
猜你喜欢

Chapter7 循环神经网络-1

Input a string of characters from the keyboard, output different characters and the number of times each character appears. (the output is not in order) use the common methods of string class to solve

C语言知识点(指针知识类型)

2020_ ACM MM_ MISA: Modality-Invariant and -Specific Representations for Multimodal Sentiment Analysis

2019_AAAI_ICCN

Stack overflow basic exercise question - 3 (with a comparison of 32 and 64 bit differences)

攻防世界 —— hacknote

2019_ IJCAI_ Adapting BERT for Target-Oriented Multimodal Sentiment Classification

BUUCTF 杂项——二维码

中国科学院院士王怀民:推进中国开源创新联合体的思考与实践
随机推荐
1.有一个分数序列:2/1,3/2,5/3,8/5,13/8,……编程求这个序列的前20项之和。
30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要
视频直播系统源码,保存平台视频内容到本地
Configure IP address
BUUCTF 杂项——二维码
Dedecms V5.7.97 contain an XSS vulnerability
线性回归与逻辑回归及其实现
Reset root password
hcia--nat实验
中国工程院院士倪光南:拥抱开源 与世界协同创新
pwn栈溢出基础练习题——1
Elementary analysis of graph convolution neural network (GCN)
js中的深拷贝浅拷贝
mysql数据库基本知识
2019_IJCAI_Adapting BERT for Target-Oriented Multimodal Sentiment Classification
pwn栈溢出基础练习题——2
更新C语言笔记
Chapter6 convolutional neural network (CNN)
2019_ AAAI_ Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
栈溢出基础练习题——5(字符串漏洞)