美文网首页
python 基础知识

python 基础知识

作者: zenos876 | 来源:发表于2019-09-24 23:42 被阅读0次

    第一个python程序 hello world

    """
    hello world
    """
    name = input("What is your name: ")
    print("Hello %s" % name)
    import time
    time.sleep(2)
    
    What is your name: zenos
    Hello zenos
    

    字符串

    """
    测试字符串
    """
    
    # 1.转义成字符串
    print('{:=^50}'.format('1'))
    print('let\'s go')
    
    # 2.特殊的字符串拼接方式
    print('{:=^50}'.format('2'))
    print("hello" 'world')
    
    x = "hellp"
    y = 'world'
    # print(x y)
    
    # 3.字符串拼接
    print('{:=^50}'.format('3'))
    print(x+y)
    
    # 4.str repr
    print('{:=^50}'.format('4'))
    print(str("Hello,\n world"))
    # 类似直接交互式解释器输出
    print(repr("Hello,\n world"))
    
    
    # 5.长字符串
    print('{:=^50}'.format('5'))
    string = """
        Hello,
        world
    """
    print(string)
    
    # 6.反斜杠会被解释器忽略
    print('{:=^50}'.format('6'))
    print("Hello,\
    world")
    
    # 7.原始字符串
    print('{:=^50}'.format('7'))
    # string1 = 'C:\Users\87600\Desktop\python_study\MyPro01'
    # print(string1)
    string = r'C:\Users\87600\Desktop\python_study\MyPro01'
    print(string)
    
    # 8.最后一个字符为\
    print('{:=^50}'.format('8'))
    string = r'C:\Users\87600\Desktop\python_study\MyPro01' '\\'
    print(string)
    
    # 9.Unicode字符
    print('{:=^50}'.format('9'))
    string = '\u4e00'+'\u9FA5'
    print(string)
    string = '\N{CAT}'
    print(string)
    
    # 10.字节编码
    print('{:=^50}'.format('10'))
    # 为了更好的使用与类似C的语言进行互操作,将字符串转换为字节序列
    # python中字符串默认为unicode编码
    
    # 将字符串以ASCII规则进行编码成bytes即  unocode -> ASCII bytes
    print("Hello, world".encode("ASCII"))
    print("Hello, world".encode("UTF-8"))
    print("Hello, world".encode("UTF-32"))
    
    # 特殊字符不能以ASCII进行编码
    print("H哈llow, world".encode("ASCII", 'ignore'))
    print("H哈llow, world".encode("ASCII", 'replace'))
    print("H哈llow, world".encode("ASCII", 'xmlcharrefreplace'))
    
    # 但是可以使用utf-8编码
    print("H哈llow, world".encode('utf-8'))
    
    # 使用decode进行解码
    print("H哈llow, world".encode('utf-8').decode())
    
    # 11.bytes str对象
    print('{:=^50}'.format('11'))
    bytes_obj = bytes('Hello world', encoding='utf-8')
    str_obj = str(b'Hello world', encoding='utf-8')
    print('bytes_obj: ',bytes_obj)
    print('str_obj: ',str_obj)
    
    # 12.bytearray
    print('{:=^50}'.format('12'))
    x = bytearray(b'Hello')
    x[1] = ord(b'u')
    
    print(x)
    
    ========================1.========================
    2
    ========================2.========================
    2
    ======================2.2.1=======================
    Gumby
    24
    ======================2.2.2=======================
    2
    Gumby
    {'name': 'Gumby', 'age': 24, 'gender': True}
    {'name': 'Gumby', 'age': 24}
    True
    False
    {'name': 'Gumby', 'age': 24, 3.1: '实数', '3.1': '字符串', (3, 1): '元组'}
    ======================2.2.3=======================
    alice's phonenumber is 111
    {}
    {'name': 'alice', 'students': ['zenos', 'herry']}
    {'name': 'zenos', 'students': ['zenos', 'herry']}
    {'name': 'alice', 'students': ['zenos', 'herry']}
    {'name': 'zenos', 'students': ['herry']}
    {'name': 'alice', 'students': ['herry']}
    {'name': 'alice', 'students': ['zenos', 'herry']}
    {'name': 'zenos', 'students': ['zenos', 'herry']}
    {'name': 'alice', 'students': ['zenos', 'herry']}
    {'name': 'zenos', 'students': ['herry']}
    {'name': 'alice', 'students': ['zenos', 'herry']}
    {'name': 'zenos', 'age': 'zenos'}
    N/A
    dict_items([('name', 'zenos'), ('age', 'zenos')])
    PS C:\Users\87600\Desktop\python_basic_learning> python -u "c:\Users\87600\Desktop\python_basic_learning\1.basic_knowledge\1.hello_world.py"
    What is your name: zenos
    Hello zenos
    PS C:\Users\87600\Desktop\python_basic_learning> a^C
    PS C:\Users\87600\Desktop\python_basic_learning> python -u "c:\Users\87600\Desktop\python_basic_learning\1.basic_knowledge\2.test_str.py"
    ========================1=========================
    let's go
    ========================2=========================
    helloworld
    ========================3=========================
    hellpworld
    ========================4=========================
    Hello,
     world
    'Hello,\n world'
    ========================5=========================
    
        Hello,
        world
    
    ========================6=========================
    Hello,world
    ========================7=========================
    C:\Users\87600\Desktop\python_study\MyPro01
    ========================8=========================
    C:\Users\87600\Desktop\python_study\MyPro01\
    ========================9=========================
    一龥
    🐈
    ========================10========================
    b'Hello, world'
    b'Hello, world'
    b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00,\x00\x00\x00 \x00\x00\x00w\x00\x00\x00o\x00\x00\x00r\x00\x00\x00l\x00\x00\x00d\x00\x00\x00'
    b'Hllow, world'
    b'H?llow, world'
    b'H哈llow, world'
    b'H\xe5\x93\x88llow, world'
    H哈llow, world
    ========================11========================
    bytes_obj:  b'Hello world'
    str_obj:  Hello world
    ========================12========================
    bytearray(b'Hullo')
    

    相关文章

      网友评论

          本文标题:python 基础知识

          本文链接:https://www.haomeiwen.com/subject/jefuuctx.html