python全栈开发 day5 五、基本数据类型的基本操作和内置方法

一、可变与不可变

可变:值改变,但是id不变,证明就是在改变原值,是可变类型。它的原理是在内存里有一个值,然后这个值发生了改变,意为id地址是同一个,没有变化

# l=['a','b']
# print(id(l))             # 结果为  34743240
# l[0]='A'
# print(l)                  # 结果为  ['A','b']
# print(id(l))             # 结果为  34743240

不可变:值改变,但是id也跟着变,证明是产生了新的值,是不可变类型。它的原理是在内存里有一个值,然后又申请了另一个地址,则id地址不同。称为不可变类型。

# x = 9
# print(id(x))           # 结果为 1580166880

# x = 10
# print(id(x))           # 结果为 1580166912

 

 

二、数字类型

# 1、整形
# 基本使用

# 用途:记录数字,年纪,手机号


# 定义方式
age = 18
age = int(18)

print(age,type(age))
# 数据类型转换: 只能将纯数字的字符串转成int

# a = int('123456')
# print(a,type(a))

# a = int('123')
# print(a,type(a ))
# 总结

# 存一个值
# 不可变(值变,id就变。)

# x = 9
# print(id(x))           # 结果为 1580166880
# x = 10
# print(id(x))           # 结果为 1580166912

 

 

三、字符串类型

# 基本使用
# 用途:记录描述性质的状态

# 定义方式:用单引号,双引号,三引号中包含一系列字符


l = 'abcde'
l = str(l)
print(l,type(l ))                     # 结果为 abcde <class 'str'>

name = 'hello'
name = str(name)
print(name,type(name))        # 结果为 hello <class 'str'>
# 数据类型转换:所有类型都可以被str转成字符串类型

# res=str([1,2,3])
# print(res,type(res))      # 结果为 [1, 2, 3] <class 'str'>


# l = [1,2,3,4,5,6]
# l = str([1,2,3,4,5,6])
# print(l,type(l))            # 结果为 [1, 2, 3, 4, 5, 6] <class 'str'>
# 常用操作+内置的方法

# 1、按索引取值(正向取+反向取) :只能取
# name = 'helloworld'
# print(name[0],name[1],name[2],name[3],name[4],type(name ))
#  结果为 h e l l o <class 'str'>

# print(name[-1],name[-2],name[-3],name[-4],name[-5],type(name ))
#  结果为  d l r o w <class 'str'>

# print(name[0])
# print(name[1])
# print(name[2])
# print(name[3])


# 2、切片(顾头不顾尾,步长)

# name = 'helloworld'
# mag = name[0:10]   # 取 0 1 2 3 4 5 6 7
# print(mag)         # 结果为 helloworld

# mag = name[::]
# print(mag)           # 结果为 helloworld

# name = 'helloworld'
# mag = name[-1:-11:-1]      # 取 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
# print(mag)          # 结果为 dlrowolleh

# mag = name[-1::-1]
# print(mag)          # 结果为 dlrowolleh

# mag = name[::-1]
# print(mag)          # 结果为 dlrowolleh


# 3、长度len
# name = 'helloworld'
# print(len(name))          # 结果为10个字符


# 4、成员运算in和not in:判断一个子字符串是否存在于大字符串中

# name = 'helloworld'
# print('i'in name)     # 结果为 False
# print('hell'in name)  # 结果为 True


# 5、移除空白strip: 用来去除字符串左右两边的字符,不指定默认去除的是空格

"""
name = input('your username:>>>').strip()
password = input('your passworld:>>>').strip()
if name == 'helloworld'and password =='1121':
    print('login successful')

else:
    print('error')
"""

# name = '    helloworld'
# name = name.strip()
# print(name)           # 结果为helloworld 前面没有空格

# name = '&*helloworld'
# name = name.strip('&*@#$%')
# print(name)             # 结果为helloworld


# name = '&*hello####world&*'
# name = name.strip('#&*@#$%')
# print(name)             # 结果为hello####world
# 注意  hello与world左边和右边的字符会消失,但是中间的字符还存在。


# name = name.strip('&*@#$%')
# print(name)
# print('&*helloworld@#$%'.strip('&*@#$%'))  # 结果为helloworld


# 6、切分split:针对有规律的字符串,按照某种分隔符切成列表

"""

name = ('jerry:25:male')
name = name.split(':')
print(name)            # 结果为 ['jerry', '25', 'male']

name = ('jerry:25:male')
name = name.split(':',1)
print(name)            # 结果为  ['jerry', '25:male']
"""

# 7、join用法。用:号作连接符号将纯字符串的列表拼接成一个字符串

# name = ['jerry','25','male']
# name = name[0]+':'+name[1]+':'+name[2]
# print(name)       # 结果为 jerry:25:male

