python基础之数据类型上篇

python基础之数据类型上篇

1、Python的数据类型主要有以下几种

类型名称
Number整形,浮点型,复数
String字符串
Boolean布尔值
None空值
List列表
Tuple元组
Set集合
Dict字典

可变数据类型:即可被更改,有Dict,Set,List
不可变数据类型:即不可被更改,有Boolean,Tuple,Number,String

2、不可变数据类型

2.1 Boolean

使用场景:主要用于条件判断和循环中
布尔值只有0和1,1即为True,0即为False

True1
False0
a = True
b = False
print(type(a))#<class 'bool'>
print(type(b))#<class 'bool'>

2.2 None

None:python中一种特殊的数据类型,和0区分,即None不等于0

n = None
printtype(n))#<class 'NoneType'>
n == 0 #False

2.3 Number

2.3.1 类型

整形:即整数,如0,-1,2,340等
浮点型:即数学中的小数,如0.1,0.4,-1.23等
复数:表示为a+bj,其中a代表复数的实部,b代表复数的虚部,,j代表(-1)^0.5, 当 b=1 或 -1 或 0 时,j也不能省略,

#整形
print(type(1))#<class 'int'>
#浮点型
print(type(1.3))#<class 'float'>
print(type(1.))#<class 'float'>
#复数
print(type(3+4j))#<class 'complex'>
2.3.2 数字类型相互转换

整形用 int(),浮点型用float(),复数用complex(x,y)其中x是实部,y是虚部

# int()
c = 1.2
c1 = int(c)
print(c1)# 1
# float
c2 = float(c1)
print(c2)# 1.0
# complex(x,y)
print(complex(c2))#1+0j
2.3.3 数字类型数学功能
# 求绝对值
a1 = -10
print(abs(a1))# 10
a2 = 3+4j
print(abs(a2))# 5.0 ,复数的绝对值等于实部平方加虚部平方开根号,返回浮点型
#求最大值
l = [1,2,3,4,5]
print(max(l)) # 5
#求最小值
print(min(l)) # 1
#向上取整
import math
a3 = 18.1
a4 = math.ceil(a3)
print(a4)# 19
#向下取整
a5 = math.floor(a3)
print(a5)# 18
#开平方
a6 = 100
print(math.sqrt(a6))# 10
# 平方
a7 = 10
print(pow(a7,2)) # 100
print(pow(a7,3)) # 1000
print(pow(a7,0.5))# 3.1622776601683795

2.4 String

2.4.1 字符串创建

