Python高级教程(十)、实例演示

exam1.py

# -*- coding: UTF-8 -*-


# 该实例输出 Hello World!
print('Hello World!')

运行结果:

Hello World!

exam2.py

# -*- coding: UTF-8 -*-
 
 
# 用户输入数字
num1 = input('输入第一个数字:')
num2 = input('输入第二个数字:')
 
# 求和
sum = float(num1) + float(num2)
 
# 显示计算结果
print('数字 {0} 和 {1} 相加结果为: {2}'.format(num1, num2, sum))

运行结果如下:

输入第一个数字:1
输入第二个数字:3
数字 1 和 3 相加结果为: 4.0

exam3.py

# -*- coding: UTF-8 -*-
 
 
# 生成 0 ~ 9 之间的随机数
 
# 导入 random(随机数) 模块
import random
 
print(random.randint(0,9))

运行三次的结果如下:

(csdn) michaelkoo@MacBook code % python exam3.py 
1
(csdn) michaelkoo@MacBook code % python exam3.py
3
(csdn) michaelkoo@MacBook code % python exam3.py
6

exam4.py

# -*- coding: UTF-8 -*-
 
 
# 九九乘法表
for i in range(1, 10):
    for j in range(1, i+1):
        print('{}x{}={}\t'.format(j, i, i*j), end='')
    print()

运行结果如下:

1x1=1	
1x2=2	2x2=4	
1x3=3	2x3=6	3x3=9	
1x4=4	2x4=8	3x4=12	4x4=16	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81

exam5.py

# -*- coding: UTF-8 -*-
 
 
# 获取用户输入十进制数
dec = int(input("输入数字:"))
 
print("十进制数为:", dec)
print("转换为二进制为:", bin(dec))
print("转换为八进制为:", oct(dec))
print("转换为十六进制为:", hex(dec))

运行结果如下:

输入数字:3
十进制数为: 3
转换为二进制为: 0b11
转换为八进制为: 0o3
转换为十六进制为: 0x3

exam6.py

 
# 用户输入字符
c = input("请输入一个字符: ")
 
# 用户输入ASCII码,并将输入的数字转为整型
a = int(input("请输入一个ASCII码: "))
 
 
print( c + " 的ASCII 码为", ord(c))
print( a , " 对应的字符为", chr(a))

运行结果如下:

请输入一个字符: k
请输入一个ASCII码: 97
k 的ASCII 码为 107
97  对应的字符为 a

exam7.py

# 引入日历模块
import calendar
 
# 输入指定年月
yy = int(input("输入年份: "))
mm = int(input("输入月份: "))
 
# 显示日历
print(calendar.month(yy,mm))

运行结果如下:

输入年份: 2013
输入月份: 05
      May 2013
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

exam8.py

people={}
for x in range(1,31):
    people[x]=1
# print(people)
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

运行结果如下:

9号下船了
18号下船了
27号下船了
6号下船了
16号下船了
26号下船了
7号下船了
19号下船了
30号下船了
12号下船了
24号下船了
8号下船了
22号下船了
5号下船了
23号下船了

exam9.py

def swapPositions(list, pos1, pos2):
     
    list[pos1], list[pos2] = list[pos2], list[pos1]
    return list
 
List = [23, 65, 19, 90]
pos1, pos2  = 1, 3
 
print(swapPositions(List, pos1-1, pos2-1))

运行结果如下:

[19, 65, 23, 90]

exam10.py

import re
  
def Find(string): 
    # findall() 查找匹配正则表达式的字符串
    url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', string)
    return url 
      
 
string = 'baidu 的网页地址为:https://www.baidu.com,Google 的网页地址为:https://www.google.com'
print("Urls: ", Find(string))

运行结果如下:

Urls:  ['https://www.baidu.com', 'https://www.google.com']

exam11.py

def exec_code(): 
    LOC = """ 
def factorial(num): 
    fact=1 
    for i in range(1,num+1): 
        fact = fact*i 
    return fact 
print(factorial(5)) 
"""
    exec(LOC) 
 
exec_code()

运行结果如下:

120

exam12.py

import time
 
a1 = "2013-5-8 23:40:00"
# 先转换为时间数组
timeArray = time.strptime(a1, "%Y-%m-%d %H:%M:%S")
 
# 转换为时间戳
timeStamp = int(time.mktime(timeArray))
print(timeStamp)
 
 
# 格式转换 - 转为 /
a2 = "2019/5/10 23:40:00"
# 先转换为时间数组,然后转换为其他格式
timeArray = time.strptime(a2, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(otherStyleTime)

运行结果如下:

1368027600
2019/05/10 23:40:00

exam13.py

import time
 
# 获得当前时间时间戳
now = int(time.time())
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)

运行结果如下:

2013-05-08 12:56:41

exam14.py

def myfont():
    name = input("输入你的名字:(only English words) \t")
    length = len(name)

    for x in range(0, length):
        c = name[x]
        c = c.upper()

        if c == 'A':
            print('''-----A-----
---A---A---
--A-A-A-A--
-A-------A-''', '\n')

        elif c == 'B':
            print('''---B-B-B---
---B--B----
---B--B----
---B-B-B---''', '\n')

        elif c == 'C':
            print('''---C-C-C---
--C--------
--C--------
---C-C-C---''', '\n')

        elif c == 'D':
            print('''---D-D-D---
---D----D--
---D----D--
---D-D-D---''', '\n')

        elif c == 'E':
            print('''---E-E-E---
---EEE-----
---EEE-----
---E-E-E---''', '\n')

        elif c == 'F':
            print('''---F-F-F---
---F-------
---F-F-F---
---F-------''', '\n')

        elif c == 'G':
            print('''---G--GG---
--G--------
--G---GG---
---G--GG---''', '\n')

        elif c == 'H':
            print('''--H-----H--
--H--H--H--
--H--H--H--
--H-----H--''', '\n')

        elif c == 'I':
            print('''--II-I-II--
-----I-----
-----I-----
--II-I-II--''', '\n')

        elif c == 'J':
            print('''-----J-----
-----J-----
--J--J-----
---J-J-----''', '\n')

        elif c == 'K':
            print('''---K---K---
---K-K-----
---K-K-----
---K---K---''', '\n')

        elif c == 'L':
            print('''--L--------
--L--------
--L--------
--L-L-L-L--''', '\n')

        elif c == 'M':
            print('''--M-----M--
--M-M-M-M--
--M--M--M--
--M-----M--''', '\n')

        elif c == 'N':
            print('''--N-----N--
--N-N---N--
--N--N--N--
--N---N-N--''', '\n')

        elif c == 'O':
            print('''----OOO----
--OO---OO--
--OO---OO--
----OOO----''', '\n')

        elif c == 'P':
            print('''---P-P-P---
---P----P---
---P-P-P----
---P--------''', '\n')

        elif c == 'Q':
            print('''----QQQ----
--QQ---QQ--
--QQ-Q-QQ--
----QQQ--Q-''', '\n')

        elif c == 'R':
            print('''--R-RR-----
--R---R----
--R-RR-----
--R---R----''', '\n')

        elif c == 'S':
            print('''----SS-----
--SS---SS--
-SS---SS---
----SS-----''', '\n')

        elif c == 'T':
            print('''--TT-T-TT--
-----T-----
-----T-----
-----T-----''', '\n')

        elif c == 'U':
            print('''--U-----U--
--U-----U--
--U-----U--
---U-U-U---''', '\n')

        elif c == 'V':
            print('''--V-----V--
---V---V---
----V-V----
-----V-----''', '\n')

        elif c == 'W':
            print('''-W---W---W-
--W--W--W--
---W---W---
----W-W----''', '\n')

        elif c == 'X':
            print('''--X-----X--
----X-X----
----X-X----
--X-----X--''', '\n')

        elif c == 'Y':
            print('''--Y-----Y--
---Y---Y---
-----Y-----
-----Y-----''', '\n')

        elif c == 'Z':
            print('''--Z--Z--Z--
-------Z---
----Z------
--Z--Z--Z--''', '\n')

        elif c == ' ':
            print('''-----------
-----------
-----------
-----------''', '\n')

        elif c == '.':
            print('''----..-----
---..-..---
---..-..---
----..-----''', '\n')

if __name__ == '__main__':
    myfont()

运行结果如下:

输入你的名字:(only English words) 	michael
--M-----M--
--M-M-M-M--
--M--M--M--
--M-----M-- 

--II-I-II--
-----I-----
-----I-----
--II-I-II-- 

---C-C-C---
--C--------
--C--------
---C-C-C--- 

--H-----H--
--H--H--H--
--H--H--H--
--H-----H-- 

-----A-----
---A---A---
--A-A-A-A--
-A-------A- 

---E-E-E---
---EEE-----
---EEE-----
---E-E-E--- 

--L--------
--L--------
--L--------
--L-L-L-L-- 

exam15.py

# 返回 x 在 arr 中的索引,如果不存在返回 -1
def binarySearch (arr, l, r, x): 
  
    # 基本判断
    if r >= l: 
  
        mid = int(l + (r - l)/2)
  
        # 元素整好的中间位置
        if arr[mid] == x: 
            return mid 
          
        # 元素小于中间位置的元素,只需要再比较左边的元素
        elif arr[mid] > x: 
            return binarySearch(arr, l, mid-1, x) 
  
        # 元素大于中间位置的元素,只需要再比较右边的元素
        else: 
            return binarySearch(arr, mid+1, r, x) 
  
    else: 
        # 不存在
        return -1
  
# 测试数组
arr = [ 2, 3, 4, 10, 40 ] 
x = 10
  
# 函数调用
result = binarySearch(arr, 0, len(arr)-1, x) 
  
if result != -1: 
    print ("元素在数组中的索引为 %d" % result )
else: 
    print ("元素不在数组中")

运行结果如下:

元素在数组中的索引为 3

exam16.py

def partition(arr,low,high): 
    i = ( low-1 )         # 最小元素索引
    pivot = arr[high]     
  
    for j in range(low , high): 
  
        # 当前元素小于或等于 pivot 
        if   arr[j] <= pivot: 
          
            i = i+1 
            arr[i],arr[j] = arr[j],arr[i] 
  
    arr[i+1],arr[high] = arr[high],arr[i+1] 
    return ( i+1 ) 
  
 
# arr[] --> 排序数组
# low  --> 起始索引
# high  --> 结束索引
  
# 快速排序函数
def quickSort(arr,low,high): 
    if low < high: 
  
        pi = partition(arr,low,high) 
  
        quickSort(arr, low, pi-1) 
        quickSort(arr, pi+1, high) 
  
arr = [10, 7, 8, 9, 1, 5] 
n = len(arr) 
quickSort(arr,0,n-1) 
print ("排序后的数组:") 
for i in range(n): 
    print ("%d" %arr[i]),

运行结果如下:

排序后的数组:
1
5
7
8
9
10
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容卡卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值