Python数据结构基础(一)——变量(Variable)

版权声明:本文版权归作者所有,未经允许不得转载! https://blog.csdn.net/m0_37827994/article/details/86490614

一、变量

变量是Python中的对象,可以容纳任何带有数字或文本的对象。
变量分为整数型(int)浮点型(float)字符串(str)布尔型(bool)

  • 整数型直接写数字
  • 浮点型记得数字后面要加小数点(.)
  • 字符串要加双引号(“ ”)
  • 布尔型即True/False(1/0)
    注:如果想知道已知数的类型,在编程时用 print(type())

Pratice 1:

# int variable
x=5
print(x)
print(type(x))

# float variable
x = 5.0
print (x)
print (type(x))

# text variable
x = "5" 
print (x)
print (type(x))

# boolean variable
x = True
print (x)
print (type(x))

输出结果:

5
<class 'int'>
5.0
<class 'float'>
5
<class 'str'>
True
<class 'bool'>

Pratice 2:

# int variables
a = 5
b = 3
print (a + b)

# string variables
a = "5"
b = "3"
print (a + b)

输出结果:

8
53

猜你喜欢

转载自blog.csdn.net/m0_37827994/article/details/86490614