# name = ['jerry','25','male']
# name = ':'.join(name)
# print(name)         # 结果为 jerry:25:male


"""
# 8、循环
for name in 'hello':
    print(name)        # 结果为
# h
# e
# l
# l
# o
#  需要掌握的操作:以下用name=tom举例说明


# 1、strip,lstrip,rstrip
# 移除空格strip:移除字符串左右两边的字符
# lstrip:移除字符串左边的字符
# rstrip:移除字符串右边的字符

# print('******tom***********'.strip('*'))    # 结果为 tom
# print('******tom***********'.lstrip('*'))   # 结果为 tom***********
# print('******tom***********'.rstrip('*'))   # 结果为 ******tom

"""

# 2、lower,upper
# lower:大写字母变小写
# upper:小写字母变大写
print('TOM123'.lower())   # 结果为 tom123   
print('tom123'.upper())   # 结果为 TOM123   

name = 'TOM'
print(name.lower())
name = 'tom'
print(name.upper())
"""

# 3、startswith,endswith
# name ='tom is me'
# print(name.startswith('t'))     # 结果为 True   首字母
# print(name.endswith('e'))       # 结果为 True   尾字母
# print(name.endswith('is'))        # 结果为 False

# 4、format的三种玩。格式化输出。和%s类似。但format可以任意位置的

# name ='my name is %s my age is %s' %('tom',25)
# print(name)      # 结果为 my name is tom my age is 25

# 方法一
# tag = 'my name is {name} my age is {age}'.format(name = 'tom',age =25 )
# print(tag)       # 结果为 my name is tom my age is 25
"""

