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

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

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

  1. a=100
  2. print(id(a))
  3. a=input('==>:')    #当a被覆盖时a=100所占用的内存被回收
  4. print(id(a))
  5.  
  6. 输出

140722188971952
==>:1
2403418781264

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

  可变类型:

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

  不可变类型:

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

三、格式化输出

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

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

  1. My user is xxx ,my user_passwd is xxx

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

  1. res = 'my user is %s ,my user_passwd is %d' %('cong,12345)
  2. print(res)

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

  第一种方法,传递参数

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

  第二种方法按顺序传送

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

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

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

  and  代表 “和”

  or  代表 “或”

  not  代表 “非”

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

五、流程控制之while

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

    break  结束循环

    ontinue  跳过本次循环

  1. while 条件:
  2. # 循环体
  3.  
  4. # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
  5. # 如果条件为假,那么循环体不执行,循环终止
  1. import time
  2. num = 0
  3. while True:
  4. print(num)
  5. num += 1
  6. time.sleep(1)

打印0-10之间的偶数

  1. count = 0
  2. while count <= 5 :
  3. count += 1
  4. print("Loop",count)
  5. else:
  6. print("循环正常执行完啦")
  7. print("-----out of while loop ------")
  8. 输出
  9. Loop 1
  10. Loop 2
  11. Loop 3
  12. Loop 4
  13. Loop 5
  14. Loop 6
  15. 循环正常执行完啦
  16. -----out of while loop ------
  1. for letter in 'Python': # continue第一个实例
  2. if letter == 'h':
  3. continue
  4. print('当前字母 :', letter)
  5.  
  6. var = 10 # continue第二个实例
  7. while var > 0:
  8. var = var -1
  9. if var == 5:
  10. continue
  11. print('当前变量值 :', var)
  12. print("Good bye!")
  13.  
  14. count = 0 #break实例
  15. while True:
  16. print(count)
  17. count+=1
  18. if count == 5:
  19. break

break 和 continue

六、流程控制之for

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

  for i in res():

    缩进的代码块

2.break与continue(同上)

七、练习

  做一个猜拳的游戏!!!

  提示 import random  随机模块

  1. import random
  2. WIN=0
  3. lose=0
  4. draw=0
  5. while True:
  6. print('==========欢迎来猜拳==========')
  7. print('WIN:%s lose:%s draw:%s' % (WIN,lose,draw))
  8. print('1.石头','2.剪刀','3.布','4.退出')
  9. hum_choose=input('==>:')
  10. computer_choose=random.choice(['石头','剪刀','布'])
  11. #胜利
  12. if hum_choose== '' and computer_choose=='剪刀' or hum_choose==''and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
  13.    print('you WIN!!!')
  14.    WIN+=1
  15. #失败
  16. elif hum_choose=='' and computer_choose=='剪刀' or hum_choose=='' and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
  17.    print('you lose')
  18.    lose+=1
  19. #平局
  20. elif hum_choose=='' and computer_choose=='剪刀'or hum_choose=='' and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
  21.   print('you draw')
  22.    draw+=1
  23. #退出游戏
  24. elif hum_choose=='':
  25. print('gameover')
  26. break
  27. else:
  28. print('input error')

猜拳游戏答案

  做一个名片管理系统

  不要看答案!自己做!

  1. l1 =[]
  2. info ={}
  3. while True:
  4. print('=' * 60)
  5. print('名片管理系统')
  6. print('1.查询 2.添加 3.修改 4.删除 5.定点查询 6.退出 ')
  7. choose = input('请输入选项:').strip()
  8. # 查询
  9. if choose == '':
  10. if l1:
  11. i = 1
  12. while i < len(l1)+1:
  13. print('%s.姓名:%s 年龄:%s 电话:%s' % (i,l1[i-1]['name'],l1[i-1]['age'],l1[i-1]['phone']))
  14. i += 1
  15. else:
  16. print('当前名片为空')
  17. # 添加
  18. elif choose == '':
  19. name = input('姓名:').strip()
  20. age = input('年龄:').strip()
  21. phone = input('电话:').strip()
  22. if name and age and phone:
  23. info = {
  24. 'name':name,
  25. 'age':age,
  26. 'phone':phone
  27. }
  28. l1.append(info)
  29. else:
  30. print('请输入相应的信息')
  31. # 修改
  32. elif choose == '':
  33. j = 1
  34. while j < len(l1)+1:
  35. print('%s.姓名:%s 年龄:%s 电话:%s' % (j,l1[j-1]['name'],l1[j-1]['age'],l1[j-1]['phone']))
  36. j += 1
  37. update = input('请输入需要修改的名片:').strip()
  38. if update:
  39. if int(update) < len(l1)+1:
  40. u_name = input('修改的姓名:').strip()
  41. u_age = input('修改的年龄:').strip()
  42. u_phone = input('修改的电话:').strip()
  43. if u_name:
  44. l1[int(update)-1]['name'] = u_name
  45. if u_age:
  46. l1[int(update)-1]['age'] = u_age
  47. if u_phone:
  48. l1[int(update)-1]['phone'] = u_phone
  49. print('姓名:%s 年龄:%s 电话:%s' %(l1[int(update)-1]['name'],l1[int(update)-1]['age'],l1[int(update)-1]['phone']))
  50. else:
  51. print('没有该名片')
  52. else:
  53. print('警告:没有输入需要修改的名片!')
  54. # 删除
  55. elif choose == '':
  56. delete = input('请输入需要删除的名片:').strip()
  57. if delete:
  58. if int(delete) < len(l1)+1:
  59. l1.remove(l1[int(delete)-1])
  60. else:
  61. print('没有该名片')
  62. else:
  63. print('警告:没有输入需要删除的名片!')
  64. # 定点查询
  65. elif choose == '':
  66. q = input('请输入指定的名片:').strip()
  67. if q:
  68. if int(q) < len(l1)+1:
  69. print('姓名:%s 年龄:%s 电话:%s' % (l1[int(q)-1]['name'],l1[int(q)-1]['age'],l1[int(q)-1]['phone']))
  70. else:
  71. print('没有该名片')
  72. else:
  73. print('警告:没有输入指定的名片!')
  74. # 退出
  75. elif choose == '':
  76. print('欢迎下次使用')
  77. break
  78. else:
  79. print('输入错误')

名片管理系统答案

  

python之地基(三)的更多相关文章

  1. 进击的Python【第三章】:Python基础(三)

    Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...

  2. Python 基础语法(三)

    Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...

  3. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  4. Python/MySQL(三、pymysql使用)

    Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...

  5. python学习第三次记录

    python学习第三次记录 python中常用的数据类型: 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). int.数 ...

  6. python中的三种输入方式

    python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...

  7. python 历险记(三)— python 的常用文件操作

    目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...

  8. 3.Python爬虫入门三之Urllib和Urllib2库的基本使用

    1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...

  9. 实操一下<python cookbook>第三版1

    这几天没写代码, 练一下代码. 找的书是<python cookbook>第三版的电子书. *这个操作符,运用得好,确实少很多代码,且清晰易懂. p = (4, 5) x, y = p p ...

  10. python中实现三目运算

    python中没有其他语言中的三元表达式,不过有类似的实现方法 如: a = 1 b =2 k = 3 if a>b else 4 上面的代码就是python中实现三目运算的一个小demo, 如 ...

