Python刷题

这篇博客介绍了Python的基础知识,包括两种方式导入模块`a_plus_b`并调用函数,使用`sys.argv[]`获取命令行参数,文件的读取操作,以及如何创建和写入列表。还讲解了使用`os`模块创建目录并写入文件,以及对输入数字进行排序的方法。这些内容涵盖了Python编程的基本操作。
摘要由CSDN通过智能技术生成

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_)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值