python2逐步转向python3之小程序实践学习

     最近提到了python版本的转换,之后的项目不得不使用python3版本来作为主要的版本了,所以现在抽点时间来学习一下python3的语法,毕竟2和3的差距还是很大的,关于2的教程和书籍铺天盖地有很多,但是关于3的学习资料就显得相对匮乏了很多,这里简单的实践了一些具体如下:

#!usr/bin/env python
# encoding:utf-8


'''
__Author__:沂水寒城
功能:之前一直使用python2,现在要转手使用python3,拿小程序练习一下
'''


import math
import cmath
import urllib
import chardet
import unicodedata
import lxml



def calNumSqrt(one_num):
    '''
    求一个数字的平方根
    '''
    if one_num >= 0:
        res = math.sqrt(one_num)
    else:
        res = cmath.sqrt(one_num)
    print('{0} Sqrt is:{1}'.format(one_num,res))


def judgeNumber(one_str):
    '''
    判断字符串是否是数字
    '''
    flag = None
    try:
        if type(eval(one_str)) == int or type(eval(one_str)) == float:
            flag = True
    except:
        flag=False
    if flag:
        print('{} is number'.format(one_str))
    else:
        print('{} is not number'.format(one_str))


def judgeRunYear(one_year):
    '''
    判断指定年份是否是闰年
    闰年判断条件:
    整百年能被400整除的是闰年、非整百年能被4整除的为闰年
    '''
    flag = None
    if one_year % 100 == 0:
        if one_year % 400 == 0:
            flag = True
        else:
            flag = False
    else:
        if one_year % 4 == 0:
            flag = True
        else:
            flag = False
    if flag:
        print('{} is RunYear'.format(one_year))
    else:
        print('{} is not RunYear'.format(one_year))


def judgeZhiNum(one_num):
    '''
    判断给定数字是否是质数
    '''
    flag = None
    if type(one_num) == str:
        flag = False
    elif one_num == 1 or one_num == 2:
        flag=True
    elif one_num < 1:
        flag = False
    else:
        for i in range(2,(one_num)):
            if one_num % i == 0:
                flag = False
                break
            else:
                flag = True
    if flag:
        print('{} is ZhiNum'.format(one_num))
    else:
        print('{} is not ZhiNum'.format(one_num))


def mutiTable(num):
    '''
    乘法表
    '''
    for i in range(1,num):
        for j in range(1,i+1):
            print('{}x{}={}\t'.format(j, i, i*j), end = '')
        print()


def armStrongNum(one_num):
    '''
    判断给定数字是否是阿姆斯特朗数
    '''
    flag = None
    if type(one_num) == str:
        try:
            if type(eval(one_num)) == int:
                flag = True
                one_num = int(one_num)
        except TypeError:
            flag = False
    else:
        flag = True
    if flag:
        length=len(str(one_num))
        tmpNum=0
        num_list = list(str(one_num))
        for i in range(len(num_list)):
            tmpNum += math.pow(int(num_list[i]),length)
        if tmpNum == one_num:
            print('{} is armStrongNum'.format(one_num))
        else:
            print('{} is not armStrongNum'.format(one_num))
    else:
        print('{} is not armStrongNum'.format(one_num))


def asciiTransfomer(one_ele):
    '''
    ASCII码转换器
    '''
    if type(one_ele) == str:
        print('{0} ASCII is {1}'.format(one_ele,ord(one_ele)))
    else:
        print('{0} corresponding char is {1}'.format(one_ele,chr(one_ele)))


def oneLineOut():
    '''
    python可以多行代码使用;进行分割写在同一行中
    '''
    one_list = [1,3,5]; two_list = [2,4,6]; print('one+two is:',one_list+two_list)


def numPlay():
    '''
    数字类型转化实践
    '''
    one_num=9
    print('{0} to float is :{1}'.format(one_num,float(one_num)))
    print('{0} to complex is :{1}'.format(one_num,complex(one_num)))
    one_num=-23
    print('{0} to abs is :{1}'.format(one_num,abs(one_num)))
    one_num=5.612302
    print('{0} up is :{1}'.format(one_num,math.ceil(one_num)))#上界
    print('{0} down is :{1}'.format(one_num,math.floor(one_num)))#下界
    print('{0} log is :{1}'.format(one_num,math.log(one_num)))
    print('{0} log is :{1}'.format(one_num,math.modf(one_num)))#整数、小数分离
    print('{0} round is :{1}'.format(one_num,round(one_num,5)))
    print('{0} sqrt is :{1}'.format(one_num,math.sqrt(one_num)))
    pi=math.pi
    e=math.e
    print('{0} is :{1}'.format('pi',pi))
    print('{0} is :{1}'.format('e',e))


