zoukankan      html  css  js  c++  java
  • Python用法速查@IO和编码

    输入

    name=input('yourname:')

    #下面是python2中的raw_input()被python3重命名为了 input()
    >>> name = raw_input('please enter your name: ')
    please enter your name: ethan     # 输入一个字符串
    >>> name
    'ethan'
    >>> type(name)
    <type 'str'>
    >>>
    >>> num = raw_input('please enter your id: ')
    please enter your id: 12345       # 输入一个数值
    >>> num
    '12345'
    >>> type(num)
    <type 'str'>
    >>>
    >>> sum = raw_input('please enter a+b: ')
    please enter a+b: 3+6             # 输入一个表达式
    >>> sum
    '3+6'
    >>> type(sum)
    <type 'str'>
    
    

    sum=eval(input('a+b:'))

    #相当于python2中的input()
    >>> name = input('please input your name: ')
    please input your name: ethan         # 输入字符串如果没加引号会出错
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'ethan' is not defined
    >>>
    >>> name = input('please input your name: ')
    please input your name: 'ethan'       # 添加引号
    >>> name
    'ethan'
    >>>
    >>> num = input('please input your id: ')
    please input your id: 12345           # 输入数值
    >>> num                               # 注意返回的是数值类型,而不是字符串
    12345
    >>> type(num)
    <type 'int'>
    >>>
    >>> sum = input('please enter a+b: ')  # 输入数字表达式,会对表达式求值
    please enter a+b: 3+6
    >>> sum
    9
    >>> type(sum)
    <type 'int'>
    >>>
    >>> sum = input('please enter a+b: ')   # 输入字符串表达式,会字符串进行运算
    please enter a+b: '3'+'6'
    >>> sum
    '36'
    
    

    输出

    print()

    python3的print()相当于=>python2的print

    # Python 2.7.11 (default, Feb 24 2016, 10:48:05)
    >>> print 123
    123
    >>> print 'abc'
    abc
    >>> x = 10
    >>> print x
    10
    >>> d = {'a': 1, 'b': 2}
    >>> print d
    {'a': 1, 'b': 2}
    >>>
    >>> print(123)
    123
    >>> print('abc')
    abc
    >>> print(x)
    10
    >>> print(d)
    {'a': 1, 'b': 2}
    
    # 格式化
    >>> s = 'hello'
    >>> l = len(s)
    >>> print('the length of %s is %d' % (s, l))
    the length of hello is 5
    >>>
    >>> pi = 3.14159
    >>> print('%10.3f' % pi)     # 字段宽度 10,精度 3
         3.142
    >>> print('%010.3f' % pi)    # 用 0 填充空白
    000003.142
    >>> print('%+f' % pi)        # 显示正负号
    +3.141590
    
    # 换行输出
    >>> for i in range(0, 3):
    ...     print(i)
    ...
    0
    1
    2
    >>> for i in range(0, 3):
    ...     print(i, end='')    # 加上一个 end 参数
    ...
    012
    
    

    编码

    sys.getdefaultencoding()

    Python 3.5.2 (default, Jun 29 2016, 13:43:58)
    [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.getdefaultencoding()
    'utf-8'
    
    

    string.decode('utf-8')

    unicode_string.encode('utf-8')

    >>> '中文'.decode('utf-8')
    u'u4e2du6587'
    
    
    

    进制转换

    n = input("请输入一个数字")

    python的思路是先将input()输入的 十进制字符 先转为十进制,进而转为其他进制。

    十进制的 n => 通过bin(n)/oct(n)/hex(n)=>2/8/16进制的 n

    bin(n)

    oct(n)

    int(n)

    hex(n)

    2/8/16进制的 n => 通过int(n, 2)/int(n, 8)/int(n, 16)=>十进制的 n

    int(n, arg)

    #-*- coding: utf-8 -*-
    #@Time : 2021/9/5 18:10
    #@Author : HUGBOY
    #@File : test.py
    #@Software: PyCharm
    
    
    n = input("请输入一个数字:")
    
    int_n = int(n)
    
    #  十进制转2/4/8
    bin_n = bin(int_n)
    oct_n = oct(int_n)
    hex_n = hex(int_n)
    
    print(bin_n, oct_n, hex_n)
    
    #  2/4/8转十进制
    bin_to_int = int(bin_n, 2)
    oct_to_int = int(oct_n, 8)
    hex_to_int = int(hex_n, 16)
    
    print(bin_to_int, oct_to_int, hex_to_int)
    
    #  互相转换
    bin_to_oct = oct(int(bin_n, 2))
    bin_to_hex = hex(int(bin_n, 2))
    
    oct_to_bin = bin(int(oct_n, 8))
    oct_to_hex = hex(int(oct_n, 8))
    
    hex_to_bin = bin(int(hex_n, 16))
    hex_to_oct = oct(int(hex_n, 16))
    
    print(bin_to_int, bin_to_oct, oct_to_bin, oct_to_hex, hex_to_bin, hex_to_oct)
    
    # 结果
    D:pythonpython.exe E:/PYTHON/0SEC/xxxx/test.py
    请输入一个数字:16
    0b10000 0o20 0x10
    16 16 16
    16 0o20 0b10000 0x10 0b10000 0o20
    
    Process finished with exit code 0
    

    ASCII

    ord('a') => 97

    chr(97) => 'a'

    参考

    explore-python/Basic


    ________________________________________________________

    Every good deed you do will someday come back to you.

    Love you,love word !
  • 相关阅读:
    js截取字符串区分汉字字母代码
    List 去处自定义重复对象方法
    63. Unique Paths II
    62. Unique Paths
    388. Longest Absolute File Path
    41. First Missing Positive
    140. Word Break II
    139. Word Break
    239. Sliding Window Maximum
    5. Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/hugboy/p/io_coding.html
Copyright © 2011-2022 走看看