hello python_Hello Python

本博客主要用来记录python的学习笔记

入坑

a,b=input().split()

print(int(a)+int(b))

print("Hello Python!")

format和%

a=1/3

print("%.3f"%(a))

url="xxxxxxxxxxxxx"

payload="cccccccccc"

print("website:{}; payload:{}".format(url,payload))

lstrip和rstrip和strip

'''

>>> a = ' welcome come to Beijing '

>>> a.strip()

'welcome come to Beijing'

>>> a.rstrip()

' welcome come to Beijing'

>>> a.lstrip()

'welcome come to Beijing '

'''

a = ' welcome come to Beijing \n'

print(a)

print(a.strip())

python的函数可以很方便的进行传列表的操作,并且代码非常简明易懂

python的文件操作

文件名xiangao.txt

文件内容:

We must Study Python to make this world a better place!

Why is Xiangao so strong,

I can't imagine!

测试程序1:

with open('xiangao.txt') as file_object:

print(file_object.read())

测试结果:

We must Study Python to make this world a better place!

Why is Xiangao so strong,

I can't imagine!

测试程序2:逐行读取

with open('xiangao.txt') as file_object:

for object in file_object:

print(object)

测试结果:

We must Study Python to make this world a better place!

Why is Xiangao so strong,

I can't imagine!

Process finished with exit code 0

测试程序3:逐行读取,消回车

with open('xiangao.txt') as file_object:

for object in file_object:

print(object.strip())

We must Study Python to make this world a better place!

Why is Xiangao so strong,

I can't imagine!

测试程序4:逐行读取并存储

with open('xiangao.txt') as file_object:

object=file_object.readlines()

for i in object:

print(i)

测试结果:

We must Study Python to make this world a better place!

Why is Xiangao so strong,

I can't imagine!

当我们需要进行写入文件操作时,调用open时必须提供另外一个参数,第一个参数文件的路径,第二个参数是'w'告诉Python我要进行写入操作,如果不加参数默认就是只读模式,还有附加模式'a',可读可写模式'r+'

测试程序:

with open('xiangao.txt','r+') as file_object:

# file_object.write('\nI must be his friend!')

object=file_object.readlines()

for i in object:

print(i)

这里出现了一些奇怪的小问题,但是我觉得无伤大雅。

try_except:

Python使用被称为异常的特殊对象来管理程序执行期间发生的错误。每当发生让Python不知所措的错误时,他都会创建一个异常对象。如果你编写了处理该异常的代码,程序将继续运行:如果你未对异常进行处理,程序将停止,并显示一个traceback,其中包含有关异常的报告。异常是使用try-except代码块进行处理的。try-except代码块让Python执行指定的操作,同时告诉Python发生异常时怎么办。使用try-except代码块,即使出现异常,程序也将继续运行,显示你编写的友好的错误消息,而不是令用户迷惑的traceback。

举个例子:

a,b=input().split()

a=int(a)

b=int(b)

print(a/b)

这是一个除法计算器:

但如果我们输入一个除数为0,则程序会报错:

Traceback (most recent call last):

File "D:\njc\SAFETY WEB\Python学习\try_except.py", line 4, in

print(a/b)

ZeroDivisionError: division by zero

这时就需要使用try except模块了

a,b=input().split()

a=int(a)

b=int(b)

try:

c=a/b

except:

print("0 is not allowed");

else:

print(c)

else中放的是当try中代码块执行成功后的后续执行操作

也可以这么写:

a,b=input().split()

a=int(a)

b=int(b)

try:

c=a/b

except ZeroDivisionError:

print("0 is not allowed");

else:

print(c)

运行结果:

D:\venv\Scripts\python.exe "D:/njc/SAFETY WEB/Python学习/try_except.py"

5 0

0 is not allowed

Process finished with exit code 0

D:\venv\Scripts\python.exe "D:/njc/SAFETY WEB/Python学习/try_except.py"

5 1

5.0

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值