Variables and data types
variable:
# a 我们就称之为变量 (使用一个名字 来代替一段内容)
a = "你好"
print(a)
Why do we use variables?
If we say: we have the following code
print("你好")
print("你好")
print("你好")
print("你好")
print("你好")
We want to change "Hello" to Hi, so we can only change one by one. If we use variables
Define an a=Hi, put what you want in a, but the variables are also standardized
a="Hi"
print(a)
print(a)
print(a)
print(a)
print(a)
But why does this report an error? Why some can be used and some cannot be used
c = yes
print(c)
This involves the concept of data types
In our Python, the data has its own corresponding type:
# 数字类型 : 整数型 int 浮点型float 复数 complex
print(45) # int整数类型
print(3.1415) # float浮点类型
print((-1) ** 0.5) # 复数 complex
# 字符串类型 python里的字符串要求使用一对单引号,或者双引号来包裹
print("我一只小鸭子,咿呀咿呀吆")
print('门前大桥下,游过一群鸭')
# 布尔类型 表示 真假 / 对错(注意大小写)
# 只有两个值 True 和 False
print(5 > 3) # True
print(5 > 9) # False
# 列表类型
names = ['拉拉','哈哈','嘻嘻','哼哼']
# 字典类型
person = {
'name':'李奎','age':22,'addr':'山东'}
# 元组类型
num=(1,2,3,4,5,6)
# 集合类型
j={
9,'你好',True,9.2}
print('门前大桥下,游过一群鸭')
type of data:
View data type:
Use the type built-in class to view the data type of the current variable
a = 34
b = '你好'
c = True
d = {
'早','啊','队','长'}
e = ['啦啦','哈哈','嘻嘻']
f = (1,2,3,4)
# 使用type内置类可以查看当前变量的数据类型
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
# 在Python里变量是没有数据类型的
# 我们所说变量的数据类型,其实是变量对应的值的数据类型
m = 23;
print(type(m))
m= "HI"
print(type(m))