Pythonの基礎サン玄
変数の型、および不変タイプ
1. 変数タイプ:
#で識別場合不変、値がそのようなリスト、辞書のように、それは変数の型と呼ばれ、変更することができます
L1 = [1,2,3,4]
T1 =(1,2,3,4)
プリント(ID(L1))
L1 [1] = 33333333333333333333333
プリント(ID(L1))
情報= { '名前':' 张小三 ' }
プリント(ID(情報))
インフォ[ '名前' ] = ' 李四 '
プリント(ID(情報))
""
36500304
36500304
6410768
6410768
""
2. 不変のタイプ:
#VALUE 一旦変更、上記IDは述べ、それは不変のタイプと呼ばれ、変更された(上記のID 変化は、それが新たなメモリ空間を作成する手段)
以下のような:整数、浮動小数点、文字列
#A = 10
#プリント(ID(A))
#A = 100
#プリント(ID(A))
そのID の前と後の異なります
[ 拡張]:
タプル、セット、
=(1,2,3,4)
プリント(ID(A))
、B =(1,2,3,4)
プリント(ID(B))
S1 = {1,2,3,4}
プリント(ID(S1))
S2 = {1,2,3,4}
プリント(ID(S2))
""
7709728
#7709872 の値を持つ変数 IDが異なっています
6223672
6223552
""
しかし、辞書の付加価値は、idが変更される可能性があり:
S1 = {1,2,3,4}
プリント(ID(S1))
s1.add('A' )
プリント(S1)
、印刷(ID(S1))
""
6486056
{1、2、3、4、 'A'}
6486056
""
II:組み込み関数( LEN 、で、ではありません)
1. LEN 統計の長さ:
# a = {'name':'程泽宇', 'age':23}
# print(len(a))
2.In及not in
in :在....里面,有则true,无则false
# print('e' in 'hello')
not in :不在.....里面,没有则true,有则false
# print('e' not in 'hello')
三、基本运算符
四、比较运算符
五、赋值运算符
六、身份运算符
【is和==的区别】
#is是判断内存地址
#==是判断变量值
#一个内存地址可以对应多个变量,一个变量只能对应一个内存地址
a =10
b =10.0
print(a==b)
print(a is b)
print(id(a))
print(id(b))
》》
True
False
505359648
6504192
》》
七、流程控制if...else....
1.语法:
if 条件语句
执行的代码块
else:
执行的代码块
2.额外:
a = {}
# 当a为None,0,空 三种情况时为假,其余都为真
if a:
print('真')
else:
print('假')
》》
假
》》
八、逻辑运算符
练习:
#1.定义布尔型变量 has_ticket 表示是否有车票
#2.定义整形变量 knife_length 表示刀的长度,单位:厘米
#3.首先检查是否有车票,如果有,才允许进行安检
#4.安检时,需要检查刀的长度,判断是否超过20厘米
# 如果超过20厘米,提示刀子的长度,不允许上车
# 如果不超过20厘米,按键通过
#5.如果没有车票,不允许进门
ticket = input('请问您是否有车票:(1:有,2:无):')
if ticket=='1':
nife = input('您的随身刀具长度为:')
if nife <='20':
print('请上车,一路平安')
else:
print('刀具过长,请处理之后再上车')
else:
print('请购买车票再来')
九、流程控制之while
while 条件:
# 循环体
# 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
# 如果条件为假,那么循环体不执行,循环终止
1. 打印0到4的数字
i = 0
while i < 5:
print(i)
i+=1
2.打印0到10之间的偶数
方案一:
i =0
while i <11:
print(i)
i+=2
方案二:
i =0
while i < 11:
if i % 2 == 0:
print(i)
i+=1
3.死循环
除了0,None,空这几个假量,其他的几乎都可以作死循环条件
如:
while 1:
print('循环到底')
4.while....else...
count = 0
while count <= 3 :
count += 1
print("hehe",count)
else:
print("循环已执行完")
》》
hehe 1
hehe 2
hehe 3
hehe 4
循环已执行完
》》
5.pass,continue,break使用
【continue跳过本次循环进入下一次循环】:
count = 0
while count <= 4 :
count += 1
if count==3:
continue
print("hehe",count)
》》
hehe 1
hehe 2
hehe 4
hehe 5
》》
【breck跳出整个循环体】
count = 0
while count <= 4 :
count += 1
if count==3:
break
print("hehe",count)
》》
hehe 1
hehe 2
》》
【pass是仅用来占位的,一般作补全语境用】
count = 0
while count <= 3 :
count += 1
if count==3:
pass #触发条件不作任何处理
print("hehe",count)
》》
hehe 1
hehe 2
hehe 3
hehe 4
》》
6.猜拳游戏
import random #‘import’:调用模块;‘random’:随机数
win =0
fault=0
balance=0
while True:
com = random.choice(['1', '2', '3']) # 1:石头,2:剪刀,3:布
print('——————欢迎来到猜拳大赛——————')
sum = input('请输入 ——1:石头,2:剪刀,3:布, 4:结束游戏 :')
print(com)
#赢
if (sum=='1'and com=='2') or (sum=='2'and com=='3') or (sum=='3' and com=='1'):
print('流弊,你赢了!')
win+=1
#败
elif (sum=='1'and com=='3') or (sum=='2'and com=='1') or (sum=='3'and com=='2'):
print('犹豫,就会败北!')
fault+=1
#平
elif (sum==com):
print('你瞅啥,平局!')
balance+=1
elif sum=='4':
print('胜:%s 败:%s 平:%s' %(win,fault,balance))
break
else:
print('请输入正确数字')
————————————————————————————分割线——————————————————————————