Python基础01-变量及数据类型

#整形int 浮点型float
x1=10
x2=10.0
print(x1,x2)
10 10.0
print(type(x1),type(x2))
<class 'int'> <class 'float'>
#字符串String
x3='hello world'
x4="hehe"
x5='''a
b
c
'''
print(x3,x4,x5)
hello world hehe a
b
c
#bool 布尔型
a=True
b=False
print(a==1)
print(b==0)
print(2>3)
True
True
False
#列表List
lst=[1,2,3,4,5]
print(lst,type(lst))
lst2=[1,2,5,'hello',[1,2,3]] #List里可以放多种数据类型
print(lst2[4],type(lst2[4]))
[1, 2, 3, 4, 5] <class 'list'>
[1, 2, 3] <class 'list'>
# 元祖Tuple
tup = (1,2,3,4,5)
lst = [1,2,3,4,5]
lst[0]=100
print(lst)
# tup[0]=100 会报错 元祖不能更改
[100, 2, 3, 4, 5]
#字典Dict
dic={'a':100,'b':'hello'}
print(dic,type(dic))
{'a': 100, 'b': 'hello'} <class 'dict'>
# 数据类型转换方法
var1=10
print(type(var1))
var2=float(var1)
print(var2,type(var2))
var3=str(var1)
print(var3,type(var3))
# print(var3+1) 会报错,因为不是数字
var4=100.456
print(int(var4))
<class 'int'>
10.0 <class 'float'>
10 <class 'str'>
100
'''
变量命名规则:
1. 变量名第一个字符必须是字母(大小写均可)或者下划线,不能数字开头
2. 变量名不能和常用功能性名字重合,例如print,if,for
3. 不要有空格
'''
# 多变量赋值

a=b=c=1
d,e,f=1,2,'hello'
print(a,b,c,d,e,f)
1 1 1 1 2 hello
发布了48 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_37551036/article/details/102528865
今日推荐