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

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

  当引用计数变为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== '' and computer_choose=='剪刀' or hum_choose==''and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
   print('you WIN!!!')
   WIN+=1
#失败
elif hum_choose=='' and computer_choose=='剪刀' or hum_choose=='' and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
   print('you lose')
   lose+=1
#平局
elif hum_choose=='' and computer_choose=='剪刀'or hum_choose=='' and computer_choose=='布' or hum_choose=='' and computer_choose=='石头':
  print('you draw')
   draw+=1
#退出游戏
elif hum_choose=='':
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 == '':
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 == '':
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 == '':
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 == '':
delete = input('请输入需要删除的名片:').strip()
if delete:
if int(delete) < len(l1)+1:
l1.remove(l1[int(delete)-1])
else:
print('没有该名片')
else:
print('警告:没有输入需要删除的名片!')
# 定点查询
elif choose == '':
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 == '':
print('欢迎下次使用')
break
else:
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. jquery ajax几种书写方式的总结

    Ajax在前端的应用极其广泛,因此,我们有必要对其进行总结,以方便后期的使用. AJAX优点: 可以异步请求服务器的数据,实现页面数据的实时动态加载, 在不重新加载整个页面的情况下,可以与服务器交换数 ...

  2. Luogu4363 [九省联考2018]一双木棋chess 【状压DP】【进制转换】

    题目分析: 首先跑个暴力,求一下有多少种状态,发现只有18xxxx种,然后每个状态有10的转移,所以复杂度大约是200w,然后利用进制转换的技巧求一下每个状态的十进制码就行了. 代码: #includ ...

  3. 牛客网 272B Xor Path(树上操作)

    题目链接:Xor Path 题意:每个顶点的点权为Ai,任意两点路径上点权异或和为Path(i,j),求所有Path(i,j)和. 题解:考虑每个顶点被用到的次数,分以下三种情况: 1.本身和其他顶点 ...

  4. 【学习笔记】python

    1.  len( s )  返回对象(字符.列表.元祖等)的长度或项目个数. >>>str = "runoob" >>> len(str) # ...

  5. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  6. Laravel底层实现原理系列

    Laravel 从学徒到工匠精校版 地址:https://laravelacademy.org/laravel-from-appreciate-to-artisan

  7. Java基础--面向对象编程2(封装)

    1.封装的定义: 封装:将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问. 2.  为什么需要封装?封装的作用和含义? 首先思考一个问题:当我们要 ...

  8. SpringCloud笔记七:Zuul

    目录 什么是Zull 为什么需要Zuul 新建Zuul项目 运行Zuul Zuul的基本配置 忽略微服务的真实名称 设置统一公共前缀 总结 什么是Zull Zuul就是一个网关,实现的功能:代理.路由 ...

  9. hadoop记录-hadoop常用

    1.hdfs目录配额 #设置配额目录hdfs dfsadmin -setSpaceQuota 10T /user/hive/warehouser/tmp查看配额目录信息hdfs dfs -count ...

  10. 网站分析平台:是选择百度统计,还是 Google Analytics 呢?

    当你拥有个人博客或个人网站时,你一定需要一个平台来分析你的网站状况.之前我在法国只是使用 Google Analytics,后来回国发现这个平台在国内受限制了,于是我找到了百度统计,目前我同时使用这两 ...