#学习笔记Python#7、列表 字典(12下)&8、列表与字典 (第12章扫尾)&9、函数(13)

7、列表 字典(12下)
2017-08-09 19:14
查找索引(为了找到一个元素位于列表中的什么位置)
暂时没有找到方法。。。。。

循环处理列表

letters=["a","b","d","e"]
for letter in letters:
    print (letter)

结果:

a
b
d
e

这里的循环变量是letter,每次迭代时,当前元素会储存在变量letter中,然后显示出来。

列表排序

letters=["a","f","d","b"]
print (letters)
letters.sort()
print(letters)

结果:

['a', 'f', 'd', 'b']
['a', 'b', 'd', 'f']

sort()会自动按字母顺序或者数字顺序从小到大排序

sort()会原地修改列表,它会改变原始列表而不是创建一个新的有序列表。
因此 print(letters.sort())是错误的。
必须分两步来完成:

letters.sort()
print(letters)

逆序排序

letters=["a","e","d","b"]
letters.reverse()
print (letters)

结果:

['b', 'd', 'e', 'a']

这个是把给定元素倒序展示。
另:

letters=["a","e","d","b"]
print (letters)
letters.sort(reverse=True)
print(letters)

结果:

['a', 'e', 'd', 'b']
['e', 'd', 'b', 'a']

这个是按大小次序排好,即从大到小。

列表是可变量,数字与字符串是不可改变的
元组------不可改变的列表
建立元组:

my_tuple=("red","green","blue")

使用圆括号
元组是不可改变的,所以无法对元组进行排序,也不能追加删除,一旦建立,就一直保持不变。

—————————————————————————————————————
8、列表与字典 (第12章扫尾)
2017-08-12 21:52

双重列表:

joemarks=[23,56,24,26]
tommarks=[34,44,33,25]
bethmarks=[35,56,45,44]
classMarks=[[joemarks],[tommarks],[bethmarks]]
print (classMarks)

结果:

[[23, 56, 24, 26], [34, 44, 33, 25], [35, 56, 45, 44]]

也可以直接 classMarks=[[23, 56, 24, 26], [34, 44, 33, 25], [35, 56, 45, 44]]
循环:

for stumarks in classMarks:
    print (stumarks)

结果:

[23, 56, 24, 26]
[34, 44, 33, 25]
[35, 56, 45, 44]

从表中获取一个值

print (classMarks[0][3])
  0  1   2   3
 23	 56	24 	 26
 34	 44	 33	 25
 35	 56	 45	 44

行 可视为大的元素(列表的列表),列 为列表的列表元素

字典:
键 值 键值对的集合成为字典

创建一个空的字典 phonenumber={}
例:

phonenumber["jhon"]="123456"
print(phonenumber)

结果:{‘jhon’: ‘123456’}
也可以直接用phonenumber={'jhon': '123456'}

我们创建字典,可以在字典中查找东西

phonenumber={}
phonenumber["jhon"]="123456"
phonenumber["jane"]="123478"
phonenumber["mary"]="129816"
print(phonenumber["jane"])

结果:123478

列表与字典:
相似:
包含任意类型 数字 字符串 对象甚至其它集合的集合
都提供了在集合中查找条目的方法
不同:
列表有顺序,如果你按照某种顺序添加元素,这些元素会保持这种顺序。还可以排序
字典是无序的,你想字典中加入内容,打印出来顺序可能会改变。
列表中的元素是索引访问(用索引号)的,而字典中的条目是使用键来访问的

keys

print(phonenumber.keys())

结果:dict_keys(['jhon', 'jane', 'mary'])

values

print(phonenumber.values())

结果:dict_values(['123456', '123478', '129816'])
哈希表
字典中的值可以包含字典
字典中的键只可以使用不可变类型(布尔,整数,浮点数,字符串和元组)不能使用一个列表或者字典当做键,因为他们是可变类型

sorted()
字典是无序的,可以对键进行排序,按照键的顺序打印

phonenumber["jhon"]="123456"
phonenumber["mary"]="129816"
phonenumber["jane"]="123478"
for key in sorted(phonenumber.keys()):
    print(key,phonenumber[key])

结果:

jane 123478
jhon 123456
mary 129816

因为键的集合是个列表

对字典的值排序

phonenumber["jhon"]="923456"
phonenumber["mary"]="129816"
phonenumber["jane"]="143478"
for value in sorted(phonenumber.values()):
    for key in phonenumber.keys():
        if phonenumber[key]==value:
            print(key,phonenumber[key])

结果:

mary 129816
jane 143478
jhon 923456

我们首先获得排序之后,针对每一个值,循环遍历字典中的所有键,直到找到与该值关联的键。

del 删除一个条目

del phonenumber["jane"]
print(phonenumber)

结果:{'jhon': '923456', 'mary': '129816'}
clear()删除所有条目

phonenumber.clear()
print(phonenumber)

结果:{}
in确定某个值在字典中是否存在

print("jane" in phonenumber)

结果True

—————————————————————————————————————
9、函数(13)
2017-08-15 14:56
使用def关键字定义一个函数,函数名后有一对括号,后面跟冒号。

def printMyAddress():
    print("aaa")
    print("www")

printMyAddress()

调用函数:实参;函数部分:形参
带两个参数的函数:

def printMyAddress(name,address):
    print(name)
    print(address)
printMyAddress("a","2")

带返回值的函数:

def calculateTax(price,tax_rate):
    total=price+(price*tax_rate)
    return total

my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)

使用表达式的任何地方都可以使用函数来返回值,可以把返回值赋给一个变量,也可以在另一个表达式中使用,或者打印出来

变量作用域
对于函数而言,函数内的名字只是在函数运行时才会创建的。在函数运行之前,或者完成运行之后甚至根本不存在。
函数运行结束时,其中所有名字都不再存在。
程序中,使用或者可以使用变量的部分,称为变量的作用域。

局部变量:如price total,在函数以外的某个位置无法打印出来其值,只在函数运行时才存在。
全局变量:函数以外定义的变量,有更大的作用区域,程序的主部分,

def calculateTax(price,tax_rate):
    total=price+(price*tax_rate)
    print(my_price)
    return total
my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)

在函数内部可以打印出全局变量的值。
如果试图在函数内部改变全局变量的值,你会得到一个新的局部变量。

def calculateTax(price,tax_rate):
    total=price+(price*tax_rate)
    my_price=335555
    print(my_price)
    return total
my_price=float(input("Enter a price:"))
totalPrice=calculateTax(my_price,0.06)
print("price=",my_price,"Total price=",totalPrice)

结果:

Enter a price:4
335555
price= 4.0 Total price= 4.2

强制为全局:
用关键字 global来做到,

def calculateTax(price,tax_rate):
global my_price

使用global关键字,Python不会建立名为my_price的局部变量,而是会使用名为my_price的全局变量,另外,如果还没有名为my_price的全局变量,Python会创建一个。

总:
局部变量与全局变量可以重名,最好不要,因为容易混乱只要有混乱,错误就趁机而入。
Python会在需要时自动创建新的局部变量,或者也可以用global关键字阻止它创建。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值