字符串是 Python 中最常用的数据类型。我们可以使用引号 ( ’ 或 " ) 来创建字符串。如:

a = 'hello,world!'
b = "hello,world!"

另外,程序中索引由0开始计数,即0,1,2,3,4…,如下图

在这里插入图片描述

m = 'hello,world!'
# 获取某一元素
print(m[0])# h
print(m[-1])# !
print(m[3]) # l
print(m[-3])# l
# 首字母变大写
print(s.title())# Hello,World!
# 截取(包起始位置的值,不包含尾部)
print(s[0:5])# hello,
# 截取e,从1到最后,注意第一个位置下标为0
print(s[1:])
# 全部输出
print(s[:])# hello,world!
#  重复输出
print(s * 2)#hello,world!hello,world!
# 倒序输出
print(s[::-1])# !dlrow,olleh
# 去除空格
s = " hello.py "
print(s.lstrip()) #l去除左边空格
print(s.rstrip()) #r去除右边空格
print(s.strip())  #strip去除左右两边的空格
2.4.2 字符串的转义

转义:代码中需要使用 特殊字符时,python用\进行转义,如你单纯想输出\,但python会将\识别为换行符,因此正确的应为\\

类型名称
\在行尾时为续行符
\n换行
\t横向制表符(4个空格)
\v纵向制表符
\\反斜杆符号
\’单引号
\’’双引号
\e转义
\f换页
\r回车
# 续行符
print("This is a new world,you should learn more knowledge\
knowledge is a matter of science,and no dishonesty or \
conceit whatover is permissible")
# This is a new world,you should learn more knowledgeknowledge is a matter of science,and no dishonesty or conceit whatover is permissible
# 换行符
print("hello,\nworld")
# hello,
# world
# 横向制表符
print("hello\tworld!")
# hello    world!
# 反斜杆符号
print("C:\\users\\desktop\\python.py")
# C:\users\desktop\python.py
2.4.3 字符串格式化输出

字符串的格式化输出主要有两种形式,一种是%s,%d,%f,代表不同类型的占位符,%s代表String类型,%d代表Int类型,%f代表Float类型,代码如下:

print("num = %d, str = %s, f = %f" % (3,"hello",3.21))#num = 3, str = hello, f = 3.21
print("%-10.2f"%22.11111,'modeng') #10 为10个长度 用做左对齐
# 输出结果:      22.11 modeng
print("%10.2f"%22.11111,'modeng')  #10 为10个长度 用做右对齐,右对齐是默认的
# 输出结果: 22.11      modeng

第二种是format()格式化输出,是python2.6版本以后新增的,以{}作为占位符,
^, <, > 分别是居中、左对齐、右对齐,后面带宽度,
: 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
+ 表示在正数前显示 +,负数前显示 -;
(空格)表示在正数前加空格
b、d、o、x 分别是二进制、十进制、八进制、十六进制。
format使用结构如下:

aaa = 3
print("{:<20}".format(aaa))# 3                   注意3后面默认填充19个空格
print("{:.3f}".format(aaa)) #3.000
print("{:.2%}".format(aaa)) # 300.00%
print("{:*^20.2f}".format(aaa)) # ********3.00********
# 可以类似于银行数字输出
ccc = 1233454567567
print("{:,}".format(ccc))# 1,233,454,567,567
# 还可指定位置
print("{1} {0} {1}".format("hello", "world"))
2.4.4 字符串常见函数
函数用法
isnumeric()如果字符串中只包含数字字符,则返回 True,否则返回 False
splitStr.join(Str)以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
center(self,width,fillchar)以原始字符串为中心生成新字符串
ljust(width,[str])返回一个原字符串左对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格
rjust(width,[str])返回一个原字符串右对齐,并使用 fillchar 填充至长度 width 的新字符串,fillchar 默认为空格
lstrip()/rstrip()/strip截掉字符串左/右边的空格或指定字符/俩侧
max(str)/min(str)返回字符串 str 中最大/小的字母
chr(x)将一个整数转换为一个字符
ord(x)将一个字符转换为它的整数值
myStr.replace(old,new[,max])将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次
find(str,beg=0,end=len)/index(str)查找某个字符出现的位置 从左侧开始查找 查到返回索引值 查不到返回-1
rfind(str,beg=0,end=len)/rindex(str)查找某个字符出现的位置 从右侧开始查找 查到返回索引值 查不到返回-1
split(str=’’[,num])以 str 为分隔符拆分字符串为列表,如果 num 有指定值,则仅截取 num 个子字符串
swapcase()将字符串中大写转换为小写,小写转换为大写
upper()转换字符串中的小写字母为大写
lower()转换字符串中的写字母为小写
splitlines(True)以回车换行为条件分割,参数用来设置是否保留换行
# isnumeric()
Str = "123"
Str2 = "acd12"
print(Str.isnumeric())# True
print(Str2.isnumeric())# False
# splitStr.join(Str)
Str3 = " ".join("helloworld")
print(Str3)# h e l l o w o r l d 注意.join()性能优于+
#center(self,width,fillchar)
Str4 = Str.center(20,'*')
print(Str4)# ********123*********
# chr(x)
ddd = 123
print(chr(ddd))# {
# ord(x)
eee = '!'
print(ord(eee))# 33
# swapcase()
fff = 'HelLo,World'
new_fff = fff.swapcase()
print(new_fff)# hELlO,wORLD
# upper()
print(fff.upper())# HELLO,WORLD
# lower()
print(fff.lower())# hello,world

2.5 Tuple

Tuple:元组
元组本质上也是有序的集合,和列表非常相似区别:​
a.列表使用[]表示,元组使用()表示​
b.列表可以修改元素值,元组不可以
注意:元组一旦被初始化之后,就不能发生改变

2.5.1 创建元组

语法:元组名 = (元组元素1,元组元素2,元组元素3…元组元素n)

tuple1 = (1,2,3,4,4)
print(tuple1)
print(type(tuple1)

输出:(1, 2, 3, 4, 4)
<class ‘tuple’>
创健只有一个元素的元组,注意逗号的使用,如果不加逗号会认为是整形

tuple2 = (1,)
print(type(tuple2))

输出:<class ‘tuple’>

2.5.2 列表的切片与索引

元组的切片与索引和列表相似,都可以采用[m,n,s],其中m代表起始位置,n代表结束位置,s代表步长,但不可被修改

tuple3 = (1,2,3,4,5,6,7,8,9,10)
print(tuple3[2])
print(tuple3[-2])
print(tuple3[2:6])
print(tuple3[2:6:1])

输出:
3
9
(3, 4, 5, 6)
(3, 4, 5, 6)

2.5.3 元组的操作
#1.元组的连接:+
tuple1 = (2,4,54,6)
tuple2 = (4,6565,87)
print(tuple1 + tuple2)
#2.元组元素的重复:*
print(tuple1 * 3)
#3.成员判断:in 和 not in
print(45 in tuple1)
print(33 not in tuple1)
2.5.4 元组的函数
函数作用
len()输出元组的长度
max()获取元组的最大值
min()获取元组的最小值
tuple()将列表转换为元组
tuple4 = (1,2,3,4,5,6,7,8,9,10)
print(len(tuple4))
print(max(tuple4))
print(min(tuple4))

输出:
10
10
1

# 列表转换为元组
l = [2,4,7,8]
print(tuple(l))

输出:
(2, 4, 7, 8)

下篇:python基础之数据类型下篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值