def strPlay():
    '''
    字符串类型转化实践
    '''
    one_str='university'
    if one_str.startswith('u'):
        print('startswith')
    if one_str.endswith('y'):
        print('endswith')
    if one_str.find('e') == -1:
        print('Not contain e')
    else:
        print('Contain e,index is:{}'.format(one_str.index('e')))
    if one_str.isalnum():
        #如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False
        print('isalnum')
    if one_str.isdigit():
        print('isdigit')
    if one_str.islower():
        #如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
        print('islower')
    if one_str.isupper():
        #如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
        print('isupper')
    if one_str.isnumeric():
        #如果字符串中只包含数字字符,则返回 True,否则返回 False
        print('isnumeric')
    if one_str.istitle():
        #如果字符串是标题化的(见 title())则返回 True,否则返回 False
        print('istitle')
    print('{0} upper is: {1}'.format(one_str,one_str.upper()))
    print('{0} lower is: {1}'.format(one_str,one_str.lower()))
    print('{0} - join is: {1}'.format(one_str,'-'.join(list(one_str))))


def exceptionFunc():
    '''
    异常的处理
    '''
    one_name="程咬金"
    try:
        one_name=one_name.encode('utf-8')
        print('name is: {}'.format(one_name))
        print('name encoding style is: {}'.format(chardet.detect(one_name)))
    except:
        one_name=one_name.encode('gbk')
        print('name is: {}'.format(one_name))
        print('name encoding style is: {}'.format(chardet.detect(one_name)))
    else:
        print('name is: {}'.format(one_name))
        print('name encoding style is: {}'.format(chardet.detect(one_name)))
    finally:
        print('exceptionFunc finished!!!')





if __name__=='__main__':
    calNumSqrt(16)
    calNumSqrt(-36)
    calNumSqrt(40)
    print('-*'*30)
    judgeNumber('apple')
    judgeNumber('3.14159265354')
    judgeNumber('1e3')
    print('-*'*30)
    judgeRunYear(2000)
    judgeRunYear(2004)
    judgeRunYear(2015)
    print('-*'*30)
    judgeZhiNum(1)
    judgeZhiNum(5)
    judgeZhiNum(8)
    print('-*'*30)
    mutiTable(4)
    mutiTable(6)
    mutiTable(10)
    print('-*'*30)
    armStrongNum(153)
    armStrongNum(666)
    armStrongNum(1634)
    print('-*'*30)
    asciiTransfomer('A')
    asciiTransfomer(112)
    asciiTransfomer('Z')
    print('-*'*30)
    oneLineOut()
    print('-*'*30)
    numPlay()
    print('-*'*30)
    strPlay()
    print('-*'*30)
    exceptionFunc()
    print('-*'*30)
   

     结果如下:

16 Sqrt is:4.0
-36 Sqrt is:6j
40 Sqrt is:6.324555320336759
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
apple is not number
3.14159265354 is number
1e3 is number
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2000 is RunYear
2004 is RunYear
2015 is not RunYear
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1 is ZhiNum
5 is ZhiNum
8 is not ZhiNum
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1x1=1
1x2=2   2x2=4
1x3=3   2x3=6   3x3=9
1x1=1
1x2=2   2x2=4
1x3=3   2x3=6   3x3=9
1x4=4   2x4=8   3x4=12  4x4=16
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25
1x1=1
1x2=2   2x2=4
1x3=3   2x3=6   3x3=9
1x4=4   2x4=8   3x4=12  4x4=16
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25
1x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=36
1x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49
1x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64
1x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
153 is armStrongNum
666 is not armStrongNum
1634 is armStrongNum
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
A ASCII is 65
112 corresponding char is p
Z ASCII is 90
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
one+two is: [1, 3, 5, 2, 4, 6]
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
9 to float is :9.0
9 to complex is :(9+0j)
-23 to abs is :23
5.612302 up is :6
5.612302 down is :5
5.612302 log is :1.7249609740496388
5.612302 log is :(0.6123019999999997, 5.0)
5.612302 round is :5.6123
5.612302 sqrt is :2.369029759205232
pi is :3.141592653589793
e is :2.718281828459045
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
startswith
endswith
Contain e,index is:4
isalnum
islower
university upper is: UNIVERSITY
university lower is: university
university - join is: u-n-i-v-e-r-s-i-t-y
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
name is: b'\xe7\xa8\x8b\xe5\x92\xac\xe9\x87\x91'
name encoding style is: {'encoding': 'utf-8', 'confidence': 0.87625, 'language':
 ''}
name is: b'\xe7\xa8\x8b\xe5\x92\xac\xe9\x87\x91'
name encoding style is: {'encoding': 'utf-8', 'confidence': 0.87625, 'language':
 ''}
exceptionFunc finished!!!
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    继续学习,记录一下! 

猜你喜欢

转载自blog.csdn.net/Together_CZ/article/details/83039360