Python基础04_while语句_函数

本文深入解析了Python中的while循环语句和函数的使用方法,包括while循环的语法、应用场景,以及函数的定义、调用、参数传递和返回值的概念。通过实例演示了如何运用这些知识来解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

while循环

  • 一个循环语句
  • 表示当某条件成立的时候,就循环
  • 不知道具体循环次数,但能确定循环的成立条件的时候用while循环
  • while语法:
      while 条件表达式:
          语句块

    #另外一种表达方法
      while  条件表达式:
          语句块1
      else:
          语句块2

函数

  • 代码的一种组织形式
  • 一个函数一般完成一项特定的功能
  • 函数使用
  • 函数需要先定义
  • 使用函数,俗称调用

# 定义一个函数
# 只是定义的话不会执行
# 1. def关键字,后跟一个空格
# 2. 函数名,自己定义,起名需要遵循便令命名规则,约定俗成,大驼峰命名只给类用
# 3. 后面括号和冒号不能省,括号内可以有参数
# 4. 函数内所有代码缩进

def func():
   
print("我是一个函数")
   
print("我要完成一定功能")
print("我结束了")

 

# 函数的调用

# 直接函数名后面跟括号

 func()

 

函数的参数和返回值

  • 参数: 负责给函数传递一些必要的数据或者信息
  • 形参(形式参数): 在函数定义的时候用到的参数没有具体值,只是一个占位的符号,成为形参
  • 实参(实际参数): 在调用函数的时候输入的值
  • 返回值: 函数的执行结果
  • 使用return关键字
  • 如果没有return,默认返回一个None
  • 函数一旦执行return语句,则无条件返回,即结束函数的执行

# 参数的定义和使用
# 参数person只是一个符号,代表的是调用的时候的某一个数据
# 调用的时候,会用p的值代替函数中所有的person

def hello(person):
   
print("{0}, 你肿么咧".format(person))
   
print("Sir, 你不理额额就走咧")
 
p
= "明月"
hello(p)

明月, 你肿么咧
Sir, 你不理额额就走咧

In [11]:

# return语句的基本使用
# 函数打完招呼后返回一句话
def hello(person):
   
print("{0}, 你肿么咧".format(person))
   
print("Sir, 你不理额额就走咧")
   
   
return "我已经跟{0}打招呼了,{1}不理我".format(person, person)
 
p
= "明月"
rst = hello(p)

print(rst)

明月, 你肿么咧
Sir, 你不理额额就走咧
我已经跟明月打招呼了,明月不理我

In [16]:

# return案例2

def hello(person):
   
print("{0}, 你肿么咧".format(person))
   
return "哈哈,我提前结束了"
    print("Sir, 你不理额额就走咧")
   
return "我已经跟{0}打招呼了,{1}不理我".format(person, person)

p = "LiYing"
rst = hello(p)
print(rst)

LiYing, 你肿么咧
哈哈,我提前结束了

In [17]:

# 查找函数帮助文档
# 1. 用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.

In [ ]:

 

In [19]:

# 九九乘法表
# version 1.0
for row in range(1,10):
   
# 打印一行
    for col in range(1, row+1):
       
# print函数默认任务打印完毕后换行
        print( row * col, end=" ")
   
print("-------------------")

1 -------------------
2 4 -------------------
3 6 9 -------------------
4 8 12 16 -------------------
5 10 15 20 25 -------------------
6 12 18 24 30 36 -------------------
7 14 21 28 35 42 49 -------------------
8 16 24 32 40 48 56 64 -------------------
9 18 27 36 45 54 63 72 81 -------------------

In [22]:

# 定义一个函数,打印一行九九乘法表
def printLine(row):
   
for col in range(1, row+1):
       
# print函数默认任务打印完毕后换行
        print( row * col, end=" ")
   
print("")
 

# 九九乘法表
# version 2.0
for row in range(1,10):
    printLine(row)

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值