1.python里面的Len()、Max()、min()函数,对于list,tuple和string都是适用的。其中需要注意的是,len()函数在获取序列的长度的时候也会将空字符的个数算进去。例如:
>>>Temp = "python " *Temp字符串里面有三个空字符。
>>>len(Temp) *大家猜猜这里返回的是6还是9?
这里的打印结果是:9
2.if和else、elif语句使用时要注意以下两点:
1、else、elif为子块,不能独立使用
2、一个if语句中可以包含多个elif语句,但结尾只能有一个else语句
3.python里面的input()函数的注意点:
int(temp) = input("input the number: ")
上面那句代码其实是错误的。很明显这样做法的意图是输入一个key后,将input()返回的字符串利用int方法变回整形数,有点一石二鸟的味道。正确的做法应该如下:
temp = input("input the number: ")
temp1 = int(temp)
也可以这样:
>>> temp = float(input("input thenumber: "))
input the number: 10.9
>>> temp
10.9
4.python里面函数的也有返回值,同其他的编程语言一样,为关键字return。
>>> def MySecondFunction(name,num):
return (name,num)
>>>print(MySecondFunction("python",1))
('python', 1)
------------------------------------
>>> def MySecondFunction(num1,num2):
return num1+num2
>>> print(MySecondFunction(2,1))
3
------------------------------------
>>> def MyThirdFunction(name):
return name
>>>print(MyThirdFunction("python"))
python
python里面的return语句的可以有(),但这不是必须的,只是看起来更加美观。同时,不知道大家注意到没有,return语句要是单独字符串,打印返回值的时候并没有括号,但是要是字符串和其他类型数值(包括字符串),打印的返回值有括号。因为返回的加逗号,其实就是返回一个元组,所以打印的也是一个元组。当然返回的是列表,那么返回的也是列表。
5.函数文档:函数文档能够更加方便地说明函数的作用。调用函数说明有两种方法。
>>> def FirstFunction(name):
"name是一个形式参数" #引号里面的内容即为说明函数的文档
print(name)
打印结果显示:
>>> FirstFunction("python")
Python
打印的内容中是不会显示函数文档的内容的
-------------------------------------------------
查看函数的文档:
1.
>>> FirstFunction.__doc__
'name是一个形式参数'
-------------------------------------------------
2.
>>> help(FirstFunction)
Help on function FirstFunction in module __main__:
FirstFunction(name)
name是一个形式参数
这里推荐使用第二种方法。
6.关键参数:关键参数是当函数的参数很多的时候,为了防止实参传递的出错而设置的。这有效保障了函数的正确性
>>> def MyFunction(name,words):
print(name + ":" + words)
>>> MyFunction("python","changthe word")
python:chang the word
------------------------------------
假如name和words的实参弄反了,出现了下面的结果,那么打印的结果就会和上面的不一样
>>> MyFunction("chang theword","python")
chang the word:python
------------------------------------
所以,我们用关键参数的方法,现在不管实参的顺序,麻麻都不用担心函数会出错啦。
>>> MyFunction(words = "chang theword",name = "python")
python:chang the word
7.收集参数:收集参数是python所特有的,标志性的特点是参数前面有一个*。其实收集参数就像列表。下面来看看:
>>> def test(*params):
print("参数的长度:" ,len(params))
print("第二个参数:",params[1])
>>>test(1,2,"python",1.5,"c")
参数的长度: 5
第二个参数: 2
---------------------------------
不管test()传入多少个实参,都会默认放在params里面。要是想传入一个参数,但又不想放在params里面,那么就需要创建一个默认参数。如果想要修改默认参数,那么就得用关键参数啦。
>>> def test(*params,exp="apple"):
print("参数的长度:" ,len(params))
print("第二个参数:",params[1])
print(exp)
>>>test(1,2,"python",1.5,"c")
参数的长度: 5
第二个参数: 2
apple
-----------------------
现在我不想要apple了,想打印banana,可以用关键参数
>>>test(1,2,"python",1.5,"c",exp = 'banana')
参数的长度: 5
第二个参数: 2
banana
8.函数的作用域问题。
def Discount(price,rate):
new_price = price * rate #在这里,new_price,price ,rate都是局部变量
old_price = 50
print("discount is: ",new_price)
print("old_price1: ",old_price)
old_price = float(input("输入金额: "))
rate = float(input("输入折扣率: "))
Discount(old_price,rate)
print("old_price2: ",old_price) #为了区分两个不同的old_price,这里将与上面old_price1的1改为2
-----------------------------------------
打印的结果为:
输入金额: 100
输入折扣率: 0.1
discount is: 10.0
old_price1: 50
old_price2: 100.0
在函数里重新赋值old_price,由于ta在函数外已经被定义为全局变量,所以在函数内的会新建old_price的是局部变量,函数外的old_price被放在全局命名空间,而函数的的变量被放在堆栈中。两者是不影响的,但是ta们的名字是完全相同的。