Python(2)——基础知识爬坑
来源:互联网 发布:淘宝的目标消费群 编辑:程序博客网 时间:2024/06/04 19:32
/============================
以下所有内容,均来自 廖雪峰 的官方网站,
Python 教程。
链接地址:http://www.liaoxuefeng.com
============================/
廖雪峰 python 教程中的 python 基础,对于其中的几个重点知识和坑点来沉淀一下:
1、输入 input()、输出 print()
print():
>>> print('100+200=',100+200)100+200= 300
input():
#python输入、输出firstName = input('你姓什么:')lastName = input('你叫什么:')age = input('几岁了:')print(type(age))print('hello',firstName,lastName,age)
输出结果:
C:\Users\allen\pythonwork>python3 print_input.py你姓什么:zhou你叫什么:allen几岁了:18<class 'str'>hello zhou allen 18
*重点关注: age 输出的类型是 str (字符串类型),而不是 int 或者 float 类型,所以当我们需要对输入的内容进行 calculate 计算时,就需要先对输入的内容进行数据转型。
这边提供一个综合的代码,可以不断调整测试一下:
weight = input('请输入您的体重(kg):')height = input('请输入您的身高(m):')height = float(height)weight = float(weight)print(type(height))print(type(weight))BMI = weight/(height*height)print(type(BMI))if BMI < 18.5: print('过轻')elif BMI >= 18.5 and BMI < 25: print('正常')elif BMI >= 25 and BMI < 28: print('过重')elif BMI >= 28 and BMI < 32: print('肥胖')elif BMI >=32: print('严重肥胖')
输出结果:
C:\Users\allen\pythonwork>python3 if.py请输入您的体重(kg):68请输入您的身高(m):1.80<class 'float'><class 'float'><class 'float'>正常
测试:可以将数据转型注释(#),来看会是什么样的结果。
2、数据转型
这边讲一下三种类型:str(字符串) 、int(整型) 、 float(浮点数型)。
int:
>>> int(123)123>>> int(123.00)123>>> int(123.5)123>>> int('123v')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: invalid literal for int() with base 10: '123v'>>> int(-123)-123>>> int(-123.0)-123>>> int(-123.5)-123>>>
分析:int( )只是简单的将数字类型转成整型,不具备四舍五入,不支持将字符串转为整型。
str:
>>> str(123)'123'>>> str(123.0)'123.0'>>> str('123v')'123v'>>> str(-123.0)'-123.0'>>> str(123.a) File "<stdin>", line 1 str(123.a) ^SyntaxError: invalid syntax>>> str('123.a')'123.a'
分析:str( )就比较简单了,只要()中的内容本身没有问题都可以转换成字符串型。
float:
>>> float(123)123.0>>> float(123.0)123.0>>> float('123.0')123.0>>> float(123v) File "<stdin>", line 1 float(123v) ^SyntaxError: invalid syntax>>> float('123v')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: could not convert string to float: '123v'>>>
分析:float( )转成浮点数类型,不管是字符串型,还是数字型,只要内容有且只有数字就可以转。
3、数组 list 和 元祖 tuple
list ‘[ ]’: append( )、pop( )、insert( )
数组例如:
phoneList = ['iphone','huawei','xiaomi']print(phoneList)print(phoneList[0])print(phoneList[1])print(phoneList[2],'\n')print('数组长度:',len(phoneList),'\n')#替换小米phoneList[2] = 'meizu'print('替换后的结果:\n',phoneList,'\n')#重新增加小米phoneList.append('xiaomi')print('添加后的结果:\n',phoneList,'\n')#又要删除小米了phoneList.pop(3)print('删除后的结果:\n',phoneList,'\n')#在指定位置添加小米phoneList.insert(0,'xiaomi')print('爱小米没理由的结果:\n',phoneList)
输入结果:
C:\Users\allen\pythonwork>python3 list.py['iphone', 'huawei', 'xiaomi']iphonehuaweixiaomi数组长度: 3替换后的结果: ['iphone', 'huawei', 'meizu']添加后的结果: ['iphone', 'huawei', 'meizu', 'xiaomi']删除后的结果: ['iphone', 'huawei', 'meizu']添加第一个位置后的结果: ['xiaomi', 'iphone', 'huawei', 'meizu']
分析:list 可变,可查。
tuple ‘( )’: t = (‘a’, ‘b’, [‘A’, ‘B’]):元祖只能查不能变的,但是 t 元祖存在一个 list。
t[2][0] = 'a' print(t)
输出结果:
('a','b',['a','B'])
4、循环
python 循环有两种:
一种是:for ... in ... 另一种是: while 只要条件成立,就不断循环。
#1-10求和sum = 0for x in [1,2,3,4,5,6,7,8,9,10]: sum = sum + xprint(sum)
#100以内所有奇数的求和sum = 0n = 99while n > 0: sum = sum + n n = n - 2print(sum)
5、字典查询 dict 和 set
dict: 类似 map,是一种哈希算法(hash),以键-值(key-value)存储,具有极快的查找速度。
形如:personMessage = {'allen':18,'curry':28,'sixuncle':35}
#以键值对形式存储personMessage = {'allen':18,'curry':28,'sixuncle':35}#查找对应的年龄print(personMessage['allen'])print(personMessage['curry'])print(personMessage['sixuncle'])#可添加personMessage['mama'] = 48print(personMessage)#不存在会报错 #personMessage['papa']#可用in测试返回False,命令行中不显示内容print('papa' in personMessage)#get可以指定返回内容print(personMessage.get('papa',-1))
输出结果:
C:\Users\allen\pythonwork>python3 dict_set.py182835{'sixuncle': 35, 'allen': 18, 'curry': 28, 'mama': 48}False-1
*dict 内部不存在排序
set:
set 和 dict 类似,也是一组 key 的集合,但不存储 value 。由于 key 不能重复,所以,在 set 中,没有重复的 key,可去重啊!
#sets = set({1,2,3,4,5,5,5,55})print(s)#添加s.add(6)print(s)#去除s.remove(55)print(s)#做交集s1 = set({1,2,3,7})print(s & s1)
输出结果:
{1, 2, 3, 4, 5, 55}{1, 2, 3, 4, 5, 6, 55}{1, 2, 3, 4, 5, 6}{1, 2, 3}
最后这边还有一个坑,list 内部是可以改变的, 但是 str 是不会变得!
>>> a = 'abc'>>> b = a.replace('a', 'A')>>> b'Abc'>>> a'abc'
>>> a = 'abc'>>> b = a>>> b'abc'>>> a = 'abC'>>> a'abC'>>> b'abc'
显然 ‘abc’ 始终是 ‘abc’!
python安装安装包:python setup.py install
希望对那么可爱,还来看我博客的你 有些许的帮助!
感谢 廖雪峰 的教程!
- Python(2)——基础知识爬坑
- Python——基础知识
- python——基础知识
- python基础知识(2)
- Python基础知识(2)——格式化字符串
- python基础知识——函数
- python基础知识——字符串
- python 基础知识—— easy_install
- python基础知识——编码
- python基础知识—— Python概述
- Python--python基础知识(2)
- 【python】python基础知识2
- Python学习笔记(一)————基础知识
- Python基础知识2——join 和 split 的使用方法
- python基础知识(1)——列表和元组
- Python学习笔记(1)——基础知识
- Python学习笔记(一)——基础知识
- Python基础知识(3)——中英文文档词频统计
- Wicket搭建项目 Maven配置
- 深入学习Oracle分区表及分区索引
- 使用Opencv实现IOS平台下的人脸识别
- TCP Nagle算法详解
- 2016北大叉院、北大信科保研经历
- Python(2)——基础知识爬坑
- 综合各类透明背景层
- View动画执行过程和插值器作用
- 全排列与去重全排列java
- Java中上转型对象的详细解释
- Git远程操作详解
- 学习PHP的一些浅谈
- 无法安装64位office,因为已有32位版本的完美解决方法
- Redis ACL之redis_set类分析介绍