作者:佳品h空投 | 来源:互联网 | 2023-05-18 03:00
fibo.py
__author__ = 'Administrator'
def fib(n):
a,b=0,1
while b print(b,end=' ')
a,b=b,a+b
print()
def fib2(n):
result=[]
a,b=0,1
while b result.append(b)
a,b=b,a+b
return result
if __name__ == '__main__':
print('fibo.py 程序自身在运行')
else:
print('fibo.py来自另一模块')
main.py
__author__="xxx"
#http://www.w3cschool.cc/python3/python3-module.html
#模块的调用
import fibo
print(fibo.fib(1000))
print(fibo.fib2(100))
#直接把模块内(函数,变量的)名称导入到当前操作模块。
#但是那些由单一下划线(_)开头的名字不在此例
from fibo import fib,fib2
print(fib(500))
print(fib2(5000))
#每个模块都有一个__name__属性,当其值是'__main__'时,表明该模块自身在运行,否则是被引入。
if __name__ == '__main__':
print('程序自身在运行')
else:
print('我来自另一模块')
#dir() 函数会罗列出当前定义的所有名称:
print(dir())
print(dir(fibo))
import sys
print(dir(sys))
#格式化字符串
s = 'Hello, world.'
print(str(s))
print('We are the {} who say "{}!"'.format('knights', 'Ni'))
print('{0} and {1}'.format('spam', 'eggs'))
print('{1} and {0}'.format('spam', 'eggs'))
print('This {food} is {adjective}.'.format(food='spam', adjective='absolutely horrible'))
import math
print(math.pi)
print('The value of PI is approximately %5.3f.' % math.pi)
#操作文件
import os
name="test.txt"
if os._exists(name):
os.remove(name)
f = open(name, 'w+')
f.write('0123456789abcdef')
f.seek(0)
print(f.readline())
f.seek(5) # 移动到文件的第六个字节
print(f.read(1))
#f.seek(-3, 2) # 移动到文件的倒数第三字节
f.close()
#try catch
try:
f = open('test.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
def this_fails():
x = 1/0
try:
this_fails()
except ZeroDivisionError as err:
print('Handling run-time error:', err)
#自定义异常
class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
try:
raise MyError("fuck")
# raise MyError(2*2)
except MyError as e:
print('My exception occurred, value:', e.value)
#http://www.w3cschool.cc/python3/python3-errors-execptions.html
__author__="xxx"
#类定义
class people:
#定义基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s is speaking: I am %d years old weight=%s" %(self.name,self.age,self.__weight))
p = people('tom',10,30)
p.speak()
#单继承示例
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#调用父类的构函
people.__init__(self,n,a,w)
self.grade = g
#覆写父类的方法
def speak(self):
print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))
s = student('ken',20,60,3)
s.speak()
#另一个类,多重继承之前的准备
class speaker():
topic = ''
name = ''
def __init__(self,n,t):
self.name = n
self.topic = t
def speak(self):
print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))
#多重继承
class sample(speaker,student):
a =''
def __init__(self,n,a,w,g,t):
student.__init__(self,n,a,w,g)
speaker.__init__(self,n,t)
test = sample("Tim",25,80,4,"Python")
test.speak()#方法名同,默认调用的是在括号中排前地父类的方法
#http://www.w3cschool.cc/python3/python3-stdlib.html
import os
print(os.getcwd())
print(dir(os))
#print(help(os))
import glob
print(glob.glob('*.py'))
import math
print(math.cos(math.pi / 4))
import random
print(random.choice(['apple', 'pear', 'banana']))
print( random.sample(range(100), 10)) # sampling without replacement
print(random.random()) # random float
print(random.randrange(6)) # random integer chosen from range(6)
from datetime import date
now = date.today()
print(now)