栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python刷题

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python刷题

Python刷题
    • import的使用
    • sys函数的使用
    • 打开文件并写入
    • 创建列表并写入
    • os模块
    • sort排序

import的使用
#第一种
from a_plus_b import plus
print(plus(a, b))
#第二种
import a_plus_b
print(a_plus_b.plus(a, b))
sys函数的使用

sys.argv[]
Python 中可以使用 sys 的 sys.argv[] 来获取命令行参数:
sys.argv 是命令行参数列表
len(sys.argv) 计算命令行参数个数
注:sys.argv[0] 表示脚本名。

打开文件并写入
with open(input_filepath, 'r') as f: #这一串当作f,with是打开自动关闭
    content = f.read()
创建列表并写入
lst = [int(i) for i in input('请输入一组数字,用空格隔开: ').split(' ')]
lst = []
for i in input('请输入一组数字,用空格隔开: ').split():   # split 默认切割空格,所以可以不加参数
    lst.append(int(i)) #int(i)是把i转为整数
os模块
import os 
def write_to_file(filepath):
    path = os.path.dirname(filepath)#dirname命令去除文件名中的非目录部分,仅显示与目录有关的内容
    if  not os.path.exists(path):
        os.makedirs(path)#makedirs是创建目录
    with open(filepath,'w') as f:
        f.write('Hello World!')
sort排序
#注意input输入的是字符串
n = int(input())
A = input()
A = [int(i) for i in A.strip().split()] #strip是用于移除开头或是结尾的指定的字符(默认为空格或换行符)或字符序列。
A.sort()
str_ = ''
for i in range(n):
    str_+= str(A[i]) + ' '
print(str_)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号