python第2课:函数、字符串

本篇博客详细讲解了Python中的函数,包括参数、返回值以及如何使用help获取官方文档。接着深入探讨了字符串str,包括转义字符、字符串格式化方法(传统与format)以及内置函数的应用,如find、index、islower、isalpha等。
摘要由CSDN通过智能技术生成

本节课内容

  • 函数+参数
  • 字符串str + 字符串内置函数

- 函数+参数

# 函数定义
def func():
    print("hehehe")
    print("hahaha")

func()
hehehe
hahaha

函数的参数和返回值

  • 参数:负责给函数传递一些必要的数据或者信息
    • 形参(形式参数):在函数定义时使用的参数,没有具体值,只是个占位符
    • 实参(实际参数):再调用函数时输入值
  • 返回值:调用函数时的一个执行结果
    • 使用return返回结果
    • 如果没有值需要返回,推荐用return None表示函数结束
    • 函数一旦执行return,则函数立即停止
    • 如果函数没有return关键字,则函数默认返回None
# 形参和实参的案例
# 参数person只是一个符号
# 调用的时候用另一个
def hello(person):
    print("{},你好吗?".format(person))
    print(f"{person},我好!")

p = "小明"
# 调用函数
hello(p)
小明,你好吗?
小明,我好!
p = "小花"
hello(p)
小花,你好吗?
小花,我好!
pp = hello("婷婷")
print(pp)
婷婷,你好吗?
婷婷,我好!
None

如果需要帮助,可以使用help调出官方文档

# help为你提供帮助
help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
# 九九乘法表
# range为左闭右开区间
# version 1.0
for i in range(1,10):
    for j in range(1,i+1):
        print("{}*{}={:2}".format(j,i,i*j),end=", ") # 以逗号和一个空格结尾,print()默认换行输出,可见官方文档end="\n"为换行
    print("")
# 占位符:输出时不够两位则会在前面加空格实现两位占位“:2”中2为格式占位符,表示占两格
1*1= 1, 
1*2= 2, 2*2= 4, 
1*3= 3, 2*3= 6, 3*3= 9, 
1*4= 4, 2*4= 8, 3*4=12, 4*4=16, 
1*5= 5, 2*5=10, 3*5=15, 4*5=20, 5*5=25, 
1*6= 6, 2*6=12, 3*6=18, 4*6=24, 5*6=30, 6*6=36, 
1*7= 7, 2*7=14, 3*7=21, 4*7=28, 5*7=35, 6*7=42, 7*7=49, 
1*8= 8, 2*8=16, 3*8=24, 4*8=32, 5*8=40, 6*8=48, 7*8=56, 8*8=64, 
1*9= 9, 2*9=18, 3*9=27, 4*9=36, 5*9=45, 6*9=54, 7*9=63, 8*9=72, 9*9=81, 
def jiujiu():
    for i in range(1,10):
        for j in range(1,i+1):
            print("{}*{}={:2}".format(j,i,i*j),end=", ") 
        print("")

jiujiu()
jiujiu()
1*1= 1, 
1*2= 2, 2*2= 4, 
1*3= 3,
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值