Python3 快速入门 实例(三)
21.Python 最小公倍数算法
def lcm ( x, y) :
if x > y:
greater = x
else :
greater = y
while True :
if ( greater % x == 0 ) and ( greater % y == 0 ) :
num = greater
break
greater += 1
return num
num1 = int ( input ( "输入第一个数字: " ) )
num2 = int ( input ( "输入第二个数字: " ) )
print ( num1, "和" , num2, "的最小公倍数为" , lcm( num1, num2) )
22.Python 简单计算器实现(2个数字之间)
def add ( x, y) :
"""相加"""
return x + y
def subtract ( x, y) :
"""相减"""
return x - y
def multiply ( x, y) :
"""相乘"""
return x * y
def divide ( x, y) :
"""相除"""
return x / y
print ( "选择运算:" )
print ( "1、相加" )
print ( "2、相减" )
print ( "3、相乘" )
print ( "4、相除" )
choice = input ( "输入你的选择(1/2/3/4):" )
num1 = int ( input ( "输入第一个数字: " ) )
num2 = int ( input ( "输入第二个数字: " ) )
if choice == '1' :
print ( num1, "+" , num2, "=" , add( num1, num2) )
elif choice == '2' :
print ( num1, "-" , num2, "=" , subtract( num1, num2) )
elif choice == '3' :
print ( num1, "*" , num2, "=" , multiply( num1, num2) )
elif choice == '4' :
print ( num1, "/" , num2, "=" , divide( num1, num2) )
else :
print ( "输入不合法" )
23.Python 生成日历
import calendar
yy = int ( input ( "输入年份:" ) )
mm = int ( input ( "输入月份:" ) )
print ( calendar. month( yy, mm) )
24.Python 使用递归斐波那契数列
def cur ( m) :
if m <= 1 :
return i
else :
return cur( m - 1 ) + cur( m - 2 )
n = int ( input ( "您要输出几项? " ) )
if n <= 0 :
print ( "输入正数" )
else :
print ( "斐波那契数列:" )
for i in range ( n) :
print ( cur( i) )
25.Python 文件 IO
with open ( "test.txt" , "wt" ) as out_file:
out_file. write( "看看我\n宝宝在这儿。" )
with open ( "test.txt" , "rt" ) as in_file:
txt = in_file. read( )
print ( txt)
26.Python 字符串判断
print ( "测试实例一" )
str = "runoob.com"
print ( str . isalnum( ) )
print ( str . isalpha( ) )
print ( str . isdigit( ) )
print ( str . islower( ) )
print ( str . isupper( ) )
print ( str . istitle( ) )
print ( str . isspace( ) )
print ( "------------------------" )
print ( "测试实例二" )
str = "runoob"
print ( str . isalnum( ) )
print ( str . isalpha( ) )
print ( str . isdigit( ) )
print ( str . islower( ) )
print ( str . isupper( ) )
print ( str . istitle( ) )
print ( str . isspace( ) )
运行结果:
测试实例一
False
False
False
True
False
False
False
- - - - - - - - - - - - - - - - - - - - - - - -
测试实例二
True
True
False
True
False
False
False
27.Python 字符串大小写转换
str = "www.hao123.COM"
print ( str . upper( ) )
print ( str . lower( ) )
print ( str . capitalize( ) )
print ( str . title( ) )
运行结果:
WWW. HAO123. COM
www. hao123. com
Www. hao123. com
Www. Hao123. Com
28.Python 计算每个月天数
import calendar
monthRange = calendar. monthrange( 2021 , 2 )
print ( f'第一天是星期{monthRange[0]},该月总共有{monthRange[1]}天。' )
29.Python 获取昨天日期
import datetime
def getYesterday ( ) :
today = datetime. date. today( )
oneday = datetime. timedelta( 1 )
yesterday = today - oneday
return yesterday
print ( getYesterday( ) )
30.Python 约瑟夫生者死者小游戏
people = { }
for x in range ( 1 , 31 ) :
people[ x] = 1
check = 0
i = 1
j = 0
while i <= 31 :
if i == 31 :
i = 1
elif j == 15 :
break
else :
if people[ i] == 0 :
i += 1
continue
else :
check += 1
if check == 9 :
people[ i] = 0
check = 0
print ( "{}号下船了" . format ( i) )
j += 1
else :
i += 1
continue