首页 > 编程语言 > 详细

python之地基(三)

时间:2019-04-24 20:16:20      阅读:175      评论:0      收藏:0      [点我收藏+]

一、引用计数和垃圾回收机制

  当一个执行程序完毕后,回收变量所占据的内存。

  当引用计数变为0的时候,回收变量所占据的内存。

a=100
print(id(a))
a=input(‘==>:‘)    #当a被覆盖时a=100所占用的内存被回收
print(id(a))

输出

140722188971952
==>:1
2403418781264

二、可变类型和不可变类型

  可变类型:

    在id不变的情况下,value值改变,则称之为可变类型。

  不可变类型:

    value值发生改变,id改变,则称之为不可变类型。

三、格式化输出

     程序中经常出现这样的场景,要求用户去输入信息,然后但因成固定的格式。

  比如要求用户输入用户名和密码然后打印如下格式

My user is  xxx ,my user_passwd is xxx

  这种情况下就用到了%s和%d

res = ‘my user is  %s  ,my user_passwd is %d‘ %(‘cong,12345)
print(res)

  这里要注意%d只可以接收数字,%s可以接收数字也可以接受字符串。

  第一种方法,传递参数

res = ‘my user is {user}, my user_passwd is {user_passwd}‘.format(user=‘cong‘,user_passwd=12345)
print(res)

  第二种方法按顺序传送

res = ‘my user is {0}, my user_passwd is {1}‘.format(‘cong‘,12345)
print(res)

四、流程控制之if...else...

  在if循环里,有几个需要用到的逻辑运算符

  and  代表 “和”

  or  代表 “或”

  not  代表 “非”

name = ‘聪聪‘
res = input(‘你叫什么名字?‘)
if res == name:
    print(‘帅哥‘)
else:
    print(‘丑男‘)
#如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨
age_of_girl=18
height=171
weight=99
is_beautiful=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_beautiful == True:
    print(‘表白...‘)
else:
    print(‘阿姨好‘)

五、流程控制之while

  在while循环里,有两个结束循环的词

    break  结束循环

    ontinue  跳过本次循环

while 条件:    
    # 循环体
 
    # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
    # 如果条件为假,那么循环体不执行,循环终止
技术分享图片
import time
num = 0
while True:
    print(num)
    num += 1
    time.sleep(1)
打印0-10之间的偶数
count = 0
while count <= 5 :
    count += 1
    print("Loop",count)
else:
    print("循环正常执行完啦")
print("-----out of while loop ------")
输出
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------
技术分享图片
for letter in Python:     # continue第一个实例
   if letter == h:
      continue
   print(当前字母 :, letter)

var = 10                    # continue第二个实例
while var > 0:
   var = var -1
   if var == 5:
      continue
   print(当前变量值 :, var)
print("Good bye!")



count = 0        #break实例
while True:
    print(count)
    count+=1
    if count == 5:
        break
break 和 continue 

六、流程控制之for

 1.迭代式循环:for,语法如下

  for i in res():

    缩进的代码块

2.break与continue(同上)

七、练习

  做一个猜拳的游戏!!!

  提示 import random  随机模块

技术分享图片
import random
WIN=0
lose=0
draw=0
while True:
    print(==========欢迎来猜拳==========)
    print(WIN:%s lose:%s draw:%s % (WIN,lose,draw))
    print(1.石头,2.剪刀,3.布,4.退出)
    hum_choose=input(==>:)
    computer_choose=random.choice([石头,剪刀,])
    #胜利
    if hum_choose== 1 and computer_choose==剪刀 or hum_choose==2and computer_choose== or hum_choose==3 and computer_choose==石头:
      print(you WIN!!!)
      WIN+=1
    #失败
    elif hum_choose==3 and computer_choose==剪刀 or hum_choose==1 and computer_choose== or hum_choose==2 and computer_choose==石头:
      print(you lose)
      lose+=1
    #平局
    elif hum_choose==2 and computer_choose==剪刀or hum_choose==3 and computer_choose== or hum_choose==1 and computer_choose==石头:
      print(you draw)
      draw+=1
    #退出游戏
    elif hum_choose==4:
        print(gameover)
        break
    else:
        print(input error)
猜拳游戏答案

   做一个名片管理系统

  不要看答案!自己做!

技术分享图片
l1 =[]
info ={}
while True:
    print(= * 60)
    print(名片管理系统)
    print(1.查询  2.添加  3.修改  4.删除  5.定点查询  6.退出  )
    choose = input(请输入选项:).strip()
    # 查询
    if choose == 1:
        if l1:
            i = 1
            while i < len(l1)+1:
                print(%s.姓名:%s 年龄:%s 电话:%s % (i,l1[i-1][name],l1[i-1][age],l1[i-1][phone]))
                i += 1
        else:
            print(当前名片为空)
    # 添加
    elif choose == 2:
        name = input(姓名:).strip()
        age = input(年龄:).strip()
        phone = input(电话:).strip()
        if name and age and phone:
            info = {
                name:name,
                age:age,
                phone:phone
            }
            l1.append(info)
        else:
            print(请输入相应的信息)
    # 修改
    elif choose == 3:
        j = 1
        while j < len(l1)+1:
            print(%s.姓名:%s 年龄:%s 电话:%s % (j,l1[j-1][name],l1[j-1][age],l1[j-1][phone]))
            j += 1
        update = input(请输入需要修改的名片:).strip()
        if update:
            if int(update) < len(l1)+1:
                u_name = input(修改的姓名:).strip()
                u_age = input(修改的年龄:).strip()
                u_phone = input(修改的电话:).strip()
                if u_name:
                    l1[int(update)-1][name] = u_name
                if u_age:
                    l1[int(update)-1][age] = u_age
                if u_phone:
                    l1[int(update)-1][phone] = u_phone
                print(姓名:%s 年龄:%s 电话:%s %(l1[int(update)-1][name],l1[int(update)-1][age],l1[int(update)-1][phone]))
            else:
                print(没有该名片)
        else:
            print(警告:没有输入需要修改的名片!)
    # 删除
    elif choose == 4:
        delete = input(请输入需要删除的名片:).strip()
        if delete:
            if int(delete) < len(l1)+1:
                l1.remove(l1[int(delete)-1])
            else:
                print(没有该名片)
        else:
            print(警告:没有输入需要删除的名片!)
    # 定点查询
    elif choose == 5:
        q = input(请输入指定的名片:).strip()
        if q:
            if int(q) < len(l1)+1:
                print(姓名:%s 年龄:%s 电话:%s % (l1[int(q)-1][name],l1[int(q)-1][age],l1[int(q)-1][phone]))
            else:
                print(没有该名片)
        else:
            print(警告:没有输入指定的名片!)
    # 退出
    elif choose == 6:
        print(欢迎下次使用)
        break
    else:
        print(输入错误)
名片管理系统答案

 

  

python之地基(三)

原文:https://www.cnblogs.com/Amrice/p/10738528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!