原创:叫我詹躲躲
来源:思否
连接:https://segmentfault.com/a/11...python
lastname = 'hello' firstname = 'world' print('个人名字是%s %s' %(lastname,firstname))
%c 字符 %s 经过str来格式化 %i 有符号十进制整数 %d 有符号十进制整数 %u 无符号十进制整数 %o 八进制整数 %x 十六进制整数(小写字母) %e 索引符号(小写e) %E 索引符号(大写E) %f 浮点实数 %g %f和%e的简写 %G %f和%E的简写
name = '老夫子' age = 28 print('姓名:{},年龄{}'.format(name,age)) 姓名:老夫子,年龄28
特色: 1.使用lambda关键字建立函数 2.没有名字的函数 3.匿名函数冒号后面的表达式有且只有一个,是表达式不是语句 4.自带return
def computer(x,y): # 计算两数和 return x+y M = lambda x,y:x+y print(M(1,2))
result = lambda a,b,c:a*b*c print(result(12,121,1))
age = 15 print('能够参军' if age>18 else '继续上学') # 直接调用 result = (lambda x,y:x if x>y else y)(2,5)
# 阶乘函数 def factorial(n): if n==1: return 1 else: return n*factorial(n-1) pass print(factorial(3))
import os #文件操做模块 def findFile(file_path): listRs = os.listdir(file_path) #获得该路径全部的文件夹 for fileItem in listRs: full_path = os.path.join(file_path,fileItem) if os.path.isdir(full_path): #判断是否为文件夹 findFile(full_path) else: print(fileItem) pass pass else: return findFile('F:\\7.代码学习')
abs(-27) #绝对值 round(21.1123) #浮点近似值 pow(2,3) #幂 2**3 divmod(10,3) # 商余 max(1,2,3,4) #最大值 min(1,2,3,4) #最小值 sum(1,2,3,4) #求和 eval() #动态执行表达式
int #整型 float #浮点型 str #字符类型 ord #返回对应字符的ASCII chr # 数字转字符 ASCII bool # boolean bin # 转换二进制 hex #转换为十六进制 oct # 八进制 list #元祖转列表 tuple #元祖 dict #建立字典 bytes #转为字节
# all 用于断定给定的可迭代参数中的全部元素是否都为TRUE,若是是返回TRUE,不然返回FALSE,除了0,空,False 外都算TRUE def all(iterable): for ele in iterable: if not ele: return False return True li = [1,2,3,4,5,6,False] print(all(li)) #False
# 所有为false,返回false def any(iterable): for ele in iterable: if ele: return False return True li = [0,False,''] print(any(li)) #False
li = ['a','b','c'] for index,item in enumerate(li,7): print(index,item) # 改下标 7 a 8 b 9 c
set1 = {'1','2'} set2 = {'11','1'} # 添加 add set1.add('3') # 清空 clear() set1.clear() # 取差集 difference set1.difference(set2) #set1取set1中有的 # 取交集 set1.intersection(set2) # 取并集 set1.union(set2) set1 | set2 # 末尾移除 set1.pop() # 指定移除 set1.discard(3) # 更新 update 合并一块儿去重 set1.update(set2)
# 1-10,20-30,35-40 def threeSum(a1,a2,a3): return sum(a1+a2+a3) a1 = list(range(1,11)) a2 = list(range(20,31)) a3 = list(range(35,41)) print(threeSum(a1,a2,a3))
def computers(): for i in range(1,101): for j in range(1,34): if i+j==100 and 3*j+i/3 ==100: print('大和尚有{}个,小和尚有{}个'.format(j,i)) pass computers() # 大和尚有25个,小和尚有75个
li = [1,1,1,2,2,2,2,3,2,2,3,4,2,1,1] def findUnionNumber(li): for item in li: if li.count(item)==1: return item pass print(findUnionNumber(li))
dict ={} for key in li: dict[key] = dict.get(key,0)+1 print(dict)
from collections import Counter a = [1, 2, 3, 1, 1, 2] result = Counter(a) print(result)
import pandas as pd a = pd.DataFrame([[1,2,3], [3,1,3], [1,2,1]]) result = a.apply(pd.value_counts) print(result)
li = [1,2,3,3,2,3,4,4,5,1,2,1] def uniqueNum(li): set1 = set(li) for i in set1: li.remove(i) set2 = set(li) for j in set2: set1.remove(j) return set1 print(uniqueNum(li))
# 面向过程编程 根据业务从上到下开始编程 # 类的结构 # 类名 属性 方法 class People: name = 'zhan', age = 20, def eat(self): print('正在吃饭') # 建立对象 people = People() people.eat()
class People: name = 'zhan', age = 20, def eat(self): print('正在吃饭') # 建立对象 people = People() people.eat() # 添加属性 people.name2 = 'zhan' people.age2 = 22
class People: # 初始化的操做,实例属性,自动执行 def __init__(self): self.name = 'zhan' self.age = 20 def eat(self): print('正在吃饭') # 建立对象 people = People() people.eat()
class People: # 初始化的操做,实例属性,自动执行 def __init__(self, name, age): self.name = name self.age = age def eat(self,food): print(self.name+food) # 建立对象 people = People('叫我詹躲躲', 20) people.eat('正在吃饭') people.eat('洗澡') people.eat('跑步')
# 相似于js里面的this class Person: def eat(self): print(id(self)) pass pass person = Person() person.eat() print(id(person)) # self和对象指向同一个内存地址,self就是对象的引用 #<__main__.Person object at 0x0000020864815CC0>
__init__ :初始化实例属性 __str__ :自定义对象的格式 __new__ :对象实例化 __class__ __del__ class Animal: def __str__(self): return '3213213123123' pass pass animal = Animal() print(animal) class Animal: def __str__(self): return '3213213123123' pass pass def __new__(cls,*args,**kwargs): print("----new执行---") return object.__new__(cls) #真正建立对象实例的 pass animal = Animal() print(animal) # __new__ 和__init__的区别 __new__ 类的实例化方法,必须返回实例,不然建立不成功 __init__数据属性的初始化工做,认为是实例的构造方法,接受实例化self并对其进行构造 __new__ 至少一个参数是cls,表明要实例化的类 __new__ 执行要比__init__早
# 属性: # name:玩家名称 # blood:血量 # 方法: # tong() 捅一刀,掉10滴血 # kanren() 砍一刀掉15滴血 # chiyao() 补血10滴血 # __str__打印玩家的状态 class Role: def __init__(self,name,blood): self.name = name self.blood = blood pass # 砍人 def tong(self,enemy): enemy.blood -=10 info = '【%s】捅了【%s】一刀'%(self.name,enemy.name) print(info) pass # 砍人 def kanren(self,enemy): enemy.blood -=15 info = '【%s】砍了【%s】一刀'%(self.name,enemy.name) print(info) pass # 吃药 def chiyao(self): self.blood +=10 info = '【%s】吃了一口药,增长10滴血'%(self.name) print(info) pass def __str__(self): return '%s还剩下%s的血量'%(self.name,self.blood) xmcx = Role('西门吹雪',100) ygc = Role('叶孤城',100) while True: if xmcx.blood<=0 or ygc.blood<=0: break print('*********************') xmcx.tong(ygc) xmcx.kanren(ygc) print('*********************') ygc.tong(xmcx) ygc.chiyao() print('*********************') print(xmcx) print(ygc) ********************* 【西门吹雪】捅了【叶孤城】一刀 【西门吹雪】砍了【叶孤城】一刀 ********************* 【叶孤城】捅了【西门吹雪】一刀 【叶孤城】吃了一口药,增长10滴血 ********************* 西门吹雪还剩下50的血量 叶孤城还剩下25的血量 ********************* 【西门吹雪】捅了【叶孤城】一刀 【西门吹雪】砍了【叶孤城】一刀 ********************* 【叶孤城】捅了【西门吹雪】一刀 【叶孤城】吃了一口药,增长10滴血 ********************* 西门吹雪还剩下40的血量 叶孤城还剩下10的血量 ********************* 【西门吹雪】捅了【叶孤城】一刀 【西门吹雪】砍了【叶孤城】一刀 ********************* 【叶孤城】捅了【西门吹雪】一刀 【叶孤城】吃了一口药,增长10滴血 ********************* 西门吹雪还剩下30的血量 叶孤城还剩下-5的血量
class Fruit: def __init__(self,name,color): self.name = name self.color = color def showColor(self): print('%s的颜色为%s'%(self.name,self.color)) apple = Fruit('苹果','红色').showColor() orange = Fruit('橘子','黄色').showColor() watermelen = Fruit('西瓜','绿色').showColor()
class CkeckSelf: def __str__(self): print(id(self)) pass CkeckSelf().__str__() selfObj = CkeckSelf() print(id(selfObj))
class Animal: def __init__(self, color, name, age): self.color = color self.name = name self.age = age def run(self): print('%s在跑步'%(self.name)) pass def eat(self): print('%s在吃东西' %(self.name)) pass def __str__(self): return '%s岁的%s的%s'%(self.age,self.color,self.name) cat = Animal('黑色','小猫',2) dog = Animal('白色','小狗',3) cat.run() dog.run() print(cat) print(dog) # 小猫在跑步 # 小狗在跑步 # 2岁的黑色的小猫 # 3岁的白色的小狗
原创:叫我詹躲躲
来源:思否
连接:https://segmentfault.com/a/11...编程