zoukankan      html  css  js  c++  java
  • Hello Python

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

    入坑

    a,b=input().split()
    print(int(a)+int(b))
    
    print("Hello Python!")
    

    format和%

    Python中%s、%d、%f意义及用法详解
    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           
    '
    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('
    I 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:
    jcSAFETY WEBPython学习	ry_except.py", line 4, in <module>
        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:venvScriptspython.exe "D:/njc/SAFETY WEB/Python学习/try_except.py"
    5 0
    0 is not allowed
    
    Process finished with exit code 0
    
    
    D:venvScriptspython.exe "D:/njc/SAFETY WEB/Python学习/try_except.py"
    5 1
    5.0
    
    Process finished with exit code 0
    
    
  • 相关阅读:
    Linux----硬连接和软连接
    C4.5算法
    浅谈机器学习方法
    ID3算法(决策树)
    修改mysql数据库 允许远程访问
    CentOS 7安装Samba 4.6 版本步骤及错误解决方法
    CentOS安装mysql
    eclipse 安装lombok插件
    电脑忘记密码,破解方式
    mysql常用的信息查询函数
  • 原文地址:https://www.cnblogs.com/mudrobot/p/13869964.html
Copyright © 2011-2022 走看看