前言 在上一节的学习中.已经介绍了几种基本类型.包括字符串的定义,以及字符串中索引.切片.字符串拼接的使用方法.以及基本的整形数据运算.一些之前都没有了解过的运算符.比如 ** 乘方 //整数除法等.这一节,我们将学习最基本的流程控制工具,比如 if 条件判断 for 循环 while 循环 if-elif-else 判断 num = int(input("输入一个数字 :")) if num > 10: print("input > 10") elif
Python学习 Part2:深入Python函数定义 在Python中,可以定义包含若干参数的函数,这里有几种可用的形式,也可以混合使用: 1. 默认参数 最常用的一种形式是为一个或多个参数指定默认值. >>> def ask_ok(prompt,retries=4,complaint='Yes or no Please!'): while True: ok=input(prompt) if ok in ('y','ye','yes'): return True if ok in ('
我们可以创建一个函数来列出费氏数列 >>> def fib(n): # write Fibonacci series up to n ... """Print a Fibonacci series up to n.""" ... a, b = 0, 1 ... while a < n: ... print(a, end=' ') ... a, b = b, a+b ... print() ... >>>