Python入门复习-2

总体很简单,就是想复习复习,找找被自己遗漏的知识点

六 字典

1、格式:alien={'color':'green','points':5}

2、添加键值对:alien['x_position']=0

3、创建一个空指点:alien={}

4、删除键值对:del alien['points']

5、遍历字典:for key, value in alien.items():

对于key, value这两个变量,可以使用任何名称

6、遍历字典中的所有键:for name in alien.keys():

按顺序遍历字典中的所有键:for name in sorted(alien.keys()):

7、遍历字典中的所有值:for value in alien.values():

删除重复项:for value in set(alien.values()):

8、字典嵌套

alien0={'color':'green','points':5}

alien1={'color':'yellow','points':10}

aliens={alien0,alien1}

9、在字典中可以存储列表

alien={'color':['green','yellow'],'points':5}

10、在字典中存储字典

alien={'color':['green','yellow'],'points':5,'friends':{'name':'lisa','sex':'woman'}}

七 用户输入和while循环

1、input():age=input("How old are you?")

python2.7中用的是:raw_input()

2、int():age=int(age)

3、% 求模运算,两数相除返回余数:4%3=1

4、while 只要用户不输入quit就不退出

while message != 'quit':
    message=input("Enter quit to end the program")
    print (message)

5、使用标志(用于判断整个程序是否处于活动状态)

active=True
while active:
    message=input("Enter quit to end the program")
    if message == 'quit':
        active=False
    else:
        print (message)

6、continue:返回到循环开头

7、删除包含特定值的所有列表元素

while 'cat' in pets:
    pets.remove('cat')
print (pets)

八 函数

1、实参和形参

def greet_user(username):
    print("hello, "+username.title()+"!")
greet_user('Lisa')
#username是形参,Lisa是实参

2、传递实参

a.位置实参:test('Lisa','woman')

b.关键字实参:test(sex='woman',name='Lisa')

3、默认值:def test(sex=,name='Lisa') 注意:给形参指定默认值时,等号旁边不要有空格

4、返回值:return name.title() 返回字典:return test

5、禁止函数修改列表:function_name(list_name[:]) 函数传递的是列表的副本

6、传递任意数量的实参:def make_pizza(*toppings):

a.调用:make_pizza('mushrooms','green pepers','extra cheese')

7、结合使用位置实参和任意数量实参

def make_pizza(size,*toppings):
    print ("\n make a "+str(size))
    for topping in toppings:
        print ("- "+topping)
make_pizza(16,'mushrooms','green pepers','extra cheese')    

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

a. make_pizza(16, top1='mushrooms',top2='green pepers',top3='extra cheese')

9、导入整个模块:1创建:pizza.py 2导入:import pizza 3调用:pizza.make_pizza()

10、导入特定的函数:from module_name import function_name1,function_name2

11、使用as给函数指定别名:from module_name import function_name1 as name1

12、导入模块中的所有函数:from module_name import *

九 类

1、创建类

class dog():
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def sit(self):
        print (self.name.title()+' is now sitting.')
    def roll_over(self):
        print (self.name.title()+' rolled over!')
#python2.7中 class dog(object)必须加object
#根据类创建实例
my_dog=dog("vv",6)
#访问属性
my_dog.name
#调用方法
my_dog.sit()
#创建多个实例
Lisa_dog=dog('ll','1')
Emilt_dog=dog('ee','2')

2、给属性指定默认值

class dog():
    def __init__(self,name,age):
        self.name=name
        self.age=age
        self.child=1
    def sit(self):
        print (self.name.title()+' is now sitting.')
    def roll_over(self,num):
        print (self.name.title()+' rolled over! and its child number is '+str(self.child))

3、修改属性的值:

直接赋值:lisa_dog.age=3

通过方法修改:lida_dog.roll_over(4)

4、继承

class Car():
    def __init__(self, make, model, year):
        self.make=make
        self.model=model
        self.year=year
    def test(self):
        print("test")
class ElectricCar(Car):
    def __init__(self, make, model, year):
        #python3
        super().__init__(make, model, year)
        #python2.7
        super(ElectricCar,self).__init__(make, model, year)
        
my_car=ElectricCar('tesla','model',2018)
print (my_car.test())

5、重写父类的方法

class ElectricCar(Car):
    #--skip--
    def test(self):
        print ("test2")

6、导入类

a. car.py

b.from car import Car

7、一个模块可以存储多个类,也可以从一个模块中导入多个类

from car import Car,dev

8、导入整个模块

import car

9、导入模块中的所有类

from module_name import *

10、在一个模块中导入另一个模块

#-----------------electric_car.py-----------------
from car import Car
class Battery():
    #--skip--
class ElectricCar(Car):
    #--skip--
#-----------------car.py-----------------
class Car():
    #--skip--
#-----------------my_cars.py-----------------
from car import Car
from electric_car import ElectricCar

11、python标准库里的模块collections中OrderedDict类几乎与字典相同,区别只在于记录了键值对的添加顺序

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值