python001

自学Python的第一天(数据类型和一些方法)

先介绍一些环境上的知识(关于pip的):pip 是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。

示例
pip install requests
pip search xml
pip show beautifulsoup4
pip uninstall requests
pip list 查看所有的包
pip-V 查看安装的包在哪个路径
pip install ipython 安装ipython环境

1.关于运算的一些知识

除法 /
取余 %
取整 //
幂运算 **
import math
math.cell(7/3) 向上取整
math.floor(7/3) 向下取整

2.数值类型(int(整型)、float(浮点型)、complex(复数))

(1)int整型
a = 10           #十进制
print('a的数据类型是',type(a))  #a的数据类型是 <class 'int'>
b = 0b101001     #二进制
print('b的数据类型是',type(b))  #b的数据类型是 <class 'int'>
c = 0o42567      #八进制
print('c的数据类型是',type(c))  #c的数据类型是 <class 'int'>
d = 0x598AE      #十六进制
print('d的数据类型是',type(d))  #d的数据类型是 <class 'int'>

补充:整型的实际取值范围受限于运行的计算机的内存大小。
32位:-2 ^ 31~2 ^ 31-1
64位:-2 ^ 63~2 ^ 63-1

(2)float浮点类型(python语言要求所有浮点数必须带有小数部分)
a = 0.0
print('a的类型是',type(a))     #a的类型是 <class 'float'>
b = 75.
print('b的类型是',type(b))     #b的类型是 <class 'float'>
c = -3.45325
print('c的类型是',type(c))     #c的类型是 <class 'float'>
d = 9.8e-2
print('d的类型是',type(d))     #d的类型是 <class 'float'>
补充:浮点数不精确
s = 0.1 + 0.2
print(s)
#0.30000000000000004

解决方法:decimal

import decimal
a = decimal.Decimal(3.14159265357879)
b = decimal.Decimal(4.31451353667421)
decimal.getcontext().prec = 20     #精确位数
print(a * b)         #13.554444030581940439         
(3)complex复数(实数部分和虚数部分都是浮点类型)
a = 23 + 4j
print('a的数据类型是:',type(a))      #a的数据类型是: <class 'complex'>
print(a.real)                       #实部 23.0
print(a.imag)                       #虚部 4.0

(4)三种数字类型的相互转化
a = 2
b = 3.14
c = '2'                    #数字类型的字符串
#转换为整数(强制类型转换)
d = int(b)
print(d)                   #3
e = int(c)
print(e)                   #2
#转换为浮点数
d = float(a)
print(d)                   #2.0
e = float(c)
print(e)                   #2.0
#转换为复数
d = complex(a,b)
print(d)                   #(2+3.14j)
e = complex(a,c)
print(e)                   #报错。两个参数时,前后都不允许有字符串
f = complex(c)
print(f)                   #(2+0j)    

3.布尔值bool(True、False)

布尔值为False的数:

  1. None、False(注意,这两个不要加引号,加上引号为字符串,会输出为True)
  2. 所有值为0的数,包括:0(整数),0.0(浮点数),0.0+0.0j(复数)
  3. ‘’(空字符串)
  4. [](空列表)
  5. {}(空字典)
  6. ()(空元组)

4.序列类型(字符串str、列表list、元组tuple)

(1)字符串str(’ ‘、" "、’’’ ‘’’、""" “”")

串是有序的、不可变的元素集合
例如:

s = 'abc'	
print(s[0])
​s[0] = 'w'	
print(s)              #报错
A.小知识:多行只能用三引号
a = """	
春眠不觉晓,	
处处闻啼鸟。
夜来风雨声,	
花落知多少。"""
B.字符串------功能集

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

C.字符串的常用功能

1.索引取值(下标取值)

s = 'hello,world' 
print(s[3])            #l 
print(s[-3])           #r

2.长度len()【内置函数,不属于类下面的方法】

s = 'hello,world' 
print(len(s))              #11

3.切片取值(变量[头下表:尾下标] 取头不取尾)

s = 'hello,world' 
print(s[1:3])          #el 
print(s[4:1])          #空白    不能交叉取值(只能从前往后取) 
print(s[:4])           #hell    前面缺是从0开始 
print(s[4:])           #o,world 后面却是到结尾 
print(s[-6:-2])        #,wor 
print(s[0:5:2])        #hlo     第三个参数是步长 
print(s[::-1])         #dlrow,olleh     步长是复数表示逆序(逆向取值) 
print(s[5:0:-2])       #,le逆序不会改变索引值
print(s[0::-1])        #h

4.移除空白strip()

s = ' hello,world  ' 
s1 = 'hello,world' 
print(s.strip())          #移除两端的空格 
print(s.lstrip())         #移除左边的空格 
print(s.rstrip())         #移除右边的空格 
print(s1.strip('h'))      #ello,world   移除指定的字符,这个字符只能是两端的

5.分割split()

s = 'hello,world'	
print(s.split())        #['hello,world']	
print(s.split(','))     #['hello,world']	
print(s.split('l',1))   #['he', 'lo,world']      第二个参数为最大分割,这一行代码是分割一次
print(s.split('l',2))   #['he', '', 'o,world']   以哪个字符作为分割线,哪个字符就没有了字符
(2)列表(list)

列表是有序的可变的元素集合

list = ['a','b','c']
l[0] = 'w' 
print(l)           #['w', 'b', 'c']   说明了可变性
A.举例

a = [] #空列表
​b = [1,1.2,1+2j,True,‘abc’,[]]
c = list()

B.列表------功能集

在这里插入图片描述

在这里插入图片描述###

C. 常用方法

a.索引
l = [‘a’,‘b’,‘c’]
​print(l[0]) #a
print(l[-3]) #a
b.长度

l = ['a','b','c']
​print(len(l))              #3

c.切片(和字符串差不多,不写了)

(3)元组tuple

元组是不可变对象,如果需要改变,转化成列表即可

A.举例:

a = (1,2,3,4)
b = (1,) #b = (1)是一个int类型的
c = 1,2,3,4,5
d = tuple()

B.元组——功能集

在这里插入图片描述

(4)列表元组的相互转化

a = [1,2,3,4]
转元组 tuple(a) (1,2, 3, 4)
转字符串 str(a) ‘[1,2, 3, 4]’

今天补充的小知识

(1)id(a) 查看内存地址
(2)拆包 a,*b,c = [1,2,3,4,5,6] (元组也是可以的)

a = 1
c = 6
b = [2,3,4,5]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值