当前位置: 首页 > wzjs >正文

手机界面设计网站海外网红营销平台

手机界面设计网站,海外网红营销平台,江苏建设造价信息网站,wordpress数据库连接一、函数基础 函数是什么? 想象你每天都要重复做同一件事,比如泡咖啡。函数就像你写好的泡咖啡步骤说明书,每次需要时直接按步骤执行,不用重新想流程。 # 定义泡咖啡的函数 def make_coffee(sugar1): # 默认加1勺糖 print("…

一、函数基础

  1. 函数是什么? 想象你每天都要重复做同一件事,比如泡咖啡。函数就像你写好的泡咖啡步骤说明书,每次需要时直接按步骤执行,不用重新想流程。

 

# 定义泡咖啡的函数 def make_coffee(sugar=1): # 默认加1勺糖 print("烧水...") print("放入咖啡粉...") print(f"加{sugar}勺糖") return "一杯咖啡做好了☕" # 调用函数 my_coffee = make_coffee(2) print(my_coffee)

  1. 函数的参数与返回值

  • 参数类型

  • 必须参数:调用时必须传递(如make_coffee(2)里的2)

  • 默认参数:不传值时使用默认值(如sugar=1)

  • 可变参数:接收任意数量参数(*args用于元组,**kwargs用于字典)

 
 

def student_info(name, age, *hobbies, **scores): print(f"姓名:{name}, 年龄:{age}") print("爱好:", hobbies) print("成绩:", scores) student_info("小明", 18, "篮球", "编程", 数学=90, 英语=85)

  • 返回值

  • 用return返回结果,可返回多个值(实际是元组)

  • 无return时函数返回None

 
 

def calculator(a, b): add = a + b subtract = a - b return add, subtract result = calculator(10, 5) print(result) # 输出 (15, 5)

  1. 变量的作用域

  • 局部变量:函数内部定义的变量(如函数内的add)

  • 全局变量:函数外部定义的变量(需用global关键字修改)

 
 

count = 0 # 全局变量 def increment(): global count count += 1 print(f"当前计数:{count}") increment() # 输出 1

互动问题:如果去掉global count会报错吗?为什么?


二、常用数据结构

  1. 列表(List)

  • 特点:可修改、有序、元素可重复

  • 常用操作:增删改查

 
 

shopping_list = ["苹果", "牛奶"] shopping_list.append("面包") # 添加元素 shopping_list[1] = "酸奶" # 修改元素 shopping_list.pop() # 删除最后一个元素 print(shopping_list) # 输出 ['苹果', '酸奶']

  1. 字典(Dictionary)

  • 特点:键值对结构,键不可重复

  • 应用场景:存储用户信息、配置参数

 
 

user = { "name": "李华", "age": 25, "is_vip": True } print(user["name"]) # 输出 李华 user["email"] = "lihua@example.com" # 添加新键值对

  1. 元组(Tuple)与集合(Set)

  • 元组:不可修改的列表,用圆括号定义

  • 集合:自动去重,支持交集、并集操作

 
 

# 元组示例 colors = ("红色", "蓝色", "绿色") print(colors[0]) # 输出 红色 # 集合示例 fruit_set = {"苹果", "香蕉", "苹果", "橙子"} print(fruit_set) # 输出 {'苹果', '香蕉', '橙子'}


三、综合案例:学生成绩管理系统

 
 

students = [] def add_student(name, score): students.append({"name": name, "score": score}) def show_ranking(): sorted_students = sorted(students, key=lambda x: x["score"], reverse=True) for student in sorted_students: print(f"{student['name']}: {student['score']}分") # 添加学生 add_student("张三", 85) add_student("李四", 92) add_student("王五", 78) # 显示排名 show_ranking()

输出结果:

 
 

李四: 92分 张三: 85分 王五: 78分

思考题:如何修改代码实现按姓名排序?


文章转载自:

http://M4i1TLRa.wqpsf.cn
http://WNMFLajx.wqpsf.cn
http://2qJdgnMg.wqpsf.cn
http://w0hJhGU6.wqpsf.cn
http://BkQxciDQ.wqpsf.cn
http://csFehp9N.wqpsf.cn
http://rJSjXlJ7.wqpsf.cn
http://5nbCa520.wqpsf.cn
http://LQf5kxK9.wqpsf.cn
http://aAobsQhH.wqpsf.cn
http://1eW41iaI.wqpsf.cn
http://J2mUx322.wqpsf.cn
http://R5r5fZjB.wqpsf.cn
http://gPsrRXku.wqpsf.cn
http://ALqrFqra.wqpsf.cn
http://PAKybk8z.wqpsf.cn
http://iz0pnFaM.wqpsf.cn
http://3j6foIlK.wqpsf.cn
http://YHxoQkGq.wqpsf.cn
http://6YMp3fam.wqpsf.cn
http://WtK6yKP3.wqpsf.cn
http://qFKYI0zA.wqpsf.cn
http://hKzgLtbg.wqpsf.cn
http://JLNSUQ8y.wqpsf.cn
http://GaKAJqrQ.wqpsf.cn
http://WnmAR9ro.wqpsf.cn
http://yCxm41yG.wqpsf.cn
http://cYjPLpDi.wqpsf.cn
http://gUk4zhbI.wqpsf.cn
http://Ku8hMnUg.wqpsf.cn
http://www.dtcms.com/wzjs/743576.html

相关文章:

  • 搜不到wordpress 网站百度产品推广怎么收费
  • 欧洲站vat激活网络推广 推广
  • 常州自助做网站建筑设计网址大全
  • 网站页面设计图片素材眼镜企业网站建设方案
  • 网站 云建站中国互联网数据平台
  • 如何做网站知乎火锅料网站方案怎么做
  • 网站建设发展状况江西省上饶市网站建设公司
  • 建设银行荆门招聘网站wordpress公共聊天室
  • 行业外贸网站建设宁德市城乡建设网站
  • 美食攻略网站建设课程设计梧州网站设计公司
  • 网络考试seo教程优化
  • 肥城住房和城乡建设局网站威海做网站
  • 定制网站大概多少钱浙江省建设监理协会官方网站
  • 谷歌账号注册网站打不开龙岗网站建设要多少钱
  • 哪些网站可以做签约设计师网站开发公司资讯
  • 沈阳网站建设技术支持自己有网站怎么做优化
  • 网站建设学习课程上海网站建设网站制作
  • 用什么网站做头像抓好门户网站建设
  • 网站平台建设的流程网站栏目名称大全
  • 电子商务网站建设的策划书磁力猫torrentkitty官网
  • 网站建设公司ttmwl安卓开发工具手机版
  • 淄博网站建设开发网站风格设计的选择
  • 公司网站平台建设wordpress nginx 301
  • c s网站开发模式wordpress 用户访问记录
  • 新泰营销型网站建设网站建设的商业阶段
  • 广州 海珠 建网站网站降权该怎么做
  • 天津网站建设美丽忻州做网站公司
  • 深圳网站建公司电子商务网站规划的原则
  • 支付网站搭建原创网站设计
  • 保险做的好的网站企业服饰网站模板