
x = 1 # 定义整型变量
_x = "Hello" # 定义字符串变量
print(x) # 输出:1
a,b,c=4,5,6
print(a)
print(b, c)
var 和 Var 不同)。
if, for)。
a = 10 # 合法
_2b = 20 # 合法
# 2c = 30 # 非法(首字符为数字)a, b, c = 4, 5, 6 # 多变量赋值
print(a, b, c) # 输出:4 5 6
w = 123 # 整型
w = 5.21 # 变为浮点型
print(id(w)) # 内存地址变化(通过 id() 查看)
10)。
3.14)。
w=123
print(w)
123
w=5.21
print(w)
5.21
w=123
print(id(w))
w=5.21
print(id(w))
True(真) / False(假)。
print(10 > 5) # 输出:True
print(5 == 3) # 输出:False+, -, *, /, %(取余), **(幂)。
print(8 % 5) # 取余 → 3
print(2 ** 3) # 幂运算 → 8
print((6+5)*4) # 括号优先 → 44
x,y=10,2
print(x+y,x-y,x*y,x/y)
print(2+4*8)
print((6+5)*4)
\n(换行)、\t(制表符)。
s1 = '单引号'
s2 = "双引号"
s3 = '''多行
字符串'''* 重复字符串。
name='王五'
address="长安街"
content='''欢迎来到北京'''
print (name)
print(address)
print(content)
print(3 * "a") # 输出:aaa
[] 包裹,元素以逗号分隔。
nums = ['111', '222', '333'] # 定义列表append()(末尾添加)、insert()(指定位置插入)。
del 删除元素
nums = ['111', '222', '333'] # 定义列表
print(nums[0]) # 输出:111
nums[0] = "001" # 修改元素
nums.append("555") # 末尾添加
del nums[1] # 删除元素
print(nums[1])
num=['111','222','333','444']
print(num[0:1])
print(num[0:3])
print(num[1:2])
print(num[2:])
num=['111','222','333','444']
num.insert(1,'001')
print(num)
>>> num=['111','222','333']
>>> ('111')in num
True
>>> ('444')in num
Falsenum1=['111','222']
num2=['333','444']
numAll=(num1+num2)
print(numAll)
numAll=(num2+num1)
print(numAll)
num1=['111','222']
num=(num1*5)
print(num)
() 包裹,元素不可修改。
t = ('111', '222', '333') # 定义元组num=('111','222','333','444') #元组
listNum =list(num) #转换为列表
print (listNum)
(listNum[0])='001' #修改列表
print( listNum)
print (type(num)) #输出元组类型
print (type(listNum)) #输出列表类型
{} 包裹,键值对结构(key: value)。
del 删除键值对。
mobile = {'tom':'19991111','bob':'19982222','alice':'19973333'}
print(mobile)
print(type(mobile))
mobile = {'tom':'19991111','bob':'19982222','alice':'19973333'}
print(mobile["tom"])
print(mobile["bob"])
mobile = {'tom':'19991111','tom':'1×××222','tom':'19993333'}
print(mobile)
mobile = {'tom':'19991111','bob':'19982222'}
(mobile['alice'])='19993333'
print(mobile)
mobile = {'tom':'19991111','bob':'19982222'}
(mobile['bob'])='19993333'
print(mobile)
mobile = {'tom':'19991111','bob':'19982222'}
(mobile['bob'])='19993333'
del (mobile['tom'])
print(mobile)