Python教程

python基础-03

本文主要是介绍python基础-03,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

python基础-03

# Python number

# Python There are three types of numbers
"""
int
float
complex
"""
x = 1    # int
y = 2.8  # float
z = 1j   # complex

# If you want to verify the type of a variable,you can use the type() function
print(type(x))
print(type(y))
print(type(z))

# int
# int integer,is a integer.It could be positive or negative,no decimal,and unlimited length.
x = 1
y = 3562254887
z = -35522
print(type(x))
print(type(y))
print(type(z))

# float
# It could be contains decimal places,and could be positive or negative.
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))

# scientific notation
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
# unreliable figure
# It has "J" as the imaginary part
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))

# type conversion
x = 1    # int
y = 2.8  # float
z = 1j   # complex
a = float(x)
b = int(y)
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))

# random number
# python有一个内置模块random可以用来生成随机数
# 示例:导入random模块,并显示1到10之间的一个随机数:
import random
print(random.randrange(1,11))

"""
Test
x 转换成浮点数
1.x = 5
x = float(x)

x 转换成整数
2.x = 5.5
x = int(x)

x 转换成复数
3.x = 5
x = complex(x)
"""
x = 5
x = float(x)
print(x)
print(type(x))
x = 5.5
x = int(x)
print(x)
print(type(x))
x = 5
x = complex(x)
print(x)
print(type(x))
这篇关于python基础-03的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!