利用 Python进行数据分析实验(一)

一、实验目的

使用Python解决简单问题

二、实验要求

自主编写并运行代码,按照模板要求撰写实验报告

三、实验步骤

本次实验共有5题:

  1. 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?四个数字是2、3、7、9呢?
  2. 判断1000-2000之间有多少个素数,并输出所有素数.
  3. 打印出所有的"四叶玫瑰数",所谓"四叶玫瑰数"是指一个四位数,其各位数字四次方和等于该数本身。
  4. 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符输出的数并分别统计每一种类型的个数。
  5. 打印九九乘法表。

四、实验结果

T1

T1-1

python 复制代码
import itertools

count = 0
for arr in itertools.permutations('1234', 3):
    # print(arr)
    print(int(arr[0]) * 100 + int(arr[1]) * 10 + int(arr[2]))
    count = count + 1

print('共有' + str(count) + '个组合')

T1-2

python 复制代码
import itertools

count = 0
for arr in itertools.permutations('2379', 3):
   # print(arr)
   print(int(arr[0]) * 100 + int(arr[1]) * 10 + int(arr[2]))
   count = count + 1

print('共有' + str(count) + '个组合')

T2

python 复制代码
import math

count = 0
res = []


def check(x):
    if x <= 1:
        return False
    for flag in range(2, int(math.sqrt(x) + 1)):
        if x % flag == 0:
            return False
    return True


for i in range(1000, 2001):
    if check(i):
        res.append(i)
        count = count + 1

for i in range(0, len(res)):
    print(res[i], end=' ')
    if ((i + 1) % 10) == 0:
        print('\n')

print('\n1000~2000有素数' + str(count) + '个')

T3

python 复制代码
def func(x):
    arr = str(x)
    if res(arr) == x:
        return True
    else:
        return False


def res(arr):
    return pow(int(arr[0]), 4) + pow(int(arr[1]), 4) + pow(int(arr[2]), 4) + pow(int(arr[3]), 4)


for i in range(1000, 10000):
    if func(i):
        print(i)

T4

python 复制代码
import re

count_n = 0
count_s = 0
count_l = 0
count_o = 0

'''
string = 'Some people, when confronted with a problem, ' \
         'think "I know, I'll use regular expressions." ' \
         'Now they have two problems.'
'''
string = input()

number = re.finditer(r'\d+', string)
for match in number:
    count_n = count_n + 1
print('数字的数量是' + str(count_n))

space = re.finditer(r'\s+', string)
for match in space:
    count_s = count_s + 1
print('空白的数据是' + str(count_s))

letter = re.finditer(r'[a-zA-Z]', string)
for match in letter:
    count_l = count_l + 1
print('字符的数量是' + str(count_l))

print('其他字符的数量是' + str(len(string) - count_s - count_l - count_n))

T5

python 复制代码
import numpy as np
import itertools

count = 0

form = np.empty([81, 2], int)
for arr in itertools.product('123456789', repeat=2):
    form[count][0] = arr[0]
    form[count][1] = arr[1]
    count = count + 1

print('打印99乘法表:')
left = 0
count = 1

for i in range(0, 9):
    for j in range(left, left + count):
        string = str(form[j][0]) + '*' + str(form[j][1]) + '=' + str(int(form[j][0]) * int(form[j][1]))
        print('%-8s' % string, end='')

    print("\n")
    left = left + 9
    count = count + 1

五、实验体会

Python标准库和第三库众多,功能强大。充分利用库函数来简化和加速代码、不重复造轮子能够显著简化代码提高编码效率

相关推荐
oooost25 分钟前
Jupyter Notebook 常用快捷键速查
ide·python·jupyter
阿钱真强道38 分钟前
30 嵌入式操作系统 | 上位机界面:Flask 网页显示 + 控制闭环
后端·python·flask
ysu_031439 分钟前
Docker-Desktop-WSL2实战指南-Windows从安装配置到项目容器化
windows·python·docker·容器·composer·dockerfile·wsl2
郝学胜-神的一滴1 小时前
AI 编程智能体 02:AI智能体到底是什么
开发语言·人工智能·python·程序人生·pycharm
Guarding and trust1 小时前
python系列之多线程编程
python
梅雅达编程笔记2 小时前
03_网页表格数据抓取
爬虫·python·beautifulsoup·pandas·数据采集
迅猛龙办公室2 小时前
Python实现求两个数字之和
java·开发语言·python
企业数字化笔记2 小时前
视频成片怎么稳定导出?编码选择、GPU/CPU降级与媒体交付验收
java·python·ffmpeg
Data analyse4562 小时前
数据合规的敏感数据怎么识别?
前端·人工智能·数据分析
szial2 小时前
网络爬虫与 CDP 实战(一):网页数据到底在哪?从 HTTP 到分页采集
网络·爬虫·python·网络爬虫