# 了解
# 方法二
tag ='my name is {0} my age is {1}'.format('tom',25)
print(tag)         # 结果为 my name is tom my age is 25
# 方法三
tag='my name is {0}{1} my age is {1}{1}{1}{1}'.format('tom',25)
print(tag)         # 结果为 my name is tom25 my age is 25252525
"""

# 5、split,rsplit
# split:切分(从左往右)
# rsplit:切分(从右往左)

"""
msg='a:b:c:d'
print(msg.split(':',1))           # 结果为  ['a', 'b:c:d']
print(msg.rsplit(':',1))          # 结果为  ['a:b:c', 'd']
name = 'jerry:tommy:judy:una'
print(name.split(':',1))        # 结果为 ['jerry', 'tommy:judy:una']
print(name.rsplit(':',1))       # 结果为 ['jerry:tommy:judy', 'una']
"""

# 6、replace:替换
# name='alex say :i have one tesla,my name is alex'
# print(name.replace('alex','SB',1))

# name = 'jerry say :my name is jerry,a college student'
# print(name.replace('jerry','man',1))  # 结果为 man say :my name is jerry,a college student

# 7、isdigit:如果字符串是由纯数字组成的,则返回True

# print('123123'.isdigit())
# print('123123   '.isdigit())
# print('123123asdf'.isdigit())
# print('12312.3'.isdigit())
#  字符串类型总结

# 存一个值
# 有序

# 不可变(值改变,但是id也跟着变,证明是产生了新的值,是不可变类





四、列表类型
# 按照位置记录多个任意类型的值。并且可以取出指定位置的值。
# 通过索引对应值,从0开始,代表的是位置。
# 定义:在中括号内用逗号隔开任意类型的值
name = ['jerry', 25, 175.5, [120, 5000]]

# 数据类型转换:但凡能够被for循环的数据类型都可以传给list,被其转换成列表
# name = list('hello')
# print(name)               # 结果为 ['h', 'e', 'l', 'l', 'o']

# name = ['jerry', 25, 175.5, [120, 5000]]
# name = list(name)
# print(name)               # 结果为 ['jerry', 25, 175.5, [120, 5000]]

# res = list({'a': 1, 'b': 2, 'c': 3})
# print(res)    # 结果为 ['a', 'b', 'c']


# 常用操作 + 内置方法

# 优先掌握的操作:
# 1、按索引存取值(正向存取+反向存取):即可存也可以取
# name = ['jerry', 25, 175.5, [120, 5000]]
# print(name[0], name[1], name[2], name[3])      # 结果为 jerry 25 175.5 [120, 5000]
# print(name[-1], name[-2], name[-3], name[-4])  # 结果为 [120, 5000] 175.5 25 jerry

#   注意注意注意:对于不存在的索引会报错

# 可存:
# dic = {"k1":111}
# dic['k2'] = 2222
# print(dic)         # 结果为 {'k1': 111, 'k2': 2222}


# 2、切片(顾头不顾尾,步长)

"""
name = ['jerry', 25, 175.5, [120, 5000]]
print(name[0:4:1])     # 结果为 ['jerry', 25, 175.5, [120, 5000]]
print(name[::1])       # 结果为 ['jerry', 25, 175.5, [120, 5000]]
print(name[0::1])      # 结果为 ['jerry', 25, 175.5, [120, 5000]]
print(name[-1::-1])    # 结果为 [[120, 5000], 175.5, 25, 'jerry']
print(name[::-1])      # 结果为 [[120, 5000], 175.5, 25, 'jerry']
print(name[:])         # 结果为 ['jerry', 25, 175.5, [120, 5000]]
print(name[::])        # 结果为 ['jerry', 25, 175.5, [120, 5000]]
"""

# 3、长度len
# name = ['jerry', 25, 175.5, [120, 5000]]
# print(len(name))      # 结果为4


# 4、成员运算in和not in
# name = ['jerry', 25, 175.5, [120, 5000]]
# print('jerry' in name)                  # 结果为True
# print('tommy' not in name)              # 结果为True


# 5、追加append和插入insert
# 追加append:只能往后追加
# name = ['jerry', 25, 175.5, [120, 5000]]
# name.append('abc')
# print(name)     # 结果为 ['jerry', 25, 175.5, [120, 5000], 'abc']

# name = ['jerry', 'tommy', 'judy']
# name.append('una')
# print(name)    # 结果为 ['jerry', 'tommy', 'judy', 'una']

# name = ['jerry','tommy','una','judy','mike']
# name.append('xxx')
# name.append('yyy')
# print(name)

# 插入insert:在指定位置插入
# name = ['jerry', 25, 175.5, [120, 5000]]
# name.insert(0, '100000')
# print(name)      # 结果为 ['100000', 'jerry', 25, 175.5, [120, 5000]]

# name = ['jerry', 25, 175.5, [120, 5000]]
# name.insert(1, '100000')
# print(name)      # 结果为 ['100000', 'jerry', 25, 175.5, [120, 5000]]

# name = ['jerry', 25, 175.5, [120, 5000]]
# name.insert(4, '100000')
# print(name)        # 结果为 ['jerry', 25, 175.5, [120, 5000], '100000']


# 6、删除

# 6.1、del:不是列表独有的删除,是通用的删除。del没有返回值,只是单纯的删除。
# name = ['jerry', 25, 175.5, [120, 5000]]
# del name[0]
# print(name)    # 结果为 [25, 175.5, [120, 5000]]

# 6.2、remove:指定要删除的那个元素。和del一样没有返回值,都只是单纯的删除
# name = ['a', 'bbb', 'c', 'd', 'e']
# name.remove('bbb')
# print(name)     # 结果为 ['a', 'c', 'd', 'e']

# 6.3、pop:(指定要删除的那个元素的索引)。有返回值,返回刚刚删掉的那个元素。 意为取走
# name = ['a', 'bbb', 'c', 'd', 'e']
# name.pop()
# print(name)       # 结果为 ['a', 'bbb', 'c', 'd']   # 不指定位置,默认删除最后一个

# name = ['a', 'bbb', 'c', 'd', 'e']
# name.pop(0)
# print(name)         # 结果为 ['bbb', 'c', 'd', 'e']


# 7、循环
# name = ['jerry', 25, 175.5, [120, 5000]]
# for item in name:
#     print(item)
# 结果为
# jerry
# 25
# 175.5
# [120, 5000]
# 需要掌握的操作
# 1、clear:清除
# name = ['aaa', 'bb', 345]
# name.clear()
# print(name)     # 结果为[]


# 2、extend加多个元素
# name = ['aaa', 'bb', 345]
# name.extend(['tommy', 120, 175.5])
# print(name)     # 结果为 ['aaa', 'bb', 345, 'tommy', 120, 175.5]

# 3、.reverse:反过来
# name = ['aaa', 'bb', 345]
# name.reverse()
# print(name)     # 结果为 [345, 'bb', 'aaa']


# 4、sort 排序:只有在类中中所有元素都是同种类型的情况下才能用sort排序(升序)
# name = [1, 100, 1000]
# name.sort()
# print(name)     # 结果为  [1, 100, 1000]

# name = ['z', 'd', 'a']
# name.sort()
# print(name)     # 结果为 ['a', 'd', 'z']


# 列表类型总结
# 存多个值
# 有序
# 可变


# 练习
# 队列:先进先出
# l=[]

# # 入队
# l.append('first')
# l.append('second')
# l.append('third')
# print(l)

# # 出队
# print(l.pop(0))
# print(l.pop(0))
# print(l.pop(0))

# 堆栈:后进先出

 

 
上一篇:两分钟倒计时


下一篇:apache加载模块的说明