Python学习第二天之数据类型

以下代码为学习时练习所写,部分输出以注释方式写在了代码之后,大家可以参考,还是推荐大家可以自己敲一遍,加深印象。

有任何疑问欢迎留言探讨。

# 机构:家里蹲
# 作者:青火
# 时间:2021/7/13 23:14
import keyword

#python中的保留字
print(keyword.kwlist)
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del',
# 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
# 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']


#标识符
#如变量名,函数名,文件名,模块名等起的名字就叫标识符
#规则:
# 1、字母,数字,下划线
# 2、不能以数字开头
# 3、不能用python的保留字
# 4、严格区分大小写

#变量
name = '北京'
print(name)
print('标识: ', id(name)) #标识,内存中的地址
print('类型: ', type(name)) #类型,说明变量所指数据的类型
print('值: ', name)

name = '南京' #重复复制,变量指向新的内存空间
print('标识: ', id(name)) #标识,内存中的地址
print('类型: ', type(name))
print(name)

#常见数据类型
#整数 int eg: 98 (正数,负数,0)
n1 = 90
n2 = -76
n3 = 0
print(n1, type(n1))
print(n2, type(n2))
print(n3, type(n3))
print('十进制: ', 110)
print('二进制: ', 10101101)
print('二进制: ', 0b10101101) # 二进制以0b开头
print('八进制: ', 0o176) #八进制以0o开头
print('十六进制: ', 0x1EAF) #十六进制以0x开头


#浮点类型 folat eg: 3.1415926
a = 3.14159
print(a, type(a))
n1 = 1.1
n2 = 2.2
print(n1 + n2) #3.3000000000000003
#浮点数因为存储方式可能会出现精度不准确情况,需用以下方式处理,可得准确数值
from decimal import Decimal
print(Decimal('1.1') + Decimal('2.2'))

#布尔类型 bool eg: true
f1 = True #表示为整数1
f2 = False #表示为整数0
print(f1)
print(f1 + 1) # True 转为整数运算
print(f2)
print(f2 + 1) # False 转为整数运算

#字符串类型 str eg: 'hello world'
#可用单引号,双引号,三引号来标识
str1 = '我要单引号'
str2 = "我是双引号"
#三引号可以跨行来写
str3 = """我是三引号
我自豪了吗
我骄傲了吗"""
str4 = '''我是三引号
我自豪了吗
我骄傲了吗'''
print(str1)
print(str2)
print(str3)
print(str4)

#数据类型转换
#str()
name = '张三'
age = 20
print(name, type(name))
print(age, type(age))
#print('我是' + name + ', 今年' + age + '岁' ) #TypeError: can only concatenate str (not "int") to str
print('我是' + name + ', 今年' + str(age) + '岁')  #利用str()函数将int转换为str类型
print(type(str(age))) #<class 'str'>
print(type(str(f1))) #将波尔类型True转为字符串True

#int()
s1 = '128'
f1 = 123.3
s2 = '125.6'
f2 = False
s3 = 'hello'
print(type(s1), type(f1), type(s2), type(f2), type(s3))
print(int(s1), type(int(s1))) #将str转换为int类型,字符串为 数字型
print(int(f1), type(int(f1))) #int()转换folat类型省去小数位
#print(int(s2), type(int(s2))) #报错, int()函数不可转换小数字符串
print(int(f2), type(int(f2))) #int()转换bool类型,True->1, False->0
#print(int(s3), type(s3)) #int()转换字符串类型时,字符串必须为数字串,也不能是小数串,否则报错

#float()
#文字类型不可转
#int类型转换后加.0
s1 = '128.28'
s2 = '76'
f2 = False
s3 = 'hello'
i = 100
print(type(s1), type(s2), type(f2), type(s3), type(1)) #<class 'str'> <class 'str'> <class 'bool'> <class 'str'> <class 'int'>
print(float(s1), type(float(s1))) # 128.28 <class 'float'>
print(float(s2), type(float(s2))) #76.0 <class 'float'>
print(float(f2), type(float(f2))) #0.0 <class 'float'>
#print(float(s3), type(float(s3))) #ValueError: could not convert string to float: 'hello'
print(float(i), type(float(i))) #100.0 <class 'float'>

#注释,解释代码,提高代码可读性
#python中没有专门的多行注释,三引号之中的类似于注释
'''我是
多行
注释'''
#文件开头加上中文声明注释,用以指定源码文件的编码格式,如:
#coding:gbk

扫码关注公众号“JAVA记录册”

该公众号致力于为大家分享工作中会用到一些简单实用的小知识,而不是那些悬在云端的高大上但又感觉空泛的文章,欢迎大家关注,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值