1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python编程入门到实践笔记-python基础(《Python编程:从入门到实践》读书笔记)...

python编程入门到实践笔记-python基础(《Python编程:从入门到实践》读书笔记)...

时间:2022-04-30 00:45:36

相关推荐

python编程入门到实践笔记-python基础(《Python编程:从入门到实践》读书笔记)...

注: 本文的大部分代码示例来自书籍《Python编程:从入门到实践》。

一、变量:

命名:

(1)变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头

(2)变量名不能包含空格,但可使用下划线来分隔其中的单词。

(3)不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print

头文件声明: #-- coding: utf-8 --

二、数据类型:

(1)数字(Number):num = 123

(2)布尔型:True 、 False

(3)字符型(char):

str = "hello"

str[1]:取第2个字符

str[1:-1:2]: 开始位置,结束位置(-1表示最后),步长.

len(str)

str.title():首字母大写

str.rstrip() lstrip() strip():处理空白字符串

str(numer): 转化成字符型

(4)列表(list):list = ["huwentao","xiaozhou","tengjiang","mayan"]

list.append()

list.insert(0, "")

del list[0]

list.pop()

list.remove():根据值来删除

list.sort()

(5)元组(tuple):不可修改 tuple = ("huwentao","xiaozhou","tengjiang","mayan")

(6)字典(dict):

user_0 = {

"username": "efermi",

"first": "enrico",

"last": "fermi",

}

❶ for key, value in user_0.items():

❷ print(" Key: " + key)

❸ print("Value: " + value)

三、语句

(1)if语句:

age = 12

❶ if age < 4:

print("Your admission cost is $0.")

❷ elif age < 18:

print("Your admission cost is $5.")

❸ else:

print("Your admission cost is $10.")

(2)for循环

(3)while循环

current_number = 1

while current_number <= 5:

print(current_number)

current_number += 1

for循环是一种遍历列表的有效方式,但在for循环中不应修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while循环。通过将while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

四、用户输入

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。

message = input("Tell me something, and I will repeat it back to you: ")

print(message)

五、函数

基本函数的定义:

def greet_user(username):

"""显示简单的问候语"""

print("Hello, " + username.title() + "!")

greet_user("jesse")

使用默认值:

def describe_pet(pet_name, animal_type="dog"):

"""显示宠物的信息"""

print(" I have a " + animal_type + ".")

print("My " + animal_type + ""s name is " + pet_name.title() + ".")

describe_pet(pet_name="willie")

传递任意数量的形参

def make_pizza(*toppings):

"""打印顾客点的所有配料"""

print(toppings)

make_pizza("pepperoni")

make_pizza("mushrooms", "green peppers", "extra cheese")

六、类

基础类的定义:

class Car():

"""一次模拟汽车的简单尝试"""

❶ def __init__(self, make, model, year):

"""初始化描述汽车的属性"""

self.make = make

self.model = model

self.year = year

❷ def get_descriptive_name(self):

"""返回整洁的描述性信息"""

long_name = str(self.year) + " " + self.make + " " + self.model

return long_name.title()

❸ my_new_car = Car("audi", "a4", )

print(my_new_car.get_descriptive_name())

类的继承:

class Car():

"""一次模拟汽车的简单尝试"""

def __init__(self, make, model, year):

self.make = make

self.model = model

self.year = year

self.odometer_reading = 0

def get_descriptive_name(self):

long_name = str(self.year) + " " + self.make + " " + self.model

return long_name.title()

def read_odometer(self):

print("This car has " + str(self.odometer_reading) + " miles on it.")

def update_odometer(self, mileage):

if mileage >= self.odometer_reading:

self.odometer_reading = mileage

else:

print("You can"t roll back an odometer!")

def increment_odometer(self, miles):

self.odometer_reading += miles

❷ class ElectricCar(Car):

"""电动汽车的独特之处"""

❸ def __init__(self, make, model, year):

"""初始化父类的属性"""

❹ super().__init__(make, model, year)

❺ my_tesla = ElectricCar("tesla", "model s

可以将实例作为属性来定义一个类

class Car():

--snip--

❶ class Battery():

"""一次模拟电动汽车电瓶的简单尝试"""

❷ def __init__(self, battery_size=70):

"""初始化电瓶的属性"""

self.battery_size = battery_size

❸ def describe_battery(self):

"""打印一条描述电瓶容量的消息"""

print("This car has a " + str(self.battery_size) + "-kWh battery.")

class ElectricCar(Car):

"""电动汽车的独特之处"""

def __init__(self, make, model, year):

"""

初始化父类的属性,再初始化电动汽车特有的属性

"""

super().__init__(make, model, year)

❹ self.battery = Battery()

my_tesla = ElectricCar("tesla", "model s", )

print(my_tesla.get_descriptive_name())

my_tesla.battery.describe_battery()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。