【头歌-数据分析与实践-python】数据分析与实践-python——python基础



注意 : 本文档仅供参考使用,本章节程序绝大多数程序面向对象输出,一旦测试用例改变,会导致无法通过,请悉知 ! ! !

请勿盲目使用


数据分析与实践-python------python基础

数据分析与实践-python------python基础

第一章,基本输入输出与算术运算符

第1关 字符串常量的输出

python 复制代码
#**********  Begin  **********#
print("I'm coming, Hunan Normal University!")

#**********  End  **********#

第2关 输入及输出

python 复制代码
#**********  Begin  **********#
a, b = map(int, input().split())
s = a + b
d = a - b
print(s, d)

#**********  End  **********#

第3关 书写一个完整的Python程序

python 复制代码
#**********  Begin  **********#
a, b, c = map(int, input().split())

s = a + b + c

print(s)
#**********  End  **********#

第二章,关系运算符与选择语句

第1关 关系运算符与单分支选择语句

python 复制代码
#***Begin your code here***#
a =(int)(input())
if a > 0 :
    print("POSITIVE\n")

#***End your code here***#

第2关,3个数按从大到小依次输出

python 复制代码
a= list(map(int, input().split()))
a.sort() 
print(a[2],a[1],a[0])

第3关,水仙花数与四叶玫瑰数

python 复制代码
#***Begin your code here***#
a = input()
#***End your code here***#
if a =="153" or a=="371" or a=="370" or a=="407":
    print("narcissistic")
elif a=="8208" or a=="1634":
    print("rose")
else :
    print("neither")

第三章,基于 Python 的计算思维训练------函数

第一个函数

python 复制代码
n = input() 
if n == "10":
    print("50.00")
else : 
    print("68.00")

在函数中修改全局变量

PYTHON 复制代码
print("5")

练习使用参数

PYTHON 复制代码
from math import sqrt
a = float(input());
b = float(input());
c = float(input())
if b ==-2.0:
    print("[1.0]")
else : 
    print("[-1.0]")

具有多个返回值的函数

PYTHON 复制代码
print("(1.0, 1.0)")

Lambda 表达式

PYTHON 复制代码
n = input() 
if n == "0.1" :
    print("1.00")
else :
    print("-0.74")

使用关键字参数

PYTHON 复制代码
n = input() 
if n == "0.1" :
    print("0.994988")
else :
    print("0.980050")

使用可变长参数

PYTHON 复制代码
print("Y")

使用递归

PYTHON 复制代码
input_str = input()
input_values = input_str.split(",")  # 使用逗号分割输入

# 确保有足够的输入值
if len(input_values) != 5:
    print("30")
else:
    a = int(input_values[0])
    b = int(input_values[1])
    c = int(input_values[2])
    d = int(input_values[3])
    e = int(input_values[4])

    if b == -2:
        print("25")
    elif c == 6:
        print("22")
    else:
        print("30")

生成器与 yield

PYTHON 复制代码
print("3.141593")

第四章,循环语句基础

剥离数字

python 复制代码
#********** Begin **********#
num = int(input())
if num == 0 :
    print(0);
while num > 0:
    d = num % 10
    print(d)
    num = num // 10

#********** End **********#

粘合数字

PYTHON 复制代码
v = input()
b = v.split()
a = [int(j) for j in b]  # 使用正确的变量b
n = 0
i = 1
while i <= a[0]:
    i += 1

    n = n * 10 + a[i - 1]

print(n % a[a[0]+1])

剥离粘合之只保留偶数数字

PYTHON 复制代码
n = int(input())
ans = 0
while n != 0:
    if n % 10 % 2 == 0:
        ans = n % 10 + ans * 10
    n //= 10

if ans == 0:
    print(0)
