This commit is contained in:
github 2020-11-09 10:02:34 +08:00
parent bf2717ff91
commit 368e204a10
37 changed files with 25 additions and 181 deletions

View File

@ -1,3 +1,15 @@
# 类是抽象的概念,仅仅是模板。 比如说:“人”, 类定义了人这种类型属性nameage...)和方法(study,work...)。
# 对象是一个你能够看得到、摸得着的具体实体: 赵本山,刘德华,赵丽颖,这些具体的人都具有人类型中定义的属性和方法,不同的是他们各自的属性不同。
# 根据类来创建对象被称为实例化。
# 语法格式
# class 类名:
#
# def 方法1(self, 参数列表):
# pass
#
# def 方法2(self, 参数列表):
# pass
class Cat:
name = ''
def __init__(self):

View File

@ -1,3 +1,4 @@
# 面向对象的实现方式封装性更好,已经行驶的公里数是对象内部的属性,对象自身负责管理,外部调用代码无需管理。我们随时可以调用对象的方法和属性得知对象当前的各种信息。而面向过程的方式而言,外部调用代码会“手忙脚乱”
def car(type,price):
print(f'car的价格是 {price}, 类型是{type}')
car(12,'qqq')

View File

@ -24,4 +24,7 @@ class Retangle(Shape):
s = Shape('red')
c1 = Circle('green',13)
r = Retangle('green',13,22)
print(r.area())
print(s,c1,r)
print(r.area())
# 一定不要忘记在子类的init方法中调用super()._init_()

View File

@ -4,7 +4,7 @@ data = np.genfromtxt('data.txt',delimiter=' ')
print(data)
# 1\. 打开 - 文件名需要注意大小写
file = open("data.txt",encoding='utf-8')
file = open("data.txt", encoding='utf-8')
print(file)
# 完整例子\. 读取
text = file.read()

View File

@ -23,5 +23,3 @@ module.output()
import syu
syu.module.output()
# from syu import *
# module1.output1()

View File

@ -16,6 +16,10 @@ if (80 <= java < 90) or (80 <= python < 90):
# 成绩>=90分妈妈给他买MP4
# 90分>成绩>=60分妈妈给他买本参考书
# 成绩<60分什么都不买
score = 0
input(score)
if score >=0:
print(11111111111)
for letter in 'www.baidu.com':
print(letter)
@ -39,7 +43,7 @@ for i in range(1,10):
print()
# 随机数的处理
综合练习---猜数字
# 综合练习---猜数字
# 计算机要求用户输入数值范围的最小值和最大值。计算机随后“思考”出在这个范围之内的一个随机数,并且重复地要求用户猜测这个数,直到用户猜对了。在用户每次进行猜测之后,计算机都会给出一个提示,并且会在这个过程的最后显示出总的猜测次数。这个程序包含了几种类型的我们学过的 Python 语句,例如,输入语句、输出语句、赋值语句、循环和条件语句
import random

View File

@ -1,4 +1,5 @@
# 写一个循环不断的问用户想买什么用户选择一个商品编号就把对应的商品添加到购物车里最终用户输入q退出时打印购物车里的商品列表
# 写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,
# 最终用户输入q退出时打印购物车里的商品列表
products = [['iphone',6888],['三星',3000],['小米',2500]]
shopping_car = []

View File

@ -1,17 +1,13 @@
# 接受一个或多个函数作为输入 输出一个函数
def add(x, y, f):
return f(x) + f(y)
res = add(3, -6, abs)
print(res)
def method():
x = 2
def double(n):
return n * x
return double

View File

@ -1,22 +0,0 @@
# for 循环语句
for letter in 'www.neuedu.com':
print(letter)
for value in range(1, 5):
print(value)
for num in range(10,20): # 迭代 10 到 20 之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print ('%d 是一个合数' % num)
break # 跳出当前循环
else: # 循环的 else 部分
print ('%d 是一个质数' % num)
# 打印九九乘法表
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}\t'.format(i, j, i*j), end='')
print()

View File

@ -1,2 +0,0 @@
def output1():
print('hello syu')

