Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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.