python第一周上课学习笔记

Python第一天学习笔记

要点 1 添加作者信息

image

要点 2 交换变量的值


#  这是代码区域

a =1

b =2

a, b = b, a

print('a的值是:',a,'b的值是:',b)

print(type(a))

要点 3 多个变量的赋值


name, age, sex ='雷小坏',18,'m'

print(name,age,sex)

要点 4 数据类型的转换


input ('请输入')

print(type(name))

name =int(name)

print(type(name))

要点 5 判断语句(if,else)


# 判断和循环

# if  else  elif

# if 要判断的条件:

#    条件成立时,要做的事情

score =input('请输入您的分数')

score =int(score)

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('成绩不及格')

要点 6 猜拳游戏


# 猜拳游戏

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== computer):

    print('平局,要不要再来一局')

#输了

else:

    print('你输了,不要走,决战到天亮')

要点 7 循环语句while


# while循环

# while 条件:

#    条件满足时执行的事情

# while  True:

    print('老婆我错了')

    i = 0

   while i < 10:

   print('老婆我错了,这是我第{}次向您道歉'.format(i))

    i += 1

要点 8 循环的嵌套


# *

# **

# ***

# ****

# *****


# 循环的嵌套

 i = 1

 while i <= 5:

    j = 1

    while j <= i:

        print("* ",end='')

        j += 1

    print ('\n')

    i += 1

要点 9 for循环,break和continue语句的使用


# for 循环

# for 临时变量  in 可迭代对象:

# 循环满足时要执行的事情

company= 'neusoft'

for xin company:

    print (x)

    if x=='s':

        print("这个s要大写")

# break 立刻结束break所在的循环

# continue 结束当前循环,紧接着执行下一次循环

# break

company='neusoft'

for xin company:

    print('--------')

    if x== 's':

        break

    print(x)

else:

    print('for 循环没有执行break,则执行本语句')

# continue

company='neusoft'

for xin company:

    print('--------')

    if x== 's':

        continue

    print(x)

else:

    print('for 循环没有执行break,则执行本语句')

要点 10 字符串,切片


# 字符串

word= "hello , 'c'"

print(word)

# 切片:截取一部分的操作

# 对象[起始:终止:步长]  不包括结束位,左闭右开

name= 'abcdef'

print(name[4:2:-1])

print(name[3:5])

print(name[2:])

print(name[1:-1])

print(name[:])

print(name[::2])

print(name[5:1:-2])

# fedcba

print(name[::-1])

要点 11 字符串常规操作


my_str='hello world neuedu and neueducpp'

count= my_str.count('n',0,20)

print(count)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。