python之地基(三)
一、引用计数和垃圾回收机制
当一个执行程序完毕后,回收变量所占据的内存。
当引用计数变为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之地基(三)的更多相关文章
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- Python 基础语法(三)
Python 基础语法(三) --------------------------------------------接 Python 基础语法(二)------------------------- ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- Python/MySQL(三、pymysql使用)
Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...
- python学习第三次记录
python学习第三次记录 python中常用的数据类型: 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). int.数 ...
- python中的三种输入方式
python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...
- python 历险记(三)— python 的常用文件操作
目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...
- 3.Python爬虫入门三之Urllib和Urllib2库的基本使用
1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...
- 实操一下<python cookbook>第三版1
这几天没写代码, 练一下代码. 找的书是<python cookbook>第三版的电子书. *这个操作符,运用得好,确实少很多代码,且清晰易懂. p = (4, 5) x, y = p p ...
- python中实现三目运算
python中没有其他语言中的三元表达式,不过有类似的实现方法 如: a = 1 b =2 k = 3 if a>b else 4 上面的代码就是python中实现三目运算的一个小demo, 如 ...
随机推荐
- 关于Eclipse使用Git基础篇
一:Git的下载与安装与基本使用 1.打开eclipse->help->Eclipse Markplace->search->fiind输入Egit 你会看到如下截图(我的为已 ...
- Appium简介
Appium简介 Appium is an open source test automation framework for use with native, hybrid and mobile w ...
- mysql join用法简介
为什么需要join 为什么需要join?join中文意思为连接,连接意味着关联即将一个表和多个表之间关联起来.在处理数据库表的时候,我们经常会发现,需要从多个表中获取信息,将多个表的多个字段数据组装起 ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- ElasticSearch常用操作
查看某个INDEX库某个TYPE表,某个字段的分词结果 GET /${index}/${type}/${id}/_termvectors?fields=${fields_name}http://19 ...
- 将gbk字符串转换成utf-8,存储到注册表中后,再次从注册表读取转换成gbk,有问题!!!
char *a = "新2新"; printf("gbk:'%s'\n", a); int ii; ; ii < strlen(a); ii++) { p ...
- 关于JVM加载class文件和类的初始化
关于JVM加载class文件和类的初始化 1.JVM加载Class文件的原理机制 1.1.装载 查找并加载类的二进制数据 1.2.链接 验证:确保被加载类的正确性.(安全性考虑) 准备:为类的静态变量 ...
- html常用标签的取值和赋值操作
我们在html页面当中,面对各种各样的标签,经常需要处理取值和赋值的问题,下面,就把常见的一些html标签元素的取值和赋值操作进行总结整理,以后备用. 1.button:改变button按钮上面的值, ...
- redis---------AOF文件异常导致的redis无法载入
AOF损坏时的对策1.若在写AOF文件时Server崩溃则可能导致AOF文件损坏而不能被Redis载入.可通过如下步骤修复: 创建一个AOF文件的备份: cp appendonly.aof appen ...
- VMware Workstation 常见问题解决
本文以FAQ的方式进行整理,大家可以根据关键字进行查找即可. 问题一:VMware 安装64位操作系统报错“此主机支持Intel VT-x, 但Intel VT-x处于禁用状态” 问题二:This v ...