python 数据类型、运算符

# print
# 输出数字、字符串
print(12.2)
print('hello')
print("hi")

#含有运算符的表达式
print(1+2)

# 输出到文件中
fp = open('D:/TEXT.txt', 'a+') # a+ 文件存在就续写,文件不在就创建
print('just for test',file = fp)
fp.close()

# 输出内容在一行,不换行
print('hello',"好",2)

#==============================================================================
# 转义字符
print('hello\tworld')
print('helloooo\tworld') # 注:\t占几格?
print('hello\rworld') # \r return覆盖
print('hi\rworld')
print('hello\bworld') # \b 退一格

print('https:\\\\www.baidu.com')
print('老师说:\'大家好\'')

# 原字符:使转义字符无效:在字符串前加 r 或 R
print(r'hello\nworld')

# 保留字
import keyword
print(keyword.kwlist)

# 变量
name = 'LPA'
print(name)
print(id(name)) # 变量的地址
print(type(name)) # 变量的类型

# 浮点数运算
from decimal import Decimal
print(Decimal('1.1')+Decimal('2.2'))

# 字符串
str1 = 'blacck stones'
str2 = "black stones"
str3 = '''black stones
blast'''
str4 = """black stones
blast"""
print(str1,str2,str3,str4)

#==============================================================================
# 数据类型转换
# 转为 str: str()
a = 10
b = 1.11
c = False
print(str(a), str(b), str(c))

# 转为 int: int() 字符串且整串可转
a = '10'
b = '1.11'
c = True
d = 'hello'
e = 3.3
print(int(a))
# print(int(b))
print(int(c))
# print(int(d))
print(int(e))

# 转为 float: float() 非浮点自动 +.0
a = '10'
b = '1.11'
c = True
d = 'hello'
f = 12
print(float(a))
print(float(b))
print(float(c))
#print(float(d))
print(float(f))

#==================================================
# 多行注释: '''xxxxxx'''
'''多
行
注
释'''
# 中文编码声明注释:在 .py文件最前边写:#coding:gbk
# 参考视频: https://www.bilibili.com/video/BV1wD4y1o7AS?p=19&spm_id_from=pageDriver

# From : bilibili 课程
# 时 间 :

# input() type: str
'''a = input('输入第一个数:')
a = int(a)
b = input('输入第二个数:')
b = int(b)
print('两数之和是:',a+b)'''

# 运算符
print(1/2) # 除法
print(11//2) # 整除
print(11%2) # 取余
print(2**3) # 幂

# 负数参与运算
print(9//4) # 2 √
print(-9//-4) # 2 √
print(-9//4) # -3 × 一正一负整除结果为向下取整的数
print(-9%4) # 3 × 余数=被除数-除数*商 -9-4*(-3)

# 链式赋值
a=b=c=10
print(a,id(a))
print(b,id(b))
print(c,id(c)) # 地址同

# 系列解包赋值
a, b, c = 1,2,3
print(a,b,c)
a,b = 1,2
a,b = b,a # 交换
print(a,b)

# 比较值:== >= !=
# 比较对象: is (说明地址同)
a = 'layla'
b = 'layla'
print(a is b) # True a与b的id同
print(a is not b) # False

lst1 = [1,2,3,4]
lst2 = [1,2,3,4]
print(lst1 == lst2) # True
print(lst1 is lst2) # False
print(id(lst1))
print(id(lst2))

# 布尔运算符:比较两个bool
# and, or, in, not in
a,b = 1,2
print(a==1 and b==1) # False
print(a==1 or  b==1) # True

q = True
w = False
print(not q) # False

str = 'black stones'
print('a' in str) # True
print('blast' not in str) #True

# 位运算符 转为二进制后的运算
# &, |, <<, >>
print(4 & 8) # 0 按位与:only 1&1=1
print(4 | 8) # 12 按位或: only 0|0=0
print(4<<1) # 8 将4向左移一位
print(4>>1) # 2 将4向右移一位

# 运算优先级:算数 先于 位 先于 比较 先于 布尔运算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值