pythonisalpha函数计算公式_常用python实现.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"冒泡排序--x的n次方--计算阶乘--字符串大小写转换--文件操作--随机验证码--字符串操作--判断奇偶数--进制转换--日历--文件IO--字符串内置函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 冒泡排序"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-58, 1, 7, 8, 10, 12, 23, 34, 56, 56, 100, 234, 354, 456]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58]\n",
"def Maopaopaixu(num): # 冒泡排序\n",
" for i in range(len(num)-1):\n",
" for j in range(len(num)-i-1):\n",
" if num[j]>num[j+1]:\n",
" num[j],num[j+1]=num[j+1],num[j]\n",
" return num\n",
"Maopaopaixu(num)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 整数x的N次方"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def x_N(x,n): # 整数x的n次方\n",
" ans=1\n",
" if x==0:return ans\n",
" while n>0:\n",
" n-=1\n",
" ans=ans*x\n",
" return ans\n",
"x_N(2,4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 计算整数的阶乘"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入一个数字:3\n"
]
},
{
"data": {
"text/plain": [
"'3的阶乘为6'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def Jiechen(): #计算整数的阶乘\n",
" num = int(input(\"请输入一个数字:\"))\n",
" ans=1\n",
" if num<0:\n",
" return '负数没有阶乘'\n",
" elif num==0:\n",
" return '0的阶乘为1'\n",
" else:\n",
" for i in range(1,num+1):\n",
" ans=ans*i\n",
" return \"%d的阶乘为%d\"%(num,ans)\n",
"Jiechen()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入一个数字:5\n"
]
},
{
"data": {
"text/plain": [
"'5的阶乘为120'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def JiechenDigui(): #递归计算整数的阶乘\n",
" def fac(n):\n",
" if n==1:\n",
" return 1\n",
" else: return n*fac(n-1)\n",
" num = int(input(\"请输入一个数字:\"))\n",
" ans=1\n",
" if num<0:\n",
" return '负数没有阶乘'\n",
" elif num==0:\n",
" return '0的阶乘为1'\n",
" else:\n",
" return \"%d的阶乘为%d\"%(num,fac(num))\n",
" \n",
"JiechenDigui()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 把一个list中所有的字符串变成小写:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HELLO\n",
"WORLD\n",
"IBM\n",
"APPLE\n",
"['hello', 'world', 'ibm', 'apple']\n"
]
}
],
"source": [
"L = ['Hello','World','IBM','Apple'] #小写方法lower,大写方法uppor\n",
"for s in L:\n",
" s=s.lower()\n",
" print(s)\n",
"print([s.upper() for s in L])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 输出某个路径下的所有文件和文件夹的路径"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入一个路径:E:\\tencent\n",
"E:\\tencent\\NBA2K\n",
"E:\\tencent\\Speed.log\n",
"E:\\tencent\\WeGameApps\n"
]
}
],
"source": [
"import os \n",
"def print_dir():\n",
" filepath = input(\"请输入一个路径:\")\n",
" if filepath == \"\":\n",
" print(\"请输入正确的路径\")\n",
" else:\n",
" for i in os.listdir(filepath): #获取目录中的文件及子目录列表\n",
" print(os.path.join(filepath,i)) #把路径组合起来\n",
"print_dir()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 输出某个路径及其子目录下的所有文件路径"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"F:\\CNKI\\cellibrary\n",
"F:\\CNKI\\cellibrary\\!!请勿删除该文件夹.txt\n"
]
}
],
"source": [
"def show_dir(filepath):\n",
" for i in os.listdir(filepath):\n",
" path = (os.path.join(filepath, i))\n",
" print(path)\n",
" if os.path.isdir(path): #isdir()判断是否是目录\n",
" show_dir(path) #如果是目录,使用递归方法\n",
"filepath = \"F:\\CNKI\"\n",
"show_dir(filepath)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 输出某个路径及其子目录下所有以.html为后缀的文件 (任意类型)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"def print_dir(filepath):\n",
" for i in os.listdir(filepath):\n",
" path = os.path.join(filepath, i)\n",
" if os.path.isdir(path):\n",
" print_dir(path)\n",
" if path.endswith(\".html\"):\n",
" print(path)\n",
"filepath = \"F:\\CNKI\"\n",
"print_dir(filepath)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 随机生成验证码的两种方式(数字字母)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['l', '6', 'G', 'w', 'v', 'V']\n",
"l6GwvV\n"
]
}
],
"source": [
"import random\n",
"list1=[]\n",
"for i in range(65,91):\n",
" list1.append(chr(i)) #通过for循环遍历asii追加到空列表中\n",
"for j in range (97,123):\n",
" list1.append(chr(j))\n",
"for k in range(48,58):\n",
" list1.append(chr(k))\n",
"ma = random.sample(list1,6)\n",
"print(ma) #获取到的为列表\n",
"ma = ''.join(ma) #将列表转化为字符串\n",
"print(ma)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"V9uXhx\n"
]
}
],
"source": [
"import random,string\n",
"str1 = \"0123456789\"\n",
"str2 = string.ascii_letters # string.ascii_letters 包含所有字母(大写或小写)的字符串\n",
"str3 = str1+str2\n",
"ma1 = random.sample(str3,6) #多个字符中选取特定数量的字符\n",
"ma1 = ''.join(ma1) #使用join拼接转换为字符串\n",
"print(ma1) #通过引入string模块和random模块使用现有的方法"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 判断字符串是否只由数字组成"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"False\n",
"True\n"
]
}
],
"source": [
"#方法一\n",
"def is_number(s):\n",
" try:\n",
" float(s)\n",
" return True\n",
" except ValueError:\n",
" pass\n",
" try:\n",
" import unicodedata\n",
" unicodedata.numeric(s)\n",
" return True\n",
" except(TypeError,ValueError):\n",
" pass\n",
" return \n",
"t=\"a12d3\"\n",
"print(is_number(t))\n",
"\n",
"\n",
"#方法二\n",
"t = \"q123\"\n",
"print(t.isdigit()) #检测字符串是否只由数字组成\n",
"\n",
"#方法三\n",
"t = \"123\"\n",
"print(t.isnumeric()) #检测字符串是否只由数字组成,这种方法是只针对unicode对象0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 判断奇偶数--学习这种输出匹配方法"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"请输入一个数字:3\n",
"3是奇数\n"
]
}
],
"source": [
"#方法一\n",
"num = int(input('请输入一个数字:'))\n",
"if (num % 2) == 0:\n",
" print(\"{0}是偶数\".format(num))\n",
"else:\n",
" print(\"{0}是奇数\".format(num))\n",
" \n",
"#方法二\n",
"while True:\n",
" try:\n",
" num = int(input('请输入一个整数:')) #判断输入是否为整数,不是纯数字需要重新输入\n",
" except ValueError:\n",
" print(\"输入的不是整数!\")\n",
" continue\n",
" if (num % 2) == 0:\n",
" print(\"{0}是偶数\".format(num))\n",
" else:\n",
" print(\"{0}是奇数\".format(num))\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 进制转换"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"输入数字:9\n",
"十进制数为: 9\n",
"转换为二进制为: 0b1001\n",
"转换为八进制为: 0o11\n",
"转换为十六进制为: 0x9\n"
]
}
],
"source": [
"# 获取输入十进制数\n",
"dec = int(input(\"输入数字:\"))\n",
"print(\"十进制数为:\", dec)\n",
"print(\"转换为二进制为:\", bin(dec))\n",
"print(\"转换为八进制为:\", oct(dec))\n",
"print(\"转换为十六进制为:\", hex(dec))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 日历模块"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"输入年份: 2020\n",
"输入月份: 12\n",
" December 2020\n",
"Mo Tu We Th Fr Sa Su\n",
" 1 2 3 4 5 6\n",
" 7 8 9 10 11 12 13\n",
"14 15 16 17 18 19 20\n",
"21 22 23 24 25 26 27\n",
"28 29 30 31\n",
"\n",
"(3, 30)\n"
]
}
],
"source": [
"# 引入日历模块\n",
"import calendar\n",
"\n",
"# 输入指定年月\n",
"yy = int(input(\"输入年份: \"))\n",
"mm = int(input(\"输入月份: \"))\n",
"\n",
"# 显示日历\n",
"print(calendar.month(yy, mm))\n",
"# 计算每个月天数\n",
"monthRange = calendar.monthrange(2016,9)\n",
"print(monthRange)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 简单文件IO"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"该文本会写入到文件中\n",
"看到我了吧!\n"
]
}
],
"source": [
"# 写文件\n",
"with open(\"test.txt\", \"wt\") as out_file:\n",
" out_file.write(\"该文本会写入到文件中\\n看到我了吧!\")\n",
"\n",
"# Read a file\n",
"with open(\"IOtest.txt\", \"rt\") as in_file:\n",
" text = in_file.read()\n",
"\n",
"print(text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 字符串内置函数"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"False\n",
"False\n",
"True\n",
"False\n",
"False\n",
"False\n",
"RUNOOB.COM\n",
"runoob.com\n",
"Runoob.com\n",
"Runoob.Com\n"
]
}
],
"source": [
"str = \"runoob.com\"\n",
"print(str.isalnum()) # 判断所有字符都是数字或者字母\n",
"print(str.isalpha()) # 判断所有字符都是字母\n",
"print(str.isdigit()) # 判断所有字符都是数字\n",
"print(str.islower()) # 判断所有字符都是小写\n",
"print(str.isupper()) # 判断所有字符都是大写\n",
"print(str.istitle()) # 判断所有单词都是首字母大写,像标题\n",
"print(str.isspace()) # 判断所有字符都是空白字符、\\t、\\n、\\r\n",
"print(str.upper()) # 把所有字符中的小写字母转换成大写字母\n",
"print(str.lower()) # 把所有字符中的大写字母转换成小写字母\n",
"print(str.capitalize()) # 把第一个字母转化为大写字母,其余小写\n",
"print(str.title()) # 把每个单词的第一个字母转化为大写,其余小写"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "797px",
"left": "42px",
"top": "110px",
"width": "371.594px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 4
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史
pythonisalpha函数计算公式_常用python实现.ipynb相关推荐
- 三刺激值计算公式_常用的车削、铣削、钻削加工计算公式全在这里了,随用随查...
点上面蓝色字体直观学机械可长期订阅我们 法律顾问:赵建英律师 01常用车削加工计算公式 ● 切削线速度Vc(m/min) Dm:加工直径,单位(mm) n:主轴转速,单位(rpm) ● 主轴转速 n ...
- turtle 函数 方法_学python第十一节:turtle深入 了解
学python第十一节:深入分析turtle Turtle是一个直观有趣的图形绘制函数. 这节课对turtle的以下几点进行补充: 在蟒蛇绘制代码中提到过import 库引用保留字的函数,是补充pyt ...
- python函数复用_【python学习-4】可复用函数与模块
1.自定义函数 自定义函数格式如下: def (参数列表):return #!/usr/bin/python#定义函数,打印数字1~5 defprintNum5():#range函数,生成列表集合,有 ...
- python函数示例_使用Python中的示例的input()函数
python函数示例 Python input()函数 (Python input() function) input() function is a library function, it is ...
- 三刺激值计算公式_常用的车削、铣削和钻削计算公式,转走不谢
01 常用车削加工计算公式 ● 切削线速度Vc(m/min) Dm:加工直径,单位(mm) n:主轴转速,单位(rpm) ● 主轴转速 n (rpm) Vc:切削线速度,单位(m/min) Dm:加工 ...
- python中append函数解析_对python中的pop函数和append函数详解
对python中的pop函数和append函数详解 pop()函数 1.描述 pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值. 语法 pop()方法语法: list. ...
- python中主函数用法_【Python 1-13】Python手把手教程之——详解函数和函数的使用...
- 作者 | 弗拉德 来源 | 弗拉德(公众号:fulade_me) 定义函数 下面是一个打印问候语的简单函数,名为greet_user(): def greet_user(): "&quo ...
- python常用函数中文_【python】python常用函数
urlencode与urldecode 当url中包含中文或者参数包含中文,需要对中文或者特殊字符(/.&)做编码转换. urlencode的本质:把字符串转为gbk编码,再把\x替换成%.如 ...
- python常用内置函数乘法_每个 Python 高手都应该知道的内置函数
每个 Python 高手都应该知道的内置函数 Python 将纳入高中教材,大学 VB 将被 Python 取代, 现在你还对 Python 一无所知吗?去年就被国外一机构预测, 2017 年 pyt ...
最新文章
- 《Hibernate 系列》- 入门例子
- 人工智能机器学习区别计算机应用,AI、人工智能和机器学习有什么区别
- 2018年Java生态行业报告
- 微软Entity Framework安装和初步研究
- 更新代码到gitlab上
- [转]C# winForm 自定义鼠标样式的两种方法
- php未定义要怎样做,php-Behat-未定义的功能步骤
- C. Sum of Log(数位dp)
- 飞腾 linux 内核,FT2004-Xenomai
- C#多线程编程系列(二)- 线程基础
- mysql 安装telnat_yum安装telnet服务
- python生成的词云没有图案_Python如何生成词云的方法
- “~/” 代表应用程序根目录的一点误区
- 带有输出参数的存储过程
- 终于找到了满意的博客主题
- Flutter实战一Flutter聊天应用(十)
- FaceWarehouse: a 3D Facial Expression Database for Visual Computing
- 在ASP.NET下做了一个实验MVC的小东西,希望能得到更多的参考意见
- yolov4-tiny目标检测模型实战——学生姿势行为检测
- 关于SQL模糊查询日期时间的方法,格式化日期时间年月日时分秒,去掉毫秒