Python学习笔记2——《Python编程:从入门到实践》

字典

  • 存储键值对
  • 键相当于索引,通过键可以得到值
  • 用花括号定义{},右边的方括号前可以加逗号,可以不加。
  • 不关心键值对的存储顺序,只跟踪键和值之间的关联关系
  • 键不能重复,但值可以重复
#定义字典
animals_color = {'cat' : 'orange', 'dog' : 'black', 'dog2' : 'white'}
#访问字典中的值
print(animals_color['cat'])
#向字典中添加新的键值对
animals_color['rabbit'] = 'white'
#删除键值对
del animals_color['cat']

遍历整个字典的方法:

#遍历所有键和值
for key, value in animals_color:
	print("\nKey: " + key)
	print("Value: " + value)
#遍历所有键
for key in animals_color.key():
	print("Key: " + key)
#不使用key()函数也可以遍历所有键(默认)
for key in animals_color:
	print("Key: " + key)
#判断字典中是否含有某个键
if 'cat' in animals_color.keys():
	print('There is a cat')
#以特定顺序显示字典中的键
for name in sorted(animals_color.keys()):
	print(name)

可以通过集合(set)提取字典中非重复值(即剔除重复值)

for color in set(animals_color.values()):	
	print(color)

通过对包含重复元素的列表调用set() ,可让Python找出列表中独一无二的元素,并使用这些元素来创建一个集合。

字典的嵌套

有时候,需要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套 。

  1. 在列表中存储字典
#创建一个用于存储外星人的空列表 
aliens = [] 
#创建30个绿色的外星人 
for alien_number in range (0,30): 
	new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'} 
	aliens.append(new_alien) 
  1. 在字典中存储列表
pizza = { 
	'crust': 'thick', 
	'toppings': ['mushrooms', 'extra cheese'], 
	} 
  1. 字典中存储字典
users = { 
	'aeinstein': { 
		'first': 'albert', 
		'last': 'einstein', 
		'location': 'princeton', 
		}, 
	'mcurie': { 
		'first': 'marie', 
		'last': 'curie', 
		'location': 'paris', 
		}, 
	} 

输入

  1. input()函数获取字符串输入
  • 语法: message = input(“提示性语句”)
message = input("input your lucky number:")
  1. int()函数将input()获取的字符串转换为数值,从而成功获取数值输入
  • 语法:int(要转换的变量)
message = input("input your lucky number:")
message = int(message)

注意:在python2.7中获取输入用raw_input()函数,而不是input()函数,因为input函数获取的是要执行的代码而不是数值或者字符串。

关于函数

定义:用def定义函数

函数传递实参的方式

python中函数传递实参的方式很多,包括:

  • 位置实参(要求实参的顺序与形参的顺序相同)

  • 关键字实参(其中每个实参都由变量名和值组成)

  • 使用列表或字典

    位置实参

    实质上与C语言一样

    def describe_pet(animal_type, pet_name):
    	print("\nI have a " + animal_type + ".")
    	print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet('hamster','harry')

    关键字实参

    关键字实参是传递给函数的名称——值对。

    def describe_pet(animal_type, pet_name):
    	print("\nI have a " + animal_type + ".")
    	print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet(pet_name = 'harry',animal_type = 'hamster')

    关键字实参的顺序无关紧要

    指定形参默认值

    def describe_pet(animal_type, pet_name='dog'):
    	print("\nI have a " + animal_type + ".")
    	print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet('harry')

    注意:无默认值的形参定义要放在有默认值的形参之前,以便于python正确解读位置实参。
    可设置形参的默认值为空,从而变为可选参数(即可以忽略的参数)

    传递任意数量的实参

    在形参前添加一个星号即可使得该形参接收不定数量的实参。

    def describe_pet(*pet_name):
    	for name in pet_name:
    		print(pet_name)

    形参名*toppings 中的星号让Python创建一个名为toppings 的空元组,并将收到的所有值都封装到这个元组中。

    使用任意数量的关键字实参

    有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可将函数编写成能够接受任意数量的键—值对——调用语句提供了多少就接受多少。方法是在该形参前加两个星号

    def build_profile(first, last, **user_info): 
    ......

    结合使用各种实参

    如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值