Python3的例子练习和文件编码格式

习题:九宫格
number = list()
for i in range(1,10):
    number.append(i)
print(number)

count = 1
for A in number:
    a = number.copy()
    a.remove(A)
    for B in a:
        b = a.copy()
        b.remove(B)
        for C in b:
            c = b.copy()
            c.remove(C)
            for D in c:
                d = c.copy()
                d.remove(D)
                for E in d:
                    e = d.copy()
                    e.remove(E)
                    for F in e:
                        f = e.copy()
                        f.remove(F)
                        for G in f:
                            g = f.copy()
                            g.remove(G)
                            for H in g:
                                h = g.copy()
                                h.remove(H)
                                for I in h:
                                    if (A+B+C == D+E+F == G+H+I == 15) and (A+D+G == A+E+I == 15) and (B+E+H == 15) and (C+E+G == C+F+I == 15):
                                        print('''
                                        第{9}方法:
                                        {0} {1} {2}
                                        {3} {4} {5}
                                        {6} {7} {8}
                                       '''
                                            .format(A,B,C,D,E,F,G,H,I,count)
                                              )
                                        count += 1


总共有8个。
习题2:
A+B+C+D = D+C+B+A

 
 
 for A in [1]:     for B in range(0,10):         for C in range(0,10):             for D in [9]:                 if ((1000*A + 100*B +10*C + D)*9 == 1000*D + 100*C + 10*B + A ):                     print("A = {0}".format(A))                     print("B = {0}".format(B))                     print("C = {0}".format(C))                     print("D = {0}".format(D))                     print("{0}{1}{2}{3}x9={4}{5}{6}{7}".format(A,B,C,D,D,C,B,A))                     #或者:                     print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A,B,C,D)) 

Python3 的编码问题 改进的特别好。Python2 就有很多问题。
目前使用支持的编码格式有3个:utf-8 gbk gb-2312
python 2的默认编码格式就是ASCII,所以不支持中文
print(sys.getdefaultencoding())

需要识别中文的话 要在文件的头部增加# -*- coding: UTF-8 -*-
print(sys.getdefaultencoding())
print("文件编码")
使用Python2来运行这个段代码会出现乱码
如果指定格式就不会可以在字符串前加一个u来表示安装uft-8编码来存放。
print(sys.getdefaultencoding())
print(u"文件编码")
还可以使用解码方式来正确显示中文
a = "文件编码"
print(a.decode("utf-8"))

python 会自动的先将字符串解码为unicode ,然后在编码成gbk,因为解码是Python自动进行的,我们没有指明解码方式,Python 就会使用sys.defaultencoding指明的方式来解码。
还可以用这种方式:
s = "中文"
print(s.decode("utf-8").encode("gbk"))
先解码utf-8 在编码gbk,就可以显示了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值