º¯ÊýµÄ¶¨Òå
º¯Êý¾ÍÊǶÔʵÏÖijһÌض¨¹¦ÄܵĴúÂëµÄ·â×°
º¯ÊýµÄ·ÖÀà
- ϵͳº¯Êý
ϵͳ(ÓïÑÔϵͳ)ÒѾÉùÃ÷ºÃ,³ÌÐòÔ±¿ÉÒÔÖ±½ÓʹÓõĺ¯Êý¡£
- ×Ô¶¨Ò庯Êý
³ÌÐòÔ±×Ô¼ºÉùÃ÷µÄº¯Êý¡£
º¯ÊýµÄÉùÃ÷
ÔÚPythonÖпÉÒÔʹÓÃdef ¹Ø¼ü×ÖÀ´¶¨Ò庯Êý,º¯ÊýµÄÃüÃû¹æÔò¸ú±äÁ¿µÄÃüÃû¹æÔòÊÇÒ»Öµġ£
def fac(num):
"""Çó½×³Ë"""
result = 1
for i in range(2, num + 1):
result *= i
return result
˵Ã÷:Python±ê×¼¿âµÄmath Ä£¿éÖÐÓÐÒ»¸öÃûΪfactorial µÄº¯ÊýÒѾʵÏÖÁËÇó½×³ËµÄ¹¦ÄÜ,ÎÒÃÇ¿ÉÒÔÖ±½ÓʹÓøú¯ÊýÀ´¼ÆËã½×³Ë¡£
º¯ÊýµÄ²ÎÊý
ÔÚ¶¨Ò庯Êýʱ,¿ÉÒÔÓвÎÊý,Ò²¿ÉÒÔ²»¼Ó²ÎÊý,²ÎÊý»¹¿ÉÒÔÖ¸¶¨Ä¬ÈÏÖµ,Èç¹ûûÓд«Èë¸Ã²ÎÊýµÄÖµ,¾ÍʹÓÃĬÈÏÖµ¡£
def add(a=0, b=0, c=0):
"""Èý¸öÊýÏà¼ÓÇóºÍ"""
return a + b + c
print(add())
print(add(1))
print(add(1, 2))
print(add(1, 2, 3))
print(add(c=50, a=100, b=200))
×¢Òâ:´øĬÈÏÖµµÄ²ÎÊý±ØÐë·ÅÔÚ²»´øĬÈÏÖµµÄ²ÎÊýÖ®ºó,·ñÔò½«²úÉúSyntaxError ´íÎó,´íÎóÏûÏ¢ÊÇ:non-default argument follows default argument ,·Òë³ÉÖÐÎĵÄÒâ˼ÊÇ¡°Ã»ÓÐĬÈÏÖµµÄ²ÎÊý·ÅÔÚÁË´øĬÈÏÖµµÄ²ÎÊýºóÃ桱¡£
¿É±ä²ÎÊý
ʹÓÿɱä²ÎÊý¿ÉÒÔʵÏÖÏòÒ»¸öº¯ÊýÖд«0¸ö»òÈÎÒâ¶à¸ö²ÎÊý,PythonÖпÉÒÔͨ¹ý* ±í´ïʽÓï·¨À´Ö§³Ö¿É±ä²ÎÊý¡£ÏÂÃæµÄ´úÂëÑÝʾÁËÓÿɱä²ÎÊýʵÏÖ¶ÔÈÎÒâ¶à¸öÊýÇóºÍµÄadd º¯Êý¡£
def add(*args):
total = 0
for arg in args:
if type(arg) in (int, float):
total += arg
return total
print(add())
print(add(1, 2))
print(add(1, 2, 3))
ÓÃÄ£¿é¹ÜÀíº¯Êý
º¯ÊýÃüÃû³åÍ»µÄ³¡¾°:ÔÚͬһ¸ö.py ÎļþÖж¨ÒåÁËÁ½¸öͬÃûµÄº¯Êý
def say_hello():
print('hello,world')
def say_hello():
print('goodbye,world')
say_hello()
PythonÖÐÿ¸öÎļþ¾Í´ú±íÁËÒ»¸öÄ£¿é(module),ÔÚ²»Í¬Ä£¿é¿ÉÒÔÓÐͬÃûµÄº¯Êý,ÔÚʹÓú¯Êýʱ,ͨ¹ýimport ¹Ø¼ü×Öµ¼ÈëÖ¸¶¨µÄÄ£¿éÔÙʹÓÃÍêÈ«ÏÞ¶¨ÃûµÄµ÷Ó÷½Ê½¾Í¿ÉÒÔÇø·ÖÊÇÄĸöÄ£¿éÖеÄsay_hello º¯Êý¡£´úÂëÈçÏÂ:
module1.py
def say_hello():
print('hello,world')
module2.py
def say_hello():
print('goodbye,world')
test.py
import module1
import module2
module1.say_hello()
module2.say_hello()
ÔÚµ¼ÈëÄ£¿éʱ,»¹¿ÉÒÔʹÓùؼü×Öas ¶ÔÄ£¿éÈ¡±ðÃû¡£
test.py
import module1 as m1
import module2 as m2
m1.say_hello()
m2.say_hello()
ÉÏÃæµÄ´úÂëÎÒÃǵ¼ÈëÁ˶¨Ò庯ÊýµÄÄ£¿é,ÎÒÃÇÒ²¿ÉÒÔʹÓÃfrom...import... Óï·¨´ÓÄ£¿éÖÐÖ±½Óµ¼ÈëÐèҪʹÓõĺ¯Êý,´úÂëÈçÏÂËùʾ¡£
test.py
from module1 import say_hello
say_hello()
from module2 import say_hello
say_hello()
Èç¹ûÎÒÃÇÈç¹û´ÓÁ½¸ö²»Í¬µÄÄ£¿éÖе¼ÈëÁËͬÃûµÄº¯Êý,ºóµ¼ÈëµÄº¯Êý»á¸²¸ÇµôÏÈÇ°µÄµ¼Èë,¾ÍÏñÏÂÃæµÄ´úÂëÖÐ,µ÷ÓÃsay_hello »áÊä³öhello, world! ,ÒòΪÎÒÃÇÏȵ¼ÈëÁËmodule2 µÄsay_hello ,ºóµ¼ÈëÁËmodule1 µÄsay_hello ¡£
from module2 import say_hello
from module1 import say_hello
say_hello()
Èç¹ûҪͬʱʹÓÃÀ´×ÔÁ½¸öÄ£¿éµÄsay_hello º¯Êý,Óùؼü×Ö¶Ôµ¼ÈëµÄº¯ÊýÈ¡±ðÃû¡£
from module1 import say_hello as f1
from module2 import say_hello as f2
f1()
f2()
²»Í¬Îļþ¼Ðϲ»Í¬Ä£¿éÖ®¼äµÄµ÷ÓÃ
н¨Ò»¸ö°ü,°üÃûΪdemo ,ÔÚ°üÀïн¨.py Îļþ,дÈëÒÔÏ´úÂë
homework5.py
"""
homework5 - Éè¼Æº¯Êý,´«ÈëÒ»¸öÁбí(ÁбíÖÐÊÇÒ»×éÑù±¾Êý¾Ý),
¼ÆËãÑù±¾Êý¾ÝµÄ¾ùÖµ¡¢ÖÐλÊý¡¢¼«²î(×î´óÖµºÍ×îСֵµÄ²î)¡¢·½²î¡¢±ê×¼²î
Author: yucui
Date: 2021/8/2
"""
import random
def ptp(nums):
"""Ç󼫲î(È«¾à)"""
return max(nums) - min(nums)
def average(nums):
"""Çó¾ùÖµ"""
return sum(nums) / len(nums)
def variance(nums):
"""Çó·½²î"""
x_bar = average(nums)
total = 0
for num in nums:
total += (x_bar - num) ** 2
return total / (len(nums) - 1)
def std(nums):
"""Çó±ê×¼²î"""
return variance(nums) ** 0.5
def median(nums):
"""ÇóÖÐλÊý"""
temp = sorted(nums)
size = len(temp)
if size % 2 == 0:
return (temp[size // 2] + temp[size // 2 - 1]) / 2
else:
return temp[size // 2]
if __name__ == '__main__':
nums_list = [random.randrange(1, 100) for _ in range(10)]
print(nums_list)
print(f'¾ùÖµ:{average(nums_list)}')
print(f'ÖÐλÊý:{median(nums_list)}')
print(f'¼«²î:{ptp(nums_list)}')
print(f'·½²î:{variance(nums_list)}')
print(f'±ê×¼²î:{std(nums_list)}')
×¢:__name__ ÊÇÒ»¸öÒþ²Ø±äÁ¿,Ëû´ú±íÁ˵±Ç°Ä£¿é(Îļþ)µÄÃû×Ö,Èç¹ûÖ±½Óͨ¹ýPython½âÊÍÆ÷ÔËÐÐhomework5.py Õâ¸öÎļþ,__name__ µÄÖµÊÇ__main__ ,Èç¹ûÊÇÔÚÆäËüÄ£¿é(Îļþ)Öе¼ÈëÁËhomework5 ,´Ëʱ__name__ µÄÖµÊÇhomework5
ÔÙн¨Ò»¸ö°ü,ÃüÃûutils ,ÔÚÕâ¸ö°üÏÂн¨Ò»¸öpythonÎļþexample05.py ,ÔÚÕâ¸öÄ£¿éÖе÷ÓÃÉÏÃæµÄÄ£¿é,´úÂëÈçÏÂ:
import random
from demo import homework5
class_a_scores = [random.randrange(50, 101) for _ in range(50)]
class_b_scores = [random.randrange(50, 101) for _ in range(50)]
print('A°à¿¼ÊԳɼ¨ÃèÊöÐÔͳ¼ÆÐÅÏ¢')
print(f'ƽ¾ù·Ö:{homework5.average(class_a_scores)}')
print(f'ÖÐλÊý:{homework5.median(class_a_scores)}')
print(f'·½²î:{homework5.variance(class_a_scores)}')
print(f'±ê×¼²î:{homework5.std(class_a_scores)}')
print('B°à¿¼ÊԳɼ¨ÃèÊöÐÔͳ¼ÆÐÅÏ¢')
print(f'ƽ¾ù·Ö:{homework5.average(class_b_scores)}')
print(f'ÖÐλÊý:{homework5.median(class_b_scores)}')
print(f'·½²î:{homework5.variance(class_b_scores)}')
print(f'±ê×¼²î:{homework5.std(class_b_scores)}')
ΪʵÏÖÎļþºó׺ÃûµÄ»ñÈ¡,ÔÚ°üutils µÄ__init__.py ÖÐдÈëÏÂÃæµÄ´úÂë
def get_suffix(file_name, *, has_point=False):
"""ÕÒÎļþºó׺Ãû
:param file_name: ´«ÈëµÄÎļþÃû
:param has_point: ÊÇ·ñ¼ÓÉϵã
:return: ºó׺Ãû
"""
find_index = file_name.rfind('.')
if find_index == 0 or find_index == len(file_name) - 1 or find_index == -1:
return ''
else:
if has_point:
return file_name[find_index:]
else:
return file_name[find_index + 1:]
ÔÚ°üdemo ÏÂн¨Ò»¸öpythonÎļþ,find.py ,ÔÚÆäÖе÷ÓÃÉÏÊö´úÂë:
import utils
print(utils.get_suffix('hello.py.txt', has_point=True))
print(utils.get_suffix('hello.py.', has_point=True))
print(utils.get_suffix('hello'))
print(utils.get_suffix(has_point=True, file_name='.hello'))
print(utils.get_suffix(has_point=True, file_name='hello.py'))
Ö»ÐèÒªµ¼Èë__init__.py ËùÔڵİü¼´¿É¡£
±ê×¼¿âÖеÄÄ£¿éºÍº¯Êý
Python±ê×¼¿âÖÐÌṩÁË´óÁ¿µÄÄ£¿éºÍº¯ÊýÀ´¼ò»¯ÎÒÃǵĿª·¢¹¤×÷,Python±ê×¼¿âÖÐÓÐÒ»ÀຯÊýÊDz»ÐèÒªimport ¾ÍÄܹ»Ö±½ÓʹÓõÄ,¼´ÄÚÖú¯Êý,ÕâЩÄÚÖú¯Êý¶¼ÊǺÜÓÐÓÃÒ²ÊÇ×î³£ÓõÄ,ÏÂÃæµÄ±í¸ñÁгöÁËÒ»²¿·ÖµÄÄÚÖú¯Êý¡£
º¯Êý | ˵Ã÷ |
---|
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 ¡£ |
Éè¼Æº¯ÊýµÄÔÔò
Éè¼Æº¯Êý×îΪÖØÒªµÄÔÔò:µ¥Ò»Ö°ÔðÔÔò(Ò»¸öº¯ÊýÖ»×öºÃÒ»¼þÊÂ),ʵÏָ߶ÈÄÚ¾Û¡£
»¹ÓÐÒ»¸öÔÔòÊǵÍñîºÏ,¼´º¯ÊýÄÚ¾¡Á¿²»ÒªÊ¹ÓÃprint ºÍinput Óï¾ä¡£
ÀýÈçÓú¯ÊýʵÏÖÇóÁ½¸öÊýµÄ×î´ó¹«Ô¼ÊýºÍ×îС¹«±¶Êý(Éè¼ÆÁ½¸öº¯Êý·Ö±ðÇó×î´ó¹«Ô¼ÊýºÍ×îС¹«±¶Êý)
def gcd(m, n):
"""Çó×î´ó¹«Ô¼Êý"""
while n % m != 0:
m, n = n % m, m
return m
def lcm(m, n):
"""Çó×îС¹«±¶Êý"""
return m * n // gcd(m, n)
a = int(input('a = '))
b = int(input('b = '))
print(gcd(a, b), lcm(a, b))
|