随机推荐

  1. 关于Eclipse使用Git基础篇

    一:Git的下载与安装与基本使用 1.打开eclipse->help->Eclipse Markplace->search->fiind输入Egit 你会看到如下截图(我的为已 ...

  2. Appium简介

    Appium简介 Appium is an open source test automation framework for use with native, hybrid and mobile w ...

  3. mysql join用法简介

    为什么需要join 为什么需要join?join中文意思为连接,连接意味着关联即将一个表和多个表之间关联起来.在处理数据库表的时候,我们经常会发现,需要从多个表中获取信息,将多个表的多个字段数据组装起 ...

  4. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  5. ElasticSearch常用操作

    查看某个INDEX库某个TYPE表,某个字段的分词结果  GET /${index}/${type}/${id}/_termvectors?fields=${fields_name}http://19 ...

  6. 将gbk字符串转换成utf-8,存储到注册表中后,再次从注册表读取转换成gbk,有问题!!!

    char *a = "新2新"; printf("gbk:'%s'\n", a); int ii; ; ii < strlen(a); ii++) { p ...

  7. 关于JVM加载class文件和类的初始化

    关于JVM加载class文件和类的初始化 1.JVM加载Class文件的原理机制 1.1.装载 查找并加载类的二进制数据 1.2.链接 验证:确保被加载类的正确性.(安全性考虑) 准备:为类的静态变量 ...

  8. html常用标签的取值和赋值操作

    我们在html页面当中,面对各种各样的标签,经常需要处理取值和赋值的问题,下面,就把常见的一些html标签元素的取值和赋值操作进行总结整理,以后备用. 1.button:改变button按钮上面的值, ...

  9. redis---------AOF文件异常导致的redis无法载入

    AOF损坏时的对策1.若在写AOF文件时Server崩溃则可能导致AOF文件损坏而不能被Redis载入.可通过如下步骤修复: 创建一个AOF文件的备份: cp appendonly.aof appen ...

  10. VMware Workstation 常见问题解决

    本文以FAQ的方式进行整理,大家可以根据关键字进行查找即可. 问题一:VMware 安装64位操作系统报错“此主机支持Intel VT-x, 但Intel VT-x处于禁用状态” 问题二:This v ...