python第一课笔记

一 python基础操作

1.用python简单的交换两个变量


a=1

b = 2

a, b =b, a

print(a,b)

这样就直接把ab互换了。

2.格式化输出

hero_name ='dva'

print('您选择的英雄是%s'%hero_name)

print('您选择的英雄是{hero_name}'.format(hero_name=hero_name))

*//代表取整除

3.多变量赋值操作

name, age, sex ='小明',18,'f'

print(name,age,sex)

4.数据类型转换

name = input('请输入')

print(type(name))

二.循环

1.while 条件:

条件满足时执行的事情

例子1:

while True:

     print('老婆我错了')    
     这样会无限循环下去
i=0

while i<10

print('老婆我错了,这是我第%d次向您道歉’%i)

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

例子2:计算1到100之间的偶数累加和

i=1

sum=0

while i<=100:

if i%2==0:

sum+=i

i+=1

print(sum)

循环的嵌套

i = 1

while i <= 5:

   j = 1

   while j <= i :

      print("* ",end='')

        j += 1

   print('\n')

   i += 1
*

* *

* * *

* * * * 

制作一个九九乘法口诀表

i = 1

while i <=9:

    j = 1

    while j <= i :

        print("%d * %d = %d  " %(j ,i ,i*j),end='')

        j += 1

    print('\n')


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 

2.for循环

for 临时变量

company = 'neusoft'

 for x in company:

    print(x)

  if x== 's':

        print("这个s要大写")
n

e

u

这个s要大写

o

f

t

3.range()函数

range(起始值,终值)

 for i in range (1, 101 ):

    print(i)

4.break:立刻结束break 所在的循环

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

company = 'neusoft'

 for x in company:

    print('__________')

    if x =='s':

        break

    print(x)

else:

print('for循环没有执行break')

5.contine的循环

 company = 'neusoft'

 for x in company:

    print('__________')

    if x =='s':

        continue

    print(x)

else:

print('for循环没有执行break')

三,字符串

word = "hello ,'c'"

print(word)

访问字符串 下标

print(word[1])

切片 对目标对象截取一部分的操作

对象【起始: 终止 : 步长】不包括结束位置

name ='abcdef'

print(name[0:3])#abc

print(name[3:5])#de

print(name[2:])#cdef

print(name[1:-1])#bcde

print(name[:])#abcdef

print(name[: :2])#ace

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

print(name[::-1])#fedcba倒叙

字符串常见操作

my_str ='hello world neueud and neudeucpp'

find()

检查str中是否包含在 my_str中,如果在返回开始的索引值,否则返回-1

index1 = my_str.find('neueud')

print(index1)

index2 = my_str.find('neueud',0,10)

print(index2)

index()跟find()一样 只不过str 不在mystr中要报一个异常

count返回 目标字符串出现的次数

count = my_str.count('neuedu',0,10)

print(count)

四,小游戏:跟电脑石头剪刀布

import random

player = input('请输入:剪刀(0),石头(1),布(2):')

player =int(player)

computer = random.randint(0,2)#生成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('你输了')
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,736评论 0 38
  • 1.1 列表的定义 List(列表) 是Python中使用最频繁的数据类型,在其他语言中通常叫做数组 专门用于存储...
    体面_5376阅读 4,355评论 0 0
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 9,947评论 0 5
  • 夏天很热,所有人都巴不得凉快下来,我也不例外。不过要是听了我的介绍,说不定你也会去泡温泉的! 一天上午...
    amy1585阅读 1,189评论 0 0
  • 静听叶落阅读 1,404评论 1 2