Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Ann011414
Python-practice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
Python-practice
/
4_python_function.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
55 lines (46 loc) · 1.93 KB
main
Breadcrumbs
Python-practice
/
4_python_function.py
Copy path
Top
File metadata and controls
Code
Blame
55 lines (46 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#如何定义自己的函数
def calculate_sector_1(): #定义函数的时候,代码不会被执行;只有调用函数的时候才会被执行
central_angle_1=160
radius_1=30
sector_area_1=central_angle_1/360*3.1415926*radius_1**2
print(f"此扇形面积为:{sector_area_1}")
calculate_sector_1()
#通过参数来让函数变的更通用
def calculate_sector(central_angle,radius): #这里是参数
sector_area=central_angle/360*3.1415926*radius**2
print(f"此扇形面积为:{sector_area}")
calculate_sector(160,30)
calculate_sector(60,15)
calculate_sector(30,16)
#如何把函数计算出来的结果拿出来使用
def calculate_sector(central_angle,radius): #这里是参数
sector_area=central_angle/360*3.1415926*radius**2
return sector_area
sector_area_1=calculate_sector(160,30)
print(sector_area_1)
#我写的一个计算BMI的函数
def BMI_caculate(weight,height):
BMI=weight/(height**2)
fenlei=""
if BMI<=18.5:
fenlei="偏瘦"
elif BMI<=25:
fenlei="正常"
elif BMI<=30:
fenlei="偏胖"
else:
fenlei="肥胖"
print(f"您的BMI分类为:{fenlei}")
return BMI
weight=float(input("请输入您的体重(kg):"))
height=float(input("请输入您的身高(m):"))
BMI=BMI_caculate(weight,height)
print("您的BMI为",BMI)
#python官方内置函数 https://docs.python.org/zh-cn/3/library/functions.html
#比如 statistics函数可以求中位数等统计相关是函数
#引入模块是方法
import statistics
from statistics import mean,median #这样使用函数的时候不用再带上某块名字
from statistics import* #这样直接全部引入模块中的函数且在使用的时候不用再带上模块名(但是不推荐,会引入很多用不到的,可能会产生命名冲突)
#还可以引入第三方的模块
# pypi.org 这个网站可以对第三方库进行搜索
You can’t perform that action at this time.