View File

@ -1,19 +0,0 @@
# 异常类型捕获演练 —— 要求用户输入整数
while True:
try:
num = int(input("请输入整数:"))
result = 8 / num
print(result)
except ValueError:
print("请输入正确的整数")
except ZeroDivisionError:
print("除 0 错误")
continue
print("111111111111111")
except Exception as result:
print("未知错误 %s" % result)
else:
print("正常执行")
finally:
print("执行完成,但是不保证正确")

View File

@ -1,12 +0,0 @@
def demo1():
return int(input("请输入一个整数:"))
def demo2():
return demo1()
try:
print(demo2())
except ValueError:
print("请输入正确的整数")
except Exception as result:
print("未知错误 %s" % result)

View File

@ -1,21 +0,0 @@
def input_password():
# 1\. 提示用户输入密码
pwd = input("请输入密码:")
# 完整例子\. 判断密码长度,如果长度 >= 8返回用户输入的密码
if len(pwd) >= 8:
return pwd
# 3\. 密码长度不够,需要抛出异常
# 1> 创建异常对象 - 使用异常的错误信息字符串作为参数
ex = Exception("密码长度不够")
# 完整例子> 抛出异常对象
raise ex
try:
user_pwd = input_password()
print(user_pwd)
except Exception as result:
print("发现错误:%s" % result)

View File

@ -1,12 +0,0 @@
class MyException(Exception): #让MyException类继承Exception
def __init__(self,name,age):
self.name = name
self.age = age
try:
#知识点:主动抛出异常,就是实例化一个异常类
raise MyException("zhansgan",19) #实例化一个异常,实例化的时候需要传参数
except MyException as obj: #这里体现一个封装,
print(obj.age,obj.name) #捕获的就是MyException类携带过来的信息
except Exception as obj: #万能捕获之前的可能捕获不到这里添加Exception作为保底
print(obj)

View File

@ -1,20 +0,0 @@
word="I'm a boby, I'm a girl. When it is true, it is ture. that are cats, the red is red."
word=word.replace(',','').replace('.','')
word=word.split()
print(word)
print('第1种方法')
setword=set(word)
for i in setword:
count=word.count(i)
print(i,'出现次数:',count)
print('第2种方法')
dict = {}
for key in word:
dict[key] = dict.get(key, 0) + 1
print(dict)
print('第3种方法')
from collections import Counter
result = Counter(dict)
print(result)
print(result.most_common(3))

View File

@ -1,42 +0,0 @@
class Cat:
"""这是一个猫类"""
def eat(self):
print(f"小猫爱吃鱼,我是{self.name},self的地址是{id(self)}")
def drink(self):
print("小猫在喝水")
tom = Cat()
print(f'tom对象的id是{id(tom)}')
tom.name = "Tom"
tom.eat()
print('-'*60)
lazy_cat = Cat()
print(f'lazy_cat对象的id是{id(lazy_cat)}')
lazy_cat.name = "大懒猫"
lazy_cat.eat()
class Cat:
def __init__(self, name):
print("初始化方法 %s" % name)
self.name = name
tom = Cat("Tom")
lazy_cat = Cat("大懒猫")
# _str_ 方法,如果在开发中,希望使用 print 输出 对象变量 时,能够打印 自定义的内容,就可以利用 __str__ 这个内置方法了
class Cat:
def __init__(self, new_name):
self.name = new_name
print("%s 来了" % self.name)
def __str__(self):
return "我是小猫:%s" % self.name
tom = Cat("Tom")
print(tom)

View File

@ -1,21 +0,0 @@
def CarInfo(type,price):
print ("the car's type %s,price:%d"%(type,price))
print('函数方式(面向过程)')
CarInfo('passat',250000)
CarInfo('ford',280000)
class Car:
def __init__(self,type,price):
self.type = type
self.price = price
def printCarInfo(self):
print ("the car's Info in class:type %s,price:%d"%(self.type,self.price))
print('面向对象')
carOne = Car('passat',250000)
carTwo = Car('ford',250000)
carOne.printCarInfo()
carTwo.printCarInfo()