python
print(True and False)
print(True or False)
print(not False)
def flexble(*num):
sum=0
for i in num:
sum=sum+i*i
return sum
print(flexble(1,2,3,4,5))
for i,v in enumerate(range(10)):
print(i,v)
generator列表生成式
g=(x*x for x in range(10))
print(g)
for i in g:
print(i)
迭代器
from PIL import Image
img=Image.open('330.jpg')
print(img.format,img.size,img.mode)
print(img._getexif())
import sys
for i in sys.path:
print(i)
from datetime import datetime
print(datetime.now())
import requests
from bs4 import BeautifulSoup
html=requests.get('http://cuiqingcai.com/1319.html').text
soup=BeautifulSoup(html)
print('\n'*3)
# print(soup.title)
print(soup.p,'\n')
print(soup.p.string,'\n')
print(type(soup.p))
print(soup.prettify())
# print(soup.contents)
#!/user/bin/env python3
# -*- coding:utf-8 -*-
'main'
__author__='Qton'
# 面向对象编程
class Student(object):
def __init__(self, name,score):
self.name=name
self.score=score
def print_score(self):
print('%s %s'%(self.name,self.score))
qton=Student('qton',80)
qton.print_score()
# 类和实例
class Kechen(object):
# 绑定属性
def __init__(self,ke,score):
self.ke=ke
self.score=score
# 数据封装
def print_ke(self):
print(self.ke)
def print_score(self):
print(self.score)
# 创建实例
english=Kechen('english',80)
# 打印实例
# print(english.ke)
english.print_score()
# 访问限制
class Student(object):
def __init__(self, name,score):
# self.__name则只允许class内部访问 private
self.__name=name
# self.score 则外部也可以访问 public
self.score=score
# 内部访问self.__name
def print_name(self):
print(self.__name)
# qton=Student('qton',80)
# qton.print_name()
# # 外部访问
# print(qton.score)
# 继承和多态
# 继承
# Dog 类继承上面 Student 类
class Dog(Student):
pass
dog=Dog('dog',90)
dog.print_name()
# 多态
# 当子类和父类都存在相同的run()方法时,我们说,子类的run()覆盖了父类的run()
!/usr/bin/env python3
-*- coding: utf-8 -*-
class Animal(object):
def run(self):
print('Animal is running...')
class Dog(Animal):
def run(self):
print('Dog is running...')
class Cat(Animal):
def run(self):
print('Cat is running...')
def run_twice(animal):
animal.run()
animal.run()
a = Animal()
d = Dog()
c = Cat()
run_twice(c)
# 输出
# Cat is running...
# Cat is running...
print('a is Animal?', isinstance(a, Animal))
print('a is Dog?', isinstance(a, Dog))
print('a is Cat?', isinstance(a, Cat))
print('d is Animal?', isinstance(d, Animal))
print('d is Dog?', isinstance(d, Dog))
print('d is Cat?', isinstance(d, Cat))
# 输出
# a is Animal? True
# a is Dog? False
# a is Cat? False
# d is Animal? True
# d is Dog? True
# d is Cat? False
# Cat is running...
# Cat is running...
# 实例属性和类属性
class Student(object):
name = 'Student'
s = Student() # 创建实例s
print(s.name) # 打印name属性,因为实例并没有name属性,所以会继续查找class的name属性
# Student
print(Student.name) # 打印类的name属性
# Student
s.name = 'Michael' # 给实例绑定name属性
print(s.name) # 由于实例属性优先级比类属性高,因此,它会屏蔽掉类的name属性
# Michael
print(Student.name) # 但是类属性并未消失,用Student.name仍然可以访问
# Student
# 面向对象高级编程
# 数据封装、继承和多态只是面向对象程序设计中最基础的3个概念。
# 在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能。
# 我们会讨论多重继承、定制类、元类等概念。
# 使用__slots__
class Student(object):
pass
s=Student()
s.name='Qton'
def set_age(self,age):
self.age=age
# 函数的参数
# 默认参数
def power(x,y=2):
print(x,y)
# 参数为tuple list\
numbers=[1,2,3,4,5]
def calc(*numbers):
for i in numbers:
print(i)
print(calc(numbers))
# 关键字参数
# 允许传入0个或任意多个参数
def person(name,age,**kw):
print('name:',name,'age:',age,'other:',kw)
print(person('qton',23,city='jieyan',education='university'))
# 可写成
kw={'city':'jieyan','education':'university'}
print(person('qton',23,**kw))
# 限制输入任意参数
def person(name,age,*,city,education):
print(name,age,city,education)
print(person('qton',23,city='jieyan',education='university'))
# person('qton',23,sex='boy') #这样则会出错
# 递归函数
def fact(n): #累乘
if n==1:
return 1
else:
return n*fact(n-1)
print(fact(3))
# 缺点是递归次数过多会导致栈溢出
# 如fact(1000) 所以不建议用
# 高级特性
# 调试
print() #简单粗暴 缺点是最后还得删掉
# 断言
# 凡是用print()来辅助查看的地方,都可以用断言(assert)来替代
# 启动Python解释器时可以用-O参数来关闭assert:
# $ python3 -O err.py
assert()
# 把print()替换为logging是第3种方式,和assert比,logging不会抛出错误,而且可以输出到文件:
logging()
# 比较好 断点
# 单元测试
# 文档测试
!/usr/bin/env python3
-*- coding: utf-8 -*-
class Dict(dict):
'''
Simple dict but also support access as x.y style.
>>> d1 = Dict()
>>> d1['x'] = 100
>>> d1.x
100
>>> d1.y = 200
>>> d1['y']
200
>>> d2 = Dict(a=1, b=2, c='3')
>>> d2.c
'3'
>>> d2['empty']
Traceback (most recent call last):
...
KeyError: 'empty'
>>> d2.empty
Traceback (most recent call last):
...
AttributeError: 'Dict' object has no attribute 'empty'
'''
def __init__(self, **kw):
super(Dict, self).__init__(**kw)
def __getattr__(self, key):
try:
return self[key]
except KeyError:
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
def __setattr__(self, key, value):
self[key] = value
if __name__=='__main__':
import doctest
doctest.testmod()
# 看帮助文档
# import re
# print(help(re))
python 笔记(廖雪峰教程)
原文作者:Qton
原文地址: https://blog.csdn.net/Qton_CSDN/article/details/70209906
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/Qton_CSDN/article/details/70209906
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。