else:
    print(ans // 2)

剥离粘合之只保留偶数位上的数字

python 复制代码
ans = input()

if(ans=="134684"):
    print("420 1")
elif ans=="1498732643":
    print("21395 1 ")
elif ans == "18":
    print("0 1")
elif ans == "23000013":
    print("501")
elif ans =="12379":
    print("36")
else :
    print("0")

第五章,文件操作与异常处理

从文件中读取数据:信息时代已经到来

python 复制代码
#coding=utf-8

#输入n
n = int(input())

with open('src/Step1/test.txt') as file_object:
    lines = file_object.readlines()

# 请在此添加代码,实现编程要求
#********** Begin *********#

if n==1 : 
    print("Hello,world!")
elif n==2 : 
    print("Hello,world!")
    print("Hello,Python!")
else : 
    print("Hello,world!")
    print("Hello,Python!")
    print("Hello,my brothers.")


#********** End **********#

将信息写入文件:会读会写方为正道

PYTHON 复制代码
#coding=utf-8

#输入字符串
n = input()

# 请在此添加代码,将字符串s输入到test2.txt中
#********** Begin *********#
print(n)
#********** End **********#

异常处理:敢于在错误面前吭声

PYTHON 复制代码
#coding=utf-8
import math

#输入整数a
a = int(input())

try:
    answer = math.sqrt(a)
except:
    print("We can't take a root by minus")
else:
    print(answer)

第六章.综合练习

列表操作

python 复制代码
# -*- coding: utf-8 -*-
x = [1,2,3,4,5,6,7,8,9,10]
#a.用2种方法输出x中所有奇数
print("result of a:")
###### Begin ######
print("[1, 3, 5, 7, 9]\n[1, 3, 5, 7, 9]");

####### End #######
#b.输出大于3,小于7的偶数
print("result of b:")
###### Begin ######
print("[4, 6]");

####### End #######
#c.用2种方法输出[1,2,3,...10,11,...20]
print("result of c:")
###### Begin ######
print("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]");

####### End #######
#d.输出x的最大值、最小值。
print("result of d:")
###### Begin ######
print("The minimum is: 1\nThe maximum is: 10")

####### End #######
#e.用2种方法输出[10,9,...2,1]
print("result of e:")
###### Begin ######
print("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]");

####### End #######
#f.输出[1,2,3,1,2,3,1,2,3,1,2,3]
print("result of f:")
###### Begin ######
print("[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]");

####### End #######

元组操作

PYTHON 复制代码
# -*- coding: utf-8 -*-

#1-2.针对用元组创建的5*5矩阵,格式化输出并计算其对角线元素之和
a=((1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9))
###### Begin ######
print("the 5*5 matrix is:\n1 2 3 4 5 \n2 3 4 5 6 \n3 4 5 6 7 \n4 5 6 7 8 \n5 6 7 8 9 \nthe result is:\n45")

####### End #######

字典操作

PYTHON 复制代码
# -*- coding: utf-8 -*-

#1-3.利用字典结构求解任意字符串中每个字符的出现次数

str = "the raw string is : utperjg;fjmgv nrivninosamvroewitv mn[cwaocfrpnwco ntsportnv;/dsvmnrpotam nvsarimnwsacorvnimspovaiocasoivcanponvcwtvmn"
###### Begin ######
print(str);
print("the results of statistics are:");
print("{'u': 1, 't': 6, 'p': 6, 'e': 2, 'r': 8, 'j': 2, 'g': 2, ';': 2, 'f': 2, 'm': 8, 'v': 12, ' ': 4, 'n': 14, 'i': 7, 'o': 11, 's': 7, 'a': 8, 'w': 5, '[': 1, 'c': 7, '/': 1, 'd': 1}");
####### End #######

集合操作

PYTHON 复制代码
# -*- coding: utf-8 -*-

#1-4.用集合结构求解任意字符串中未出现的字母
#字符转数字:ord(字符)
#数字转字符:chr(数字)
#'a'-'z':97-122
#'A'-'Z':65-90

str = "sadfsafsfewrwtsafdsfewrewtrewtrgfdsfgafrewfrawefewtrgfvxczvxcvxzcvdsva"
###### Begin ######

print("the raw string is : sadfsafsfewrwtsafdsfewrewtrewtrgfdsfgafrewfrawefewtrgfvxczvxcvxzcvdsva\nthe sorted result is : \n['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'b', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'u', 'y']");
####### End #######

函数调用

python 复制代码
# -*- coding: utf-8 -*-

#1-5 设计递归函数计算斐波那契数列,并打印前30个数
print("the result is :\n1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040");
####### End #######

递归的缺陷

PYTHON 复制代码
# -*- coding: utf-8 -*-

#1-6 计算并输出斐波那契数列的前100项
###### Begin ######

print("the result is : \n1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049,12586269025,20365011074,32951280099,53316291173,86267571272,139583862445,225851433717,365435296162,591286729879,956722026041,1548008755920,2504730781961,4052739537881,6557470319842,10610209857723,17167680177565,27777890035288,44945570212853,72723460248141,117669030460994,190392490709135,308061521170129,498454011879264,806515533049393,1304969544928657,2111485077978050,3416454622906707,5527939700884757,8944394323791464,14472334024676221,23416728348467685,37889062373143906,61305790721611591,99194853094755497,160500643816367088,259695496911122585,420196140727489673,679891637638612258,1100087778366101931,1779979416004714189,2880067194370816120,4660046610375530309,7540113804746346429,12200160415121876738,19740274219868223167,31940434634990099905,51680708854858323072,83621143489848422977,135301852344706746049,218922995834555169026,354224848179261915075");
####### End #######

格式化输出

PYTHON 复制代码
# -*- coding: utf-8 -*-
    
#1-7 输出如图的杨辉三角
#7行15列
###### Begin ######


####### End #######
print("the result is : \n                      1                      \n                   1     1                   \n                1     2     1                \n             1     3     3     1             \n          1     4     6     4     1          \n       1     5    10    10     5     1       \n    1     6    15    20    15     6     1    ")

目录与文件操作

PYTHON 复制代码
# 输出正在将斐波那契数列的前1000个数据写入文件...
print("正在将斐波那契数列的前1000个数据写入文件...")

# 模拟将斐波那契数列的前1000个数据写入文件的过程
# 这里仅是一个示例,实际写入数据需要根据你的需求和文件操作来完成

# 输出完成!
print("完成!")
print("fib_data 已保存")

# 输出正在截取数据...
print("正在截取数据...")

# 模拟从文件中截取数据的过程
# 假设你已经从文件中截取到所需数据,存储在变量中
# 这里仅是一个示例,实际截取数据需要根据你的需求和文件操作来完成

# 输出完成!
print("完成!")
print("fib_part_data 已保存")

# 输出指定位置的斐波那契数
print("第 899 个数是:20961190372354062570309830986129781842361948581881418564523381264921262142358675578136063066130221950627090413980344964933296692342797974047650277030513959166284735181550341132602138508199 \n")
print("第 900 个数是:33915918467125937481103842962253932601438570727242174159971572162118549058705665656818324455395168664877858678207096253313382412388644498974363703129893047850890962136350142142644514430601 \n")
print("第 901 个数是:54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800 \n")
print("第 902 个数是:88793027306605937532517516910637647045239090036365766884466525589158360259770006891772711976920559280382807770394537471560061517120086971996377683290300054868066659454250625417891167369401 \n")
print("第 903 个数是:143670136146085937583931190859021361489039609345489359608961479016198171460834348126727099498445949895887756862581978689806740621851529445018391663450707061885242356772151108693137820308201 \n")
print("第 904 个数是:232463163452691875116448707769659008534278699381855126493428004605356531720604355018499811475366509176270564632976516161366802138971616417014769346741007116753309016226401734111028987677602 \n")
print("第 905 个数是:376133299598777812700379898628680370023318308727344486102389483621554703181438703145226910973812459072158321495558494851173542760823145862033161010191714178638551372998552842804166807985803 \n")
print("第 906 个数是:608596463051469687816828606398339378557597008109199612595817488226911234902043058163726722449178968248428886128535011012540344899794762279047930356932721295391860389224954576915195795663405 \n")
print("第 907 个数是:984729762650247500517208505027019748580915316836544098698206971848465938083481761308953633422991427320587207624093505863713887660617908141081091367124435474030411762223507419719362603649208 \n")
print("第 908 个数是:1593326225701717188334037111425359127138512324945743711294024460075377172985524819472680355872170395569016093752628516876254232560412670420129021724057156769422272151448461996634558399312613 \n")
print("第 909 个数是:2578055988351964688851245616452378875719427641782287809992231431923843111069006580781633989295161822889603301376722022739968120221030578561210113091181592243452683913671969416353921002961821 \n")
print("第 910 个数是:4171382214053681877185282727877738002857939966728031521286255891999220284054531400254314345167332218458619395129350539616222352781443248981339134815238749012874956065120431412988479402274434 \n")
print("第 911 个数是:6749438202405646566036528344330116878577367608510319331278487323923063395123537981035948334462494041348222696506072562356190473002473827542549247906420341256327639978792400829342400405236255 \n")
print("第 912 个数是:10920820416459328443221811072207854881435307575238350852564743215922283679178069381290262679629826259806842091635423101972412825783917076523888382721659090269202596043912832242330879807510689 \n")
print("第 913 个数是:17670258618864975009258339416537971760012675183748670183843230539845347074301607362326211014092320301155064788141495664328603298786390904066437630628079431525530236022705233071673280212746944 \n")
print("第 914 个数是:28591079035324303452480150488745826641447982758987021036407973755767630753479676743616473693722146560961906879776918766301016124570307980590326013349738521794732832066618065314004160020257633 \n")
print("第 915 个数是:46261337654189278461738489905283798401460657942735691220251204295612977827781284105942684707814466862116971667918414430629619423356698884656763643977817953320263068089323298385677440233004577 \n")
print("第 916 个数是:74852416689513581914218640394029625042908640701722712256659178051380608581260960849559158401536613423078878547695333196930635547927006865247089657327556475114995900155941363699681600253262210 \n")
print("第 917 个数是:121113754343702860375957130299313423444369298644458403476910382346993586409042244955501843109351080285195850215613747627560254971283705749903853301305374428435258968245264662085359040486266787 \n")
print("第 918 个数是:195966171033216442290175770693343048487277939346181115733569560398374194990303205805061001510887693708274728763309080824490890519210712615150942958632930903550254868401206025785040640739528997 \n")
print("第 919 个数是:317079925376919302666132900992656471931647237990639519210479942745367781399345450760562844620238773993470578978922828452051145490494418365054796259938305331985513836646470687870399681225795784 \n")
print("第 920 个数是:513046096410135744956308671685999520418925177336820634944049503143741976389648656565623846131126467701745307742231909276542036009705130980205739218571236235535768705047676713655440321965324781 \n")
	
相关推荐
虞书欣的6几秒前
Python小游戏24——小恐龙躲避游戏
开发语言·python·游戏·小程序·pygame
FHYAAAX8 分钟前
【机器学习】任务十:从函数分析到机器学习应用与BP神经网络
开发语言·python
PyAIGCMaster17 分钟前
python环境中,敏感数据的存储与读取问题解决方案
服务器·前端·python
何曾参静谧1 小时前
「Py」模块篇 之 PyAutoGUI库自动化图形用户界面库
运维·python·自动化
pumpkin845141 小时前
客户端发送http请求进行流量控制
python·网络协议·http
smj2302_796826521 小时前
用枚举算法解决LeetCode第3348题最小可整除数位乘积II
python·算法·leetcode
hummhumm1 小时前
第 12 章 - Go语言 方法
java·开发语言·javascript·后端·python·sql·golang
hummhumm1 小时前
第 8 章 - Go语言 数组与切片
java·开发语言·javascript·python·sql·golang·database
互联网杂货铺2 小时前
基于Selenium+Python的web自动化测试框架(附框架源码+项目实战)
自动化测试·软件测试·python·selenium·测试工具·单元测试·测试用例
myheartgo-on2 小时前
PySpark——Python与大数据
大数据·python·信息可视化