python速成第二天

【第二天,继续坚持!】

 一、基本语句

#6.1 条件语句
age = 12
if age < 8:
    ticket = 0
elif age < 12:
    ticket = 10
else:
    ticket = 20
print(ticket)

#6.2 输入语句
message = input("I will print what you write down here:" )
print(message)
#值得注意的是,input得到的是字符串,这意味着处理数值输入时需要类型转换

#6.3 循环语句
#for语句在列表操作中已使用,不再赘述
#6.3.1 while循环
num = 1
while num <=5:
    print(num)
    num += 1
#6.3.2 使用标志
flag = True
while flag:
    message = input("please enter:")
    if message == 'quit':
        flag = False
    else:
        print(message)
#6.3.3 使用break
while True:
    message = input("please enter:")
    if message == 'quit':
        break
    else:
        print(message)
#6.3.4 while循环删除指定的所有列表元素
pets = ['dog','cat','fish','rabbit','cat','dog','cat']
while 'cat' in pets:
    pets.remove('cat')
print(pets)

 

二、函数

#7.1 定义函数
def greet_user():
    print("hello function!")

greet_user()

#7.2 传递实参
def greating(name,date="2022-5-18"):
    print("Welcome," + name + "!")
    print("Today is " + date )

greating("scott","2022-5-17")               #位置实参
greating(date="2022-5-17",name="scott")     #关键字实参
greating("scott")                           #date设置了默认值

#7.3 返回值
def city_country(cityname,country):
    info = '"' + cityname + ',' + country + '"'
    return info

city1 = city_country("beijing","china")
print(city1)


def make_album(singer,album,number=''):
    if number:
        info = "singer:" + singer + "  album:" + album + "  number:" + str(number)
    else :
        info = "singer:" + singer + "  album:" + album
    return info

test1 = make_album("Taylor","lover")
test2 = make_album("Taylor","lover",18)
print(test1)
print(test2)

#7.4 传递列表
magicians = ['amy','bob','paul','alice','mike']
def show_magicians(mag_list):
    for magician in mag_list:
        print(magician)
# 7.4.1 禁止修改列表(利用切片建立副本)
new_magicians=[]
def show_great(mag_list):
    for i in range(0,len(mag_list)):
        magician = mag_list.pop()
        new_magician = "the Great " + magician
        mag_list.insert(0,new_magician)
    return mag_list

new_magicians =  show_great(magicians[:])
show_magicians(new_magicians)

#7.4.2 直接修改列表
def show_great(mag_list):
    for i in range(0,len(mag_list)):
        magician = mag_list.pop()
        new_magician = "the Great " + magician
        mag_list.insert(0,new_magician)

show_great(magicians)
show_magicians(magicians)


#7.5 传递任意数量的实参
#7.5.1 *创建一个空元组
def sandwich(*ingredients):
    print("\tYour sandwich will include the following things:")
    for item in ingredients:
        print("-"+item)
sandwich('egg','mushrooms','extra cheese')
#7.5.2 **创建一个空字典
def bulid_profile(first,last,**user_info):
    profile = {}
    profile['firstname'] = first
    profile['lastname'] = last
    for key,value in user_info.items():
        profile[key] = value
    return profile
user_profile = bulid_profile('scott','win',location = 'princeton',field = 'physics')
print(user_profile)

#7.6 函数存储
#7.6.1 导入整个包
import pizza
pizza.sandwich('egg','mushrooms')
#7.6.2 导入整个包并使用as指定别名
import pizza as k
k.sandwich('egg','mushrooms')
#7.6.3 导入指定函数
from pizza import sandwich
sandwich('egg','mushrooms')
#7.6.4 导入指定函数并使用as指定别名
from pizza import sandwich as sw
sw('egg','mushrooms')

 

三、类

注意:基本规范

  1.  类名应采用驼峰命名法,即将类名中的每个单词的首字母都大写,而不使用下划线。
  2.  实例名和模块名都采用小写格式,并在单词之间加上下划线。
  3. 每个类定义后面都应该包含一个文档字符串,用于简要地描述功能。
  4. 每个模块也都应包含一个文档字符串,对其中的类可用于做什么进行描述。
  5. 在类中,可使用一个空行来分隔方法;而在模块中,可使用两个空行来分隔类。
#8.1 创建和使用类
class Restaurant():
    def __init__(self,restaurant_name,cuisine_type):
        self.name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(self.name + "  " + self.cuisine_type)
    def open_restaurant(self):
        print("it's opening")

pizza_store = Restaurant('pistore','pizza')
pizza_store.describe_restaurant()
pizza_store.open_restaurant()

#8.2 使用类和实例
#8.2.1 给属性指定默认值
class Car():
    def __init__(self,make,model,year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0
    def update_odometer(self,mileage):
        self.odometer_reading = mileage
#8.2.2 修改属性的值
my_new_car = Car('audi','a4',2016)
my_new_car.odometer_reading = 23

my_new_car.update_odometer(23)

#8.3 继承
class ElectricCar(Car):
    def __init__(self,make,model,year):
        super().__init__(make,model,year)
my_tesla = ElectricCar('tesla','model s',2016)

#8.4 导入类
#参考函数导入方法 import

#8.5 python标准库
#参考网站:http://pymotw.com/

 

参考

Python编程:从入门到实践S

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值