Python入门学习笔记————04(while循环,函数)

while 循环

  • 一个循环语句
  • 某条件成立时循环
  • 不确定具体的循环次数,但能够知道就具体的循环条件就用while
  • 语法
          while 条件表达式:
              语句块
    
        while ... ellse...
        whhile 条件表达式:
            语句块1
        else:
            语句块2  


        (与for循环中的else基本一致)

In [3]:

#年利率6.6% ,存十万元,多少年到二十万
benjin = 100000
year = 0
while benjin < 200000 :
    benjin = benjin * (1+0.066)
    year += 1 # year = year + 1
    print('第{0}年拿到{1}元'.format(year,benjin))
第1年拿到106600.0元
第2年拿到113635.6元
第3年拿到121135.54960000001元
第4年拿到129130.49587360003元
第5年拿到137653.10860125764元
第6年拿到146738.21376894065元
第7年拿到156422.93587769073元
第8年拿到166746.84964561832元
第9年拿到177752.14172222913元
第10年拿到189483.78307589627元
第11年拿到201989.71275890543元

# 函数

- 代码的组织形式
- 一般一个代码自完成一项功能
- 函数的使用
    - 函数的定义
        def 函数名(可有参数也可没有参数):
            函数内容
    - 函数的调用
        直接调用函数名(有参数写参数,没参数不写)

In [8]:

#函数的定义
# 定义一个函数
#只定义不执行
#注意函数的命名准则,一般不使用大驼峰
#注意函数的缩进
def func():
    print('hello')
    print('how are you?')
    print('bye')

In [7]:

 
c
hello
how are you?
bye

In [10]:

# 注意缩进
def func():
    print('hello')
    print('how are you?')
print('bye')
bye

In [11]:

#函数的引用
func()
hello
how are you?
 
### 函数的参数和返回值
- 负责给函数传递一些必要的数据或信息
    - 形参(形式参数):在函数定义时用到,没有实际值,只是占位
    - 实参(实际参数):在函数调用时输入的值
- 返回值:函数的执行结果
    - 用return关键字
    - 如果没有return,则默认返回None
    - 一旦出现return则无条件法回,结束函数

In [16]:

#参数的使用,return的使用
def hello(person):
    print ('你好啊{0}'.format(person))
    print ('你在干嘛呢')
    return '{0},你不理我,我就走了啊!{0}'.format(person)    #函数执行完成以后的结果
p = '狗蛋'
str=hello(p)    #调用时用p 代表person
print(str)     #打印函数执行结果
你好啊狗蛋
你在干嘛呢
狗蛋,你不理我,我就走了啊!狗蛋

In [19]:

#return实例
def hello(person):
    print ('你好啊{0}'.format(person))
    return '我结束了'   #出现return,函数执行结束
    print ('你在干嘛呢')
    return '{0},你不理我,我就走了啊!{0}'.format(person)
p = '狗蛋'
str=hello(p)
print(str)    
你好啊狗蛋
我结束了

In [21]:

#帮助命令的使用
#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 [24]:

#九九乘法表   version1.0
for row in range(1,10):
    for col in range(1,row+1):
        print (row*col,end=' ')   #print函数会自动进行换行
    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 [26]:

##九九乘法表   version2.0
def printline(row):
    for col in range(1,row+1):
         print (row*col,end=' ')   #print函数会自动进行换行
    print("")
 
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 

参考详情

(函数定义时用形参,调用时为实参)

  • [参考资料](https://www.cnblogs.com/bingabcd/p/6671368.html)
  • python参考资料:headfirst Python-->零基础入门学习Python(小甲鱼)
  • 参数分类

    • 普通参数
    • 默认参数
    • 关键字参数
    • 收集参数
    • 普通参数

      • 定义时直接定义变量名
      • 调用时直接把变量或者值放入指定位置

        def 函数名(参数1,参数2,参数3,...)

           函数体
        

        #调用 函数名(val1,val2,val3,...) 实参与形参按位置一一对应

    • 默认参数

      • 形参带有默认值
      • 调用时如果没有赋值则直接使用默认值

        def func_name(p1=vl1,p2=vl2,p3=vl3,...)

          func_block
        
          调用1
          func_name()
        
          调用2
          val1=100
          vavl2=200
          func_name(val1,val2)
        

In [37]:

#默认参数例子
def red (name,age,sex='man'):
    if sex == 'man':
        print('{0} ,he is a good student'.format(name))
    else:
        print('{0} ,she is a good student'.format(name))

In [41]:

#调用
red ('xiaoming',21)
red ('lingling',20,'woman')
xiaoming ,he is a good student
lingling ,she is a good student

猜你喜欢

转载自blog.csdn.net/weixin_42492218/article/details/84863710