Python(2)——基础知识爬坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DOCALLEN/article/details/70858895

/============================
以下所有内容,均来自 廖雪峰 的官方网站,
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']
iphone
huawei
xiaomi

数组长度: 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 = 0
for x in [1,2,3,4,5,6,7,8,9,10]:
    sum = sum + x
print(sum) 
#100以内所有奇数的求和
sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
print(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'] = 48
print(personMessage)
#不存在会报错 
#personMessage['papa']

#可用in测试返回False,命令行中不显示内容
print('papa' in personMessage)
#get可以指定返回内容
print(personMessage.get('papa',-1))

输出结果:

C:\Users\allen\pythonwork>python3 dict_set.py
18
28
35
{'sixuncle': 35, 'allen': 18, 'curry': 28, 'mama': 48}
False
-1

*dict 内部不存在排序

set:
set 和 dict 类似,也是一组 key 的集合,但不存储 value 。由于 key 不能重复,所以,在 set 中,没有重复的 key,可去重啊!

#set
s = 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

希望对那么可爱,还来看我博客的你 有些许的帮助!

感谢 廖雪峰 的教程!

猜你喜欢

转载自blog.csdn.net/DOCALLEN/article/details/70858895