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'\u4e2d\u6587'
进制转换
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:\python\python.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'
Base64编码
"ni hao ya".encode("utf-8")
>>>b'ni hao ya'
base64.b64encode(b'ni hao ya')
>>>b'bmkgaGFvIHlh'
base64.b64decode(b'bmkgaGFvIHlh')
>>>b'ni hao ya'
str(b'ni hao ya',"utf-8")
>>>'ni hao ya'
Python3 默认为unioncode
而base64.b64encode()/base64.b64decode()的参数为byte8位带符号的二进制数
,在base64编码解码前需要先转码
#-*- coding: utf-8 -*-
#@Time : 2021/9/5 18:10
#@Author : HUGBOY
#@File : test.py
#@Software: PyCharm
import base64
say = "hello world"
# 编码解码
en_say = base64.b64encode(say.encode("utf-8"))
de_say = base64.b64decode(en_say)
# 去掉b''
en_say_b = str(en_say, "utf-8")
de_say_b = str(de_say, "utf-8")
print(say)
print(en_say)
print(de_say)
print(en_say_b)
print(de_say_b)
# 输出
D:\python\python.exe E:/PYTHON/0SEC/xxx/test.py
hello world
b'aGVsbG8gd29ybGQ='
b'hello world'
aGVsbG8gd29ybGQ=
hello world
Process finished with exit code 0
参考
________________________________________________________
Every good deed you do will someday come back to you.
Love you,love word !