class Pen(object):
count = 0
def __init__(self, name, len):
self.name = name
self.len = len
Pen.count += 1
red_pen = Pen('紅筆', 5)
black_pen = Pen('黑筆', 6)
print(Pen.count)
count = 0
def __init__(self, name, len):
self.name = name
self.len = len
Pen.count += 1
red_pen = Pen('紅筆', 5)
black_pen = Pen('黑筆', 6)
print(Pen.count)
2024-04-29
class animal(object):
def __init__(self, name, color):
self.name = name
self.color = color
dog = animal('阿黃', '金色')
cat = animal('阿歡', '灰色')
print(dog.name)
def __init__(self, name, color):
self.name = name
self.color = color
dog = animal('阿黃', '金色')
cat = animal('阿歡', '灰色')
print(dog.name)
2024-04-28
def f(x):
return pow(x, 0.5)
for item in map(f, range(1, 101)):
if item == int(item):
print(int(item))
def z(item):
if item == int(item):
return print(int(item))
for item in filter(z, [item]):
print(item)
return pow(x, 0.5)
for item in map(f, range(1, 101)):
if item == int(item):
print(int(item))
def z(item):
if item == int(item):
return print(int(item))
for item in filter(z, [item]):
print(item)
2024-04-23
from functools import reduce
def f(x, y):
return x * y
print(int(reduce(f, [1.0, 3.0, 5.0, 7.0, 9.0])))
def f(x, y):
return x * y
print(int(reduce(f, [1.0, 3.0, 5.0, 7.0, 9.0])))
2024-04-22
import math
def add(a, b, f):
return f(a) + f(b)
print(int(add(1, 4, math.sqrt)))
def add(a, b, f):
return f(a) + f(b)
print(int(add(1, 4, math.sqrt)))
2024-04-22
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
print(count())
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
print(count())
2024-04-03
兩層不就夠了
from functools import reduce
def cale_prod(list1):
def ji(x,y):
return x*y
return reduce(ji,list1)
list1 = list(map(int, input().split(',')))
print(cale_prod(list1))
from functools import reduce
def cale_prod(list1):
def ji(x,y):
return x*y
return reduce(ji,list1)
list1 = list(map(int, input().split(',')))
print(cale_prod(list1))
2024-04-03
f = open('6.txt', 'a+')
f.seek(0)
content = f.readlines()
f.seek(2)
f.writelines('\n')
f.writelines(content)
f.close()
這樣比較合理
f.seek(0)
content = f.readlines()
f.seek(2)
f.writelines('\n')
f.writelines(content)
f.close()
這樣比較合理
2024-03-29
優(yōu)化一下整數(shù)
def __str__(self):
g = gcd(self.zi, self.mu) #進(jìn)行約分
if self.mu/g != 1:
return '{}/{}'.format(int(self.zi/g), int(self.mu/g))
else:
return '{}'.format(int(self.zi/g))
def __str__(self):
g = gcd(self.zi, self.mu) #進(jìn)行約分
if self.mu/g != 1:
return '{}/{}'.format(int(self.zi/g), int(self.mu/g))
else:
return '{}'.format(int(self.zi/g))
2024-03-27
完結(jié),代碼分享
https://github.com/saysky/python_demo
https://github.com/saysky/python_demo
2024-03-18
# Enter a code
class Animal(object):
__age = 0
def __init__(self, name, age):
self.__age = age
self.name = name
dog = Animal('Cgo', 20)
print(dog.age)
class Animal(object):
__age = 0
def __init__(self, name, age):
self.__age = age
self.name = name
dog = Animal('Cgo', 20)
print(dog.age)
2023-12-27