Pythonº¯ÊýºÍÃæÏò¶ÔÏó,С°×¿´Á˶¼ËµºÃ
python³ýÁËʹÓÃÄÚÖú¯ÊýÒÔÍâ,»¹¿ÉÒÔ×Ô¶¨Ò庯Êý¡£ÎÒÃÇΪʲôҪ¶¨ÒåºÍʹÓú¯ÊýÄØ?
ÏÂÃæÎÒ¾Ù¸öÀý×Ó,±ÈÈçÎÒÏëÇó10µÄ½×³Ë
m = 1
for i in range(1, 11):
m *= i
print(m)
´úÂëËäÈ»²»³¤,µ«ÊÇÈç¹ûÎÒ»¹ÏëÇó20,21,55,100µÄ½×³ËÄØ?°´ÕÕÉÏÃæµÄ·½Ê½Ç󼸸öÊýµÄ½×³Ë¾ÍҪд¼¸¸öÑ»·,ÕâÑùÎÒÃÇÒ²Äܵõ½ÏëÒªµÄ´ð°¸,µ«´úÂëÊDz»ÊÇÏԵá¯ÄÑ¿´¡®¡£ÕâʱºòÎÒÃǾͿÉÒÔ¶¨ÒåÒ»¸öº¯Êý,ÒÔºóÇó½×³ËÖ»ÐèÒªµ÷ÓÃÕâ¸öº¯Êý¾ÍÐÐÁË¡£
def fac(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
fac(8)
fac(25)
fac(63)
fac(100)
ÊDz»ÊǸоõºÜ·½±ãÄØ?½ÓÏÂÀ´¸ø´ó¼Ò½éÉÜһϺ¯ÊýÓ÷¨
¶¨Ò庯Êý
¶¨ÒåÒ»¸öpythonº¯Êý,ÏÂÃæÊǺ¯ÊýµÄ»ù±¾ÐÎʽ:
def º¯ÊýÃû(²ÎÊý1,²ÎÊý2,¡,²ÎÊýn):
? º¯ÊýÌå
? return ·µ»ØÖµ
ÎÒÃÇ¿ÉÒÔ¿´µ½pythonÓÃdefÀ´¶¨ÒåÒ»¸öº¯Êý,ºóÃæÓк¯ÊýÃûºÍÓ¢ÎÄÀ¨ºÅ(À¨ºÅÀïÃæ¿ÉÒÔ¶¨ÒåһЩ²ÎÊý,²ÎÊý¿ÉÒÔ¸ù¾Ý¾ßÌåÐèÇóÉèÖÃ,Ò²¿ÉÒÔûÓвÎÊý,²ÎÊý»¹¿ÉÒÔÉèÖÃĬÈÏÖµ)¡£ÏÂÃæreturn¿ÉÒÔ·µ»ØÒ»¸öÖµ,Õâ¸öÖµÓÐʱÈκÎÐÎʽµÄÊý¾Ý,µ±È»Ò²¿ÉÒÔûÓзµ»ØÖµ¡£
½ÓÏÂÀ´ÊÇһЩʵÀý:
ʵÀý1:
def hello_world():
return 'hello, world'
print(hello_world())
ʵÀý2:
def hello(name):
print(f'ÄãºÃ,{name}')
hello('°ÙÀïÊØÔ¼')
ʵÀý3:
def add(nums):
sum = 0
for n in nums:
sum += n
return sum
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(add(nums))
º¯ÊýµÄ·µ»ØÖµ
return:·µ»ØÖµ,Èç¹ûûÓÐÔòĬÈÏ·µ»ØNone ʵÀý:
def num() :
s = 10
return s
def num2():
s = 10
a = num()
b = num2()
print(a, b)
ʵÀý:
s = 'hello, ÕÅÈý'
return s
def hello2():
print('hello, jack')
a = hello()
print(a)
b = hello2()
º¯Êý²ÎÊý
º¯Êý¿ÉÒÔÉèÖòÎÊý,Ò²¿ÉÒÔ²»ÉèÖòÎÊý,¿ÉÒÔÉèÖÃÒ»¸ö²ÎÊý,Ò²¿ÉÒÔÉèÖöà¸ö²ÎÊý,ͬʱº¯ÊýÒ²¿ÉÒÔÉèÖÃĬÈÏÖµ¡£
ʵÀý:
import random
def roll_dice(n=2):
"""Ò¡É«×Ó
:param n: É«×Ó¸öÊý,ĬÈÏÁ½¸öÉ«×Ó
:return: ·µ»ØÉ«×ÓµÄ×ܵãÊý
"""
points = 0
for _ in range(n):
points += random.randrange(1, 7)
return points
print(roll_dice())
print(roll_dice(3))
ʵÀý:
def BMI(high, weight, sex):
"""
ÌåÖÊÖ¸Êý¼ÆËã
:param high:Éí¸ß ,µ¥Î»:Ã×
:param weight:ÌåÖØ,µ¥Î»:ǧ¿Ë¡¢¹«½ï
:param sex:ÐÔ±ð,ÄÐ:1,Å®:0
:return: ·µ»ØÌåÖÊÖ¸Êý
"""
bmi = weight / high ** 2
if sex == 0:
bmi -= 1.1
if bmi < 19:
print('ÌåÖØÆ«Çá')
elif 19 <= bmi < 25:
print('½¡¿µÌåÖØ')
elif 25 <= bmi < 30:
print('³¬ÖØ')
elif 30 <= bmi < 39:
print('ÑÏÖس¬ÖØ')
else:
print('¼«¶È³¬ÖØ')
return weight / high ** 2
print(BMI(1.6, 50, 1))
´ËʱÎÒÃÇÔÚÉèÖÃÁËÈý¸ö²ÎÊý,µ±µ÷Óú¯Êýʱ,ÎÒÃÇÒ²±ØÐë´«ÈëÈý¸ö²ÎÊý,ÊäÈë²ÎÊý µÄλÖÃĬÈϺÍÉèÖòÎÊýµÄλÖÃÒ»Ö¡£Èç¹û²ÎÊýÓÐĬÈÏÖµ,Ôòµ÷Óú¯Êýʱ,´øÓÐĬÈÏÖµµÄ²ÎÊý¿ÉÒÔ²»±ØÊäÈë¡£
ʵÀý
def sum(a, b):
"""
¼Ó·¨º¯Êý,½«aºÍbÏà¼Ó
:param a: ¼ÓÊý
:param b: ¼ÓÊý
:return: ·µ»ØÏà¼ÓÖ®ºÍ
"""
return a+b
print(sum(1, 2))
ʵÀý:
def sum(a, b, c, d, e):
"""
¼Ó·¨º¯Êý,½«a, b, c, d, eÏà¼Ó
:return: ·µ»ØÏà¼ÓÖ®ºÍ
"""
return a+b+c+d+e
print(sum(1, 2, 3, 4, 5))
µ±ÊäÈëµÄ²ÎÊý¹ý¶à,²¢ÇÒ²ÎÊýµÄÊôÐÔÒ»ÖÂʱ,ÎÒÃÇ¿ÉÒÔʹÓÿɱä²ÎÊý
¿É±ä²ÎÊý
def sum(*a):
"""
¼Ó·¨º¯Êý
:param a: ±íʾһ¸ö¿É±ä²ÎÊý,´æ´¢ÈÎÒâ¶à²ÎÊý,´ËʱaΪһ¸öÔª×é
:return: ·µ»ØÏà¼ÓÖ®ºÍ
"""
m = 0
for i in a:
m += i
return m
print(sum(1, 2, 3))
print(sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
ÔÚ²ÎÊýÇ°Ãæ¼Ó*ºÅ,´Ëʱ²ÎÊýΪ¿É±ä²ÎÊý¡£¿É±ä²ÎÊýΪһ¸öÔª×é,½«ËùÓжàÓàµÄ²ÎÊýÈ«²¿´æÔÚÀïÃæ¡£
ͬʱÎÒÃÇ¿ÉÒÔÔÚ²»Í¬µÄÏîÄ¿Öе÷Óú¯Êý ʵÀý:
module1.py
def hello():
print('hello, world')
module2.py
def hello():
print('goodbye, world')
test.py
import module1
import module2
module1.hello()
module2.hello()
ÔÚµ¼ÈëÄ£¿éʱ,»¹¿ÉÒÔʹÓÃas ¹Ø¼ü×Ö¶ÔÄ£¿é½øÐбðÃû,ÕâÑùÎÒÃÇ¿ÉÒÔʹÓøüΪ¼ò¶ÌµÄÍêÈ«ÏÞ¶¨Ãû¡£
import module1 as m1
import module2 as m2
m1.hello()
m2.hello()
Èç¹ûÎÒÃÇÓÃfrom¡import¡µÄÐÎʽ,ÎÒÃDZØÐëʹÓÃas¹Ø¼ü×Ö¶Ôµ¼È뺯Êý½øÐбðÃû,²»È»Á½¸öÏàͬµÄhelloº¯Êý,ÆäÖÐÒ»¸ö»á±»¸²¸Ç¡£
from module1 import hello as h1
from module2 import hello as h2
h1()
h2()
±ê×¼¿âÖеÄÄ£¿éºÍº¯Êý
º¯Êý | ˵Ã÷ |
---|
abs | ·µ»ØÒ»¸öÊýµÄ¾ø¶ÔÖµ,ÀýÈç:abs(-1.3) »á·µ»Ø1.3 ¡£ | bin | °ÑÒ»¸öÕûÊýת»»³ÉÒÔ'0b' ¿ªÍ·µÄ¶þ½øÖÆ×Ö·û´®,ÀýÈç:bin(123) »á·µ»Ø'0b1111011' ¡£ | chr | ½«Unicode±àÂëת»»³É¶ÔÓ¦µÄ×Ö·û,ÀýÈç:chr(8364) »á·µ»Ø'€' ¡£ | hex | ½«Ò»¸öÕûÊýת»»³ÉÒÔ'0x' ¿ªÍ·µÄÊ®Áù½øÖÆ×Ö·û´®,ÀýÈç:hex(123) »á·µ»Ø'0x7b' ¡£ | input | ´ÓÊäÈëÖжÁÈ¡Ò»ÐÐ,·µ»Ø¶Áµ½µÄ×Ö·û´®¡£ | len | »ñÈ¡×Ö·û´®¡¢ÁбíµÈµÄ³¤¶È¡£ | max | ·µ»Ø¶à¸ö²ÎÊý»òÒ»¸ö¿Éµü´ú¶ÔÏó(ºóÃæ»á½²)ÖеÄ×î´óÖµ,ÀýÈç:max(12, 95, 37) »á·µ»Ø95 ¡£ | min | ·µ»Ø¶à¸ö²ÎÊý»òÒ»¸ö¿Éµü´ú¶ÔÏó(ºóÃæ»á½²)ÖеÄ×îСֵ,ÀýÈç:min(12, 95, 37) »á·µ»Ø12 ¡£ | oct | °ÑÒ»¸öÕûÊýת»»³ÉÒÔ'0o' ¿ªÍ·µÄ°Ë½øÖÆ×Ö·û´®,ÀýÈç:oct(123) »á·µ»Ø'0o173' ¡£ | open | ´ò¿ªÒ»¸öÎļþ²¢·µ»ØÎļþ¶ÔÏó(ºóÃæ»á½²)¡£ | ord | ½«×Ö·ûת»»³É¶ÔÓ¦µÄUnicode±àÂë,ÀýÈç:ord('€') »á·µ»Ø8364 ¡£ | pow | ÇóÃÝÔËËã,ÀýÈç:pow(2, 3) »á·µ»Ø8 ;pow(2, 0.5) »á·µ»Ø1.4142135623730951 ¡£ | print | ´òÓ¡Êä³ö¡£ | range | ¹¹ÔìÒ»¸ö·¶Î§ÐòÁÐ,ÀýÈç:range(100) »á²úÉú0 µ½99 µÄÕûÊýÐòÁС£ | round | °´ÕÕÖ¸¶¨µÄ¾«¶È¶ÔÊýÖµ½øÐÐËÄÉáÎåÈë,ÀýÈç:round(1.23456, 4) »á·µ»Ø1.2346 ¡£ | sum | ¶ÔÒ»¸öÐòÁÐÖеÄÏî´Ó×óµ½ÓÒ½øÐÐÇóºÍÔËËã,ÀýÈç:sum(range(1, 101)) »á·µ»Ø5050 ¡£ | type | ·µ»Ø¶ÔÏóµÄÀàÐÍ,ÀýÈç:type(10) »á·µ»Øint ;¶øtype('hello') »á·µ»Østr ¡£ |
º¯Êý½ø½×
λÖòÎÊýºÍ¹Ø¼ü×Ö²ÎÊý
def add(a,b,c):
µ±ÎÒÏëµ÷Óú¯Êýaddʱ,Èçadd(1,2,3),´Ëʱa = 1,b = 2,c = 3,Ò²¾ÍÊÇ˵,´«ÈëµÄ²ÎÊýÖµÓëÔÀ´º¯ÊýµÄ²ÎÊýλÖÃÒ»Ò»¶ÔÓ¦¡£´Ëʱ²ÎÊý¶¼ÊÇλÖòÎÊý,µ±È»,ÎÒÃÇÒ²¿ÉÒÔͨ¹ý²ÎÊýÃû=²ÎÊýÖµµÄ·½Ê½½øÐд«²Î,Èçadd(a=1,c=3,b=2),ÒòΪ´«Èë²ÎÊýʱ´øÉÏÁ˲ÎÊýÃû,ËùÒÔ²ÎÊýλÖÿÉÒÔËæÒâ¸Ä±ä¡£
Èç¹ûÎÒÃÇÏ£Íûµ÷Óú¯Êýʱ±ØÐëÒÔ²ÎÊýÃû=²ÎÊýÖµµÄ¸ñʽ½øÐд«²Î,ÎÒÃǶԺ¯Êý²ÎÊý×öÈçϸıä:
def add(*,a,b,c):
Èç´Ë,Èç¹ûÏëµ÷ÓÃaddº¯Êý,±ØÐëÒªÒÔ²ÎÊýÃû=²ÎÊýÖµµÄ¸ñʽ½øÐд«²Î,·ñÔò¾Í»áÒý·¢Òì³£,ÕâʱºòµÄ²ÎÊýÎÒÃdzÆ֮Ϊ¹Ø¼ü×Ö²ÎÊý¡£
²ÎÊýÐÎʽ | ²ÎÊýÀàÐÍ | ʹÓÃʵÀý |
---|
def add(a, b, c) | a¡¢b¡¢c ¶¼ÊÇλÖòÎÊý | add(1, 2, 3) | def add(*, a, b, c) | a¡¢b¡¢c ¶¼Êǹؼü×Ö²ÎÊý | add( a = 1, b = 2, c = 3) | def add(a, *, b, c) | a ÊÇλÖòÎÊý,b¡¢c Êǹؼü×Ö²ÎÊý | add( 1, b = 2, c = 3) |
¼òµ¥À´Ëµ,* Ç°ÃæµÄ²ÎÊý¶¼ÊÇλÖòÎÊý, * ºóÃæµÄ²ÎÊý¶¼Êǹؼü×Ö²ÎÊý
Ç°ÃæÎÒÃÇ˵µ½¿É±ä²ÎÊý ( *²ÎÊýÃû )¿ÉÒÔÈÝÄÉÈÎÒâ¶àµÄ²ÎÊý¸öÊý,ÎÒÃÇÔÚÉè¼Æº¯Êýʱ,Èç¹û¼È²»ÖªµÀµ÷ÓÃÕ߻ᴫÈëµÄ²ÎÊý¸öÊý,Ò²²»ÖªµÀµ÷ÓÃÕ߻᲻»áÖ¸¶¨²ÎÊýÃû,ÄÇôͬʱʹÓÿɱä²ÎÊýºÍ¹Ø¼ü×Ö²ÎÊý¡£¹Ø¼ü×Ö²ÎÊý»á½«´«ÈëµÄ´ø²ÎÊýÃûµÄ²ÎÊý×é×°³ÉÒ»¸ö×Öµä,²ÎÊýÃû¾ÍÊÇ×ÖµäÖмüÖµ¶ÔµÄ¼ü,¶ø²ÎÊýÖµ¾ÍÊÇ×ÖµäÖмüÖµ¶ÔµÄÖµ
def calc(*args, **kwargs):
*args Ϊ¿É±ä²ÎÊý,·µ»ØµÄÊÇÒ»¸öÔª×é **kwargs Ϊ¿É±äµÄ¹Ø¼ü×Ö²ÎÊý,·µ»ØµÄÊÇÒ»¸ö×Öµä
ʵÀý:
def sum(*args, **kwargs):
result = 0
for arg in args:
result += arg
for value in kwargs.values():
result += value
return result
print(sum(1, 3, 5))
print(sum(a = 1, b = 4, c = 3))
print(sum(1, 5, c = 7, d = 3))
Éè¼Æº¯Êýʱ,λÖòÎÊý±ØÐëÔڹؼü×Ö²ÎÊýÇ°Ãæ;ûÓÐĬÈÏÖµµÄ²ÎÊý±ØÐëÔÚÓÐĬÈÏÖµµÄ²ÎÊýÇ°Ãæ¡£
¸ß¼¶º¯Êý
º¯Êý±¾ÉíÒ²¿ÉÒÔ×÷Ϊº¯ÊýµÄ²ÎÊý»òÕß·µ»ØÖµ,ÕâÑùµÄº¯ÊýÎÒÃdzÆ֮Ϊ¸ß¼¶º¯Êý¡£
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def mul(num1, num2):
return num1 * num2
def div(num1, num2):
return num1 / num2
def sum(*args, start, ft, **kwargs):
"""
¸ß¼¶º¯Êý,×ÔÓÉʵÏÖ¶à¸öÊýµÄËÄÔòÔËËã
start:¿ªÊ¼Öµ,¼Ó·¨ÔËËãʱΪ0,¼õ·¨¡¢³Ë·¨¡¢³ý·¨ÔËËãʱΪ0
ft:º¯ÊýÃû
"""
result = start_value
for arg in args:
result = ft(result, arg)
for value in kwargs.values():
result = ft(result, value)
return result
print(sum(1, 3, 5, start = 0, ft = add, a = 1, b = 4, c = 3))
print(sum(1, 3, 5, start = 1, ft = sub, a = 1, b = 4, c = 3))
print(sum(1, 3, 5, start = 1, ft = mul, a = 1, b = 4, c = 3))
print(sum(1, 3, 5, start = 1, ft = div, a = 1, b = 4, c = 3))
´Ëʱº¯ÊýsumÒѾ²»¾ÖÏÞÓÚÒ»¸ö¼Ó·¨º¯Êý,¶øÊÇ¿ÉÒÔÇáËÉ¿ÉÒÔ½øÐÐËÄÔòÔËËãÀïÃæÈκÎÒ»¸ö¼ÆË㷽ʽ¡£ÐèҪעÒâµÄÊÇ,½«º¯Êý×÷Ϊ²ÎÊýºÍµ÷Óú¯ÊýÊÇÓÐÏÔÖøµÄÇø±ðµÄ,µ÷Óú¯ÊýÐèÒªÔÚº¯ÊýÃûºóÃæ¸úÉÏÔ²À¨ºÅ,¶ø°Ñº¯Êý×÷Ϊ²ÎÊýʱֻÐèÒªº¯ÊýÃû¼´¿É¡£ÎÒÃÇ»¹¿ÉÒÔÖ±½Óµ÷python±ê×¼¿âÖÐoperatorÄ£¿é,Ëü¿ÉÒÔÌṩ¼Ó·¨³Ë·¨ÔËËã,ÎÒÃÇÖ±½ÓʹÓü´¿É¡£
Lambdaº¯Êý(ÄäÃûº¯Êý)
Lambdaº¯ÊýÓÖ½ÐÄãÃǺ¯ÊýÊÇÒ»ÖÖûÓк¯ÊýÃûµÄº¯Êý
±í´ïʽ ------- lambda ²ÎÊý1 , ¡, ²ÎÊýn : Ö´ÐÐÌå
Ö´ÐÐÌåͨ³£ÊÇÒ»¸ö±í´ïʽ,Æä·µ»Øֵͨ³£¾ÍÊÇÔËËã½á¹û
ʵÀý:
def sum(*args, start=0, ft=lambda x, y: x + y, **kwargs):
result = start
for arg in args:
result = ft(result, arg)
for value in kwargs.values():
result = ft(result, value)
return result
print(sum(1, 3, 5, x = 7, y = 9))
print(sum(1, 3, 5, x = 7, y = 9, start = 1, ft = lambda x, y: x * y))
ÃæÏò¶ÔÏó
Ê×Ïȸø´ó¼Ò½éÉܼ¸¸öÖØÒªµÄ¸ÅÄî¡£
-
Àà¡¢¶ÔÏó ÀàÊÇÒ»¸ö³éÏóµÄ¸ÅÄî,¶ÔÏóÊÇÀàµÄ¾ßÌåʵÏÖ¡£ °ÑÊôÓÚͬһ¸öÀàµÄ¹²Í¬ÌØÕ÷ÌáÈ¡³öÀ´,ÎÒÃǾ͵õ½Ò»¸öÀà,±ÈÈçÈËÊÇÒ»¸öÀà,СÃ÷¡¢ÕÅÈý¡¢ÀîËľÍÊÇÒ»¸ö¾ßÌåµÄ¶ÔÏó; ÌìÌåÊÇÒ»¸öÀà,ÄÇôµØÇò¡¢»ðÐÇ¡¢ÔÂÇò¾ÍÊÇÒ»¸ö¾ßÌåµÄ¶ÔÏó¡£ -
·â×°¡¢¼Ì³Ð¡¢¶à̬ ·â×°:°ÑÊý¾ÝºÍ²Ù×÷Êý¾ÝµÄº¯Êý´ÓÂß¼ÉÏ×é×°³ÉÒ»¸öÕûÌå(¶ÔÏó)¡£Òþ²ØʵÏÖϸ½Ú,±©Â¶¼òµ¥µÄµ÷Óýӿڡ£ ¼Ì³Ð:À©Õ¹ÒÑÓеÄÀà´´½¨ÐÂÀà,ʵÏÖ¶ÔÒÑÓÐÀàµÄ´úÂ븴Óᣠ¶à̬:¸ø²»Í¬µÄ¶ÔÏó·¢³öͬÑùµÄÏûÏ¢,²»Í¬µÄ¶ÔÏóÖ´ÐÐÁ˲»Í¬µÄÐÐΪ¡£
¼òµ¥¸ÅÀ¨Ò»ÏÂÃæÏò¶ÔÏó±à³Ì¾ÍÊÇ:°ÑÒ»×éÊý¾ÝºÍ´¦ÀíÊý¾ÝµÄ·½·¨×é³É¶ÔÏó,°ÑÐÐΪÏàͬµÄ¶ÔÏó¹éÄÉΪÀà,ͨ¹ý·â×°Òþ²Ø¶ÔÏóµÄÄÚ²¿Ï¸½Ú,ͨ¹ý¼Ì³ÐʵÏÖÀàµÄÌØ»¯ºÍ·º»¯,ͨ¹ý¶à̬ʵÏÖ»ùÓÚ¶ÔÏóÀàÐ͵Ķ¯Ì¬·ÖÅÉ¡£ÊÇÒ»ÖÖÖØÒªµÄ±à³Ì˼άºÍ·½Ê½¡£
¶¨ÒåÀà ¼ÓÉÏÀàÃûÀ´¶¨ÒåÀà,ͨ¹ýËõ½øÎÒÃÇ¿ÉÒÔÈ·¶¨ÀàµÄ´úÂë¿é,¾ÍÈçͬ¶¨Ò庯ÊýÄÇÑù¡£ÔÚÀàµÄ´úÂë¿éÖÐ,ÎÒÃÇÐèҪдһЩº¯Êý,ÎÒÃÇ˵¹ýÀàÊÇÒ»¸ö³éÏó¸ÅÄî,ÄÇôÕâЩº¯Êý¾ÍÊÇÎÒÃǶÔÒ»Àà¶ÔÏó¹²Í¬µÄ¶¯Ì¬ÌØÕ÷µÄÌáÈ¡¡£Ð´ÔÚÀàÀïÃæµÄº¯ÊýÎÒÃÇͨ³£³Æ֮Ϊ·½·¨,·½·¨¾ÍÊǶÔÏóµÄÐÐΪ,Ò²¾ÍÊǶÔÏó¿ÉÒÔ½ÓÊÕµÄÏûÏ¢¡£·½·¨µÄµÚÒ»¸ö²ÎÊýͨ³£¶¼ÊÇself ,Ëü´ú±íÁ˽ÓÊÕÕâ¸öÏûÏ¢µÄ¶ÔÏó±¾Éí¡£
class Student:
def study(self, name, course):
print(f'{name}ÕýÔÚѧϰ{course}¡£')
def play(self, name, game):
print(f'{name}ÕýÔÚÍæ{game}')
´´½¨ºÍʹÓöÔÏó
class Student:
def study(self, course):
print(f'ѧϰ{course}¡£')
def play(self, name, game):
print(f'Íæ{game}')
if __name__ == '__main__':
student1 = Student()
student2 = Student()
student1.study('ÓïÎÄ')
student2.study('Êýѧ')
student1.play('Á¬Á¬¿´')
student1.play('ÍÆÏä×Ó')
³õʼ»¯·½·¨
ÎÒÃÇ¿ÉÒÔͨ¹ý¸øStudentÀàÌí¼Ó__init__ ·½·¨µÄ·½Ê½ÎªÑ§Éú¶ÔÏóÖ¸¶¨ÊôÐÔ,ͬʱÍê³É¶ÔÊôÐÔ¸³³õʼֵµÄ²Ù×÷,Õâ¸ö·½·¨·½·¨Í¨³£Ò²±»³ÆΪ³õʼ»¯·½·¨¡£ÎÒÃǶÔÉÏÃæµÄStudentÀàÐÞ¸Ä,¸øѧÉú¶ÔÏóÌí¼Óname(ÐÕÃû)ºÍage(ÄêÁä)Á½¸öÊôÐÔ¡£
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def study(self, course):
print(f'{self.name}ѧϰ{course}¡£')
def play(self, game):
print(f'{self.name}Íæ{game}')
if __name__ == '__main__':
student1 = Student('ÕÅÈý', 15)
student2 = Student('ÀîËÄ', 16)
student1.study('ÓïÎÄ')
student2.study('Êýѧ')
student1.play('Á¬Á¬¿´')
student1.play('ÍÆÏä×Ó')
ÔÚPythonÖÐ,ÒÔÁ½¸öÏ»®Ïß¿ªÍ·ºÍ½áβµÄ·½·¨,ÎÒÃÇÒ»°ã³Æ֮ΪħÊõ·½·¨»òħ·¨·½·¨¡£Í¨¹ý__repr__ ħÊõ·½·¨,Èç¹ûÎÒÃÇÔÚ´òÓ¡¶ÔÏóµÄʱºò¿´µ½µÄÊÇÎÒÃÇ×Ô¶¨ÒåµÄÐÅÏ¢,¸Ã·½·¨·µ»ØµÄ×Ö·û´®¾ÍÊÇÓÃprint º¯Êý´òÓ¡¶ÔÏóµÄʱºò»áÏÔʾµÄÄÚÈÝ¡£
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def study(self, course):
print(f'{self.name}ѧϰ{course}¡£')
def play(self, game):
print(f'{self.name}Íæ{game}')
def __repr__(self):
return f'{self.name}: {self.age}'
if __name__ == '__main__':
student1 = Student('ÕÅÈý', 15)
print(student1)
students = [student1, Student('ÀÏÀîËÄ', 16), Student('ÍõÎå', 25)]
print(students)
С³ÌÐò:
class Clock:
"""ʱ¼ä"""
def __init__(self, hour=0, minute=0, second=0):
self.second = second
self.minute = minute
self.hour = hour
def run(self):
self.second += 1
if self.second == 60:
self.second = 0
self.minute += 1
if self.minute == 60:
self.minute = 0
self.hour += 1
if self.hour == 24:
self.hour = 0
def show(self):
return f'{self.hour:0>2d}:{self.minute:0>2d}:{self.second:0>2d}'
if __name__ == '__main__':
clock = Clock(23, 59, 50)
while True:
os.system('cls')
print(clock.show())
time.sleep(1)
clock.run()
ºóÐø»¹»á¼ÌÐø¸üÐÂ,¸ÐÐËȤµÄ»ï°é¿ÉÒÔÁôÒâ
|