The basic data types Python basis [actual constants, variables, numbers, points, precision, strings, byte strings, function]

Since real source reference "Python program design (2nd edition)" (Dong Fu country, Tsinghua University Press)

PS: Irish_Moonshine is testing a self-learning method, which is currently the basis for learning Python and Python data analysis study. After the test is completed the test results will be shared with you! ! !

import and distinction from math import sin math from math import * and the
import math library introduced all, and all methods must use math. ()
from math import * likewise introduced all the library, but does not require all of the is math. ()
from math Import sin sin introduces the math module, the method does not require the use of math

As double as we should avoid direct representation equivalent to two real numbers, and the absolute value of the method should be used.

Fraction score Object

from math import *
import random
from fractions import Fraction #分数
from fractions import Decimal #高精度实数(高精度浮点数)

x = type('Hello World!') #输出字符串类型
y = type(b'Hello World') #b表示字节串
z = 'Hello world'.encode('utf-8') #使用utf-8编码格式进行编码
w = 'Hello world'.encode('gbk') #使用gbk编码格式进行编码
o = '爱尔兰月光'.encode('utf-8') #对中文进行编码
q = '爱尔兰月光'.encode('gbk') #对bytes字节串进行编码

#m = _.decode('gbk') #该指令无法使用

print(x)
print(y)
print(z)
print(w)
print(o)
print(q)
#print(m) #同上无法使用,所以无法打印

x = 'Hello Wolrd!'
y = "Python is a great language."
z = '''Irish_Moonshine's programming.'''
print(x)
print(y)
print(z)
z = x + y
print(z)#类似于C++ String
x = 'good''morning'#可以实现连接
print(x)
x = 'good'+'morning'
print(x) #与上一语句等价

tem = Decimal(1/9)
print(tem);
tem = Decimal(1/3)
print(tem);
tem = Decimal(1/9)+Decimal(1/3)
print(tem);


x=Fraction(3,5)
y=Fraction(3,7)
z=Fraction(3.5)#小数转换成实数
print(x);
print(y);
print(z);
print(x.numerator);#查看分子
print(x.denominator);#查看分母
print(Fraction(x+y))#四则运算自动通分






a = sin(0.5)
r = random.random()
rr = random.randint(1,100)
rrr = random.randrange(1,100000);
print(r)
print(rr)
print(rrr)
print(a)
print(pi)
print(e)
print(radians(180)) #角度转弧度


def main():
        if __name__=='__main__':
            print("This program is run directly.")
        elif __name__=='hello':
            print('This program is used as a module.')
        #fi



main()

Guess you like

Origin blog.csdn.net/Irish_Moonshine/article/details/91831872