python input()函数
name = input('Please enter your name: ')
print('Hello, ' + name + '!')
求模运算(%),不会指出一个数是另一个数的多少倍,而只指出余数是多少
4 % 3
# 1
5 % 3
# 2
while循环
promt = "\nTell me something, and I will repeat it back to you: "
promt = "\nEnter 'quit' to end the program."
active = True
while active:
message = input(promt)
if message == 'quit':
active = False
else:
print(message)
break跳出循环
prompt = "\nPlease enter the name of a city you have visited: "
prompt = "\n(Enter 'quit' when you are finished.)"
while True:
city = input(prompt)
if city == 'quit':
break
else:
print("I'd love to go " + city.title() + '!')
#(Enter 'quit' when you are finished.) Beijing
#I'd love to go Beijing!
#(Enter 'quit' when you are finished.)quit
循环中的continue
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
#1
#3
#5
#7
#9
删除包含特定值的所有列表元素
pets = ['dog', 'cat', 'dog', 'goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
#['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
#['dog', 'dog', 'goldfish', 'rabbit']
定义函数
def greet_user():
print('Hello!')
greet_user()
#Hello!
#---------------修改--------------
def greet_user(username):
print('Hello, ' + username + '!')
greet_user('Hui')
#Hello, Hui!
导入matplotlib库绘图
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
plt.plot(squares)
plt.show()
#![在这里插入图片描述](https://img-blog.csdnimg.cn/8c9b2821e20444a3961f0e117605caad.png)
设置坐标轴标题及刻度大小
import matplotlib.pyplot as plt
squares = [1,4,9,16,25]
#设置图表标题,并给坐标轴加上标签
plt.plot(squares,linewidth = 5)
plt.title("Square Numbers", fontsize = 24)
plt.xlabel("Value",fontsize=14)
plt.ylabel("Square of Value",fontsize=14)
#设置刻度标记的大小
plt.tick_params(axis='both',labelsize=14)
plt.show()
#![在这里插入图片描述](https://img-blog.csdnimg.cn/f68238ac2e2c4acebef71b9681e30131.png)
#linewidth 决定plpt()绘制的线条的粗细
#title() 给图表制定标题
#fontsize指定了图表中文字的大小
#xlabel() ylabel()为每条轴设置标题
#tick_params() 设置刻度样式
保存图片
plt.savefig('squares_plot.png',bbox_inches='tight')
使用scatter绘制一系列点,并使用颜色映射
import matplotlib.pyplot as plt
x_value = list(range(1,5001))
y_value = [x**3 for x in x_value]
plt.scatter(x_value,y_value,c=y_value, cmap=plt.cm.Blues,edgecolor='none',s=40)
plt.show()
#![在这里插入图片描述](https://img-blog.csdnimg.cn/bd46ddc3b9e24ed397b2f637ba01c05e.png)
#绘制散点scatter()
#颜色映射 将参数c设置为一个y值列表,并使用参数cmap制定pyplot使用哪个颜色映射
类的定义。类中的函数称为方法,可以通过实例访问的变量称为属性
class Dog():
'''一次模拟小狗的简单尝试'''
def __init__(self,name,age):
'''初始化属性name和age'''
self.name = name
self.age = age
def sit(self):
'''模拟小狗被命令时蹲下'''
print(self.name.title() + " is now sitting.")
def rool_over(self):
'''模拟小狗被命令时打滚'''
print(self.name.title() + ' is rolled over.')
继承
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
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 rool 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',2016)
print(my_tesla.get_descriptive_name())
#2016 tesla model s