第一天总结

变量

在python中交换两个变量的值

a=1
b=2
print(a,b)
a,b=b,a
print(a,b)
1 2
2 1

标识符

python标识符由字母/下划线和数字组成,且数字不能开头,区分大小写
一般用下划线-

ero_name="鲁班七号"
level=15
print('您选择的英雄是%s\n当前等级为%d'%(hero_name,level))
您选择的英雄是鲁班七号
当前等级为15

/n表示换行

多行赋值操作

name,age,sex='杜',20,"D"
print(name,age,sex)
杜 20 D

数据类型转换

name=input('请输入')
print(type(name))
name=int(name)
print(type(name))
str1='1+19'
res=eval(str1)
print(res)
请输入1
<class 'str'>
<class 'int'>
20

判断和循环

  • if和else
    if 要判断的条件:
    条件成立时执行的语言
age=input('请输入你的年龄')
age=int(age)
if age>=18:
    print('你可以去网吧了')
    if age==20:
        print('网吧冲会员减半')
else:
    print('不能去网吧,去借张身份证上网吧')
请输入你的年龄>? 20
你可以去网吧了
网吧冲会员减半
  • elif
    if xxx1:
    xxxx1
    elif xxx2:
    xxxx2
    elif xxx3:
    xxxx3
    else: xxxx4
score=77
if score>=90 and score<=100:
    print('考试等级是A')
elif score>=80 and score<=90:
    print('考试等级是B')
elif score>=70 and score<=80:
    print('考试等级是C')
elif score>=60 and score<=70:
    print('考试等级是D')
else:
    print('考试不及格')
考试等级是C
score=77
score ='你的分数是77' if score==77 else score
print(score)
你的分数是77

猜拳游戏

import random
player=input('请输入:剪刀(0),石头(1),布(2)')
player=int(player)
#生成[0,2]随机整数
computer=random.randint(0,2)
#获胜的情况
if ((player==0 and computer ==2) or (player==1 and computer==0) or(player==2 and computer ==1)):
    print('恭喜您获胜了')

#平局的情况
elif ((player==0 and computer ==0) or (player==1 and computer==1) or(player==2 and computer ==2)):
    print('平局,要不要再来一句')

#输的情况
else :
    print('你输了')

循环while

while 条件:
条件满足时执行的事情

i=0
while i< 10:
    print('这是我第{}次向您道歉'.format(i))
i+=1
这是我第0次向您道歉
这是我第1次向您道歉
这是我第2次向您道歉
这是我第3次向您道歉
这是我第4次向您道歉
这是我第5次向您道歉
这是我第6次向您道歉
这是我第7次向您道歉
这是我第8次向您道歉
这是我第9次向您道歉
  • 计算1到100之间偶数的累加和(包含1和100)
i=1
sum=0
while i<=100:
    if i %2==0:
        sum+=i
    i+=1
print(sum)
2550
  • 打印九九乘法表
i=1
while i<=9:
    j=1
    while j<=i:
        print('%d*%d=%d  '%(j,i,j*i),end='')
        j+=1
    print('\n')
    i+=1
1*1=1  

1*2=2  2*2=4  

1*3=3  2*3=6  3*3=9  

1*4=4  2*4=8  3*4=12  4*4=16  

1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  

1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  

1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  

1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  

1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81 

for 循环

for 临时变量 in 可迭代的对象:
循环满足时的事情

company='neusoft'
for i in company:
    print(i)
    if i =='s':
        print('阿斯达')
n
e
u
s
阿斯达
o
f
t
  • range(起始值,终止,步长)
for i in range (1,101,2):
    print(i)
  • break 跳出当前循环
  • continue跳出当前循环,继续下一次循环
company='neusoft'
for i in company:
    print('++++++')
    if i =='s':
        continue
    print(i)
else:
    print('没有执行break')
++++++
n
++++++
e
++++++
u
++++++
++++++
o
++++++
f
++++++
t
没有执行break

字符串

 word='hello "world"'
 print(word)
hello "world"
  • 访问字符串
print(word[1])
e
  • 切片
name='qiepian'
print(name*3)
qiepianqiepianqiepian
  • 截取一部分的操作
  • 对象[起始: 终止 : 步长] 不包括结束位置
name='abcdef'
print(name[0:3])
print(name[0:-1])
print(name[::2])
print(name[5:1:-2])
print(name[::-1])
abc
abcde
ace
fd
fedcba

字符串常见操作

my_str='hello world neuedu and neueducpp'
  • find()
    检查str中是否包含my_str中,如果在返回开始的索引值,否则返回-1
index=my_str.find('neuedu',0,10)
print(index)
-1
  • index()跟find()一样,只不过str不在mystr中要报一个异常
index=my_str.index('neuedu')
print(index)
12
  • count返回目标字符串出现的次数
index=my_str.count('neuedu',0,18)
print(index)
1
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容