增加函数的灵活性,给函数加参数:
def hello(name):
print('hello' + name)
hello('siki')
hello('mike')
则 hello(name)里的name是给的变量,后面调用hello时括号里给出变量
name:形参
siki mike:实参
给多个参数:
def add(a,b):
res = a + b
print(res)
add(5,19)
两个参数之间用逗号
增加函数的灵活性,给函数加参数:
def hello(name):
print('hello' + name)
hello('siki')
hello('mike')
则 hello(name)里的name是给的变量,后面调用hello时括号里给出变量
name:形参
siki mike:实参
给多个参数:
def add(a,b):
res = a + b
print(res)
add(5,19)
两个参数之间用逗号
函数类型:
1. 内置函数
2. 自定义函数
方法:
def methodName():
Methodbody
讲解:
def 是定义一个函数(注意加:)
methodname是方法名,自己定义的
methodbody是方法体,前面有缩进,方法体可以包含多行语句
自定义函数的目的:方便调用多次使用的代码
1. 利用while给字典输入数据:
d = {}
while True:
key = input('input key:')
value = input('input value:')
d[key]=value
res = input('any new key or value? (yes/no)')
if res == 'no':
break
print(d)
2. 把print的输出不换行
原理:当print后不进行定义时,则默认是 end='\n', 即换行
print(i, end='\n')
因此,删去\n就不会换行了,中间可以加自己想用的分隔符,如空格、横杠等
所以在i后面添加多个\n时,可以进行多个换行
1. 将一个列表的值移到另一个列表:
l1 = [ 'sz' , 'gz' , 'bj' , 'hk' ]
l2 = [ ]
while l1 :
l2.append(l1.pop())
print(l1)
print(l2)
如果在while后面加个列表,如上面,则列表不为空时,返回结果为true;为空则为false
2. 把列表里的某一个值(可能重复)全部移除:
l = [ 'sz' , 'gz' , 'bj' , 'hk' , 'sz' , 'sz' ]
while 'sz' in l:
l.remove('sz')
print(l)
遍历列表
1. 遍历列表用for循环比较方便
2. 用while循环列表:
l = [ 'sz' , 'gz' , 'bj' , 'hk' ]
i = 0
#len(l)-1是最大索引值
while i <= len(l)-1:
print(l[i])
i += 1
遍历字典:
d = { 'name' : 'siki' , 'age' : 90}
#d.keys()可以获得所有的键
i = 0
keyList = list(d.keys())
while i <len(keyList):
key = keyList[i]
value = d[key]
print(key,value)
i += 1
下面这个代码,会导致即使输入了0,也会再操作一次再停止
i = 1
while i != 0 :
i = int(input('input number:'))
if i %2 == 0 :
print('oushu')
print(i)
方法一:
he = 0
i = -1
while he <= 100 :
i = int(input('input int:'))
he += i
print(he)
方法二:用标注位
he = 0
i = -1
flag = True
while he <= 100 :
i = int(input('input int:'))
he += i
if he>100:
flag = False
print(he)
方法三:利用break
he = 0
i = -1
while True :
i = int(input('input int:'))
he += i
if he>100:
break
print(he)
方法一:
he = 0
i = -1
while i != 0 :
i = int(input())
he += i
print(he)
这里在最开始给i赋值为-1,不影响循环,因为while里是用用户输入的覆盖了
自己的方法:
he = 0
print('input a number')
i = int(input())
while i != 0:
he += i
print('input a number')
i = int(input())
print(he)
这里的提示句可以直接写到input里,如input(’请输入一个数字:’)
方法一,用for:
he = 0
for i in range(1,101):
he += i
print(he)
方法二,用while:
he = 0
i = 1
while i < 101:
he += i
i += 1
print(he)
while循环类似for遍历,二者在不同的情况下好用
while循环体的语句:
while True:
do xx
当while后面接的语句为true时,则会一直循环下面的命令(如果没有终止条件,属于死循环)
如:
i = 1
while i<=10:
print(i)
i += 1
(最后i其实是11了)
列表中元素的改变:
1. 修改:
列表名[N] = '新数据'
2. 添加:
(1)添加到末尾
列表名.append('新数据')
(2)插入
列表名.insert(N,'新数据')
N代表新数据插入的位置,原来在这个位置的及后面的相应向后走
3. 删除
(1)根据索引删除
del(列表名[N]) 或 del 列表名[N]
N:要删除的元素所在的位置
(2)根据数据删除
列表名.remove('要删除的数据')
注意如果列表里有多个重复的数字,则remove会删除掉第一个,后面的不会删
判断键是否在字典里:
res = 'gender' in stu1.keys()
print (res)
判断值是否在字典里:
res = 'siki' in stu1.values()
print (res)
如果是siki' in stu1,则默认是判断是否在键里
设置键的默认值:
stu1.setdefault('name','micheal')
取得键值对的集合:
主要为了方便遍历键值对
方式:stu1.items()
相当于形成了元祖
使用方法:
1. 遍历方法:
for i in stu1.items():
print(i)
如果用print(i[0] , i[1]),则是分别打印出每个键值对的键和值,也可以用如下的方法:
for k,v in stu1.items():
print(k,v)
2. 生成列表
items = list(stu1.items())
print(items)
获得键的集合:
stu1.keys()
stu1为字典名字
也可以直接输出
使用方式:
1. 用for循环遍历:
for k in stu1.keys():
print(k)
2. 形成列表
keys = list(stu1.keys())
print(keys)
获得值的集合:
stu1.values()
使用方法同上
两个列表相等:
长度和内容完全一样,并且顺序也一样
两个字典相同:
内容一样,顺序不相关
用 == 进行判断
列表里嵌套字典:
students = [ { 'name' : 'siki' , 'age' : 17 , 'gender' : 'boy' } ,
{ 'name' : 'lic' , 'age' : 27 , 'gender' : 'girl' } ,
{ 'name' : 'siki' , 'age' : 17 , 'gender' : 'boy' } ,
{ 'name' : 'siki' , 'age' : 17 , 'gender' : 'boy' } ]
访问:print (students[1]['gender'])
字典里嵌套列表:
stu1 = { 'name' : 'siki' , 'age' : 17 , 'gender' : 'boy' , 'hobby' : ['篮球','zu'] }
访问:print(stu1['hobby'][0])
python:一般是一行一句代码,但定义东西时可以放在多行,如定义列表、字典 (这样更容易看)
很长的字符串超出了一行,则可以使用连续字符,在行末加\,则可以把不同的行认为一行
1. 添加键值对:
stu1['rank'] = 3
2. 修改键值对(同上,直接覆盖):
stu1['rank'] = 3
3. 删除:
del(stu1['age']) 或 del stu['age']
字典和列表的相同点:
字典和列表的不同点:
字典
键值对的集合,也是一种数据类型
1. 定义字典:stu1 = { 'name' : 'siki' }
2. 通过键访问值:stu1['name']
与列表的区别:
列表适合保存同一类型的东西
字典适合保存同一个对象的所有信息
用 for in range(n,m)来使下方的命令循环(m-n)次
用break提前结束循环