python-1

仅供学习使用

练习1

# 字符串加密、映射转换字符串

message = input("please input your message >>>>")

result = ""
for c in message:
    result += str(ord(c)) + "|"
result = result[:len(result) - 1]
print("加密后的结果:" + result)

data = ""
for c in result.split("|"):
    data += str(chr(int(c)))
print("解密后的数据:" + data)
please input your message >>>>起步python
加密后的结果:36215|27493|112|121|116|104|111|110
解密后的数据:起步python

加密:ord(c)
解密:chr(int(c))

注意必要的int、str转换

参考https://www.runoob.com/python/python-func-ord.html

练习2

打印九九乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print("{}".format(i*j),end=" ")
    print("")
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

利用end=" ",打印完毕不换行

练习3

寻找素数

# 找出100以内的素数,由于1不是,所以直接忽略掉即可
for i in range(2,101):
    r = True
    for j in range(2,i):
        if i%j == 0:
            r = False
            break;
    if r == True:
        print("{}".format(i),end=" ")
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

练习4

打印浮点数

# 保留小数点后2点
pi = 3.1415926
print("{:.2f}".format(pi))

# 保留符号,并且保留小数点后2位
print("{:+.2f}".format(pi))

# 不带小数
print("{:.0f}".format(pi))

# 使用%号进行显示,显示小数点后3位
print("{:.3%}".format(pi))
3.14
+3.14
3
314.159%

练习5

读取图片

# coding:utf-8
# 2019/10/16 20:02
import  matplotlib.pyplot as plt

img = plt.imread('/Users/huihui/改进点/WechatIMG583.jpeg')
print(img.shape)

plt.imshow(img)
plt.show()

练习6

python 关键词

import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

练习7

知识点:
十进制转二进制使用bin(i)
十进制转八进制使用oct(i)
十进制转十六进制使用hex(i)

练习8

知识点:
在Python中一共有6种标准数据类型,他们分别是数字、字符串、列表、元组、集合和字典

原文地址:https://www.cnblogs.com/xuehuiping/p/11694874.html