一、可变不可变类型

基本数据类型及内置方法_数据类型转换

 如果值改变,但是内存地址不变,证明就是在改变原值,即原值可变
如果值改变,但是内存地址也变了,证明不是在改变原值,而是产生了新的值,即原值不可变

二、数字类型int与float

1.定义:整型:age=10 # 本质age = int(10)

            浮点型:salary=3000.3 # 本质salary=float(3000.3)

2.类型转换: res = int(" 18 ")                 res = float(" 1.8 ")
                    print(res,type(res))             print(res,type(res))
3.使用:数学运算与比较运算

4.存一个值,不可变

三、字符串

1.定义:在单引号\双引号\三引号内包含一串字符

name = "egon"   name = str("egon")

2.类型转换

str数据类型转换:可以把任意类型都转换成字符串类型

res = str([1,2,3])
print(res,type(res)) # "[1,2,3]"

 3.使用

  3.1优先掌握的操作

     (1)、按索引取值(正向取+反向取) :只能取

msg="hello world"
print(msg[4])
print(msg[-1])

    (2)、切片(顾头不顾尾,步长):从一个大字符串中拷贝出一个子字符串

msg = “hello world”
res = msg[1:5:2]
res = msg[1:5]
res = msg[::-1]

 (3)、长度len

msg = 'hello world'
print(len(msg))

 (4)、成员运算in和not in

msg = 'hello world'
print('el' in msg)
print('el' not in msg)  # 推荐
print(not 'el' in msg)

 (5)、移除空白strip

 5.1 括号内不指定字符,默认移除首尾空白字符(空格、\n、\t)
str1 = '  life is short!  '
str1.strip()  
life is short!

 5.2 括号内指定字符,移除首尾指定的字符
>>> str2 = '**tony**'  
>>> str2.strip('*')  
tony

name = '*****eg on****'
print(name.strip('*').replace(" ",''))

msg = "*(&%&%&_+)**&^e*gon*)(^%$$&$"
print(msg.strip('*()&%_+^%$'))

x = 'a c d e'
print(x.replace(' ','',1))
print(x.replace(' ',''))
 
 
案例
 inp_user = input("username>>>: ").strip()  # inp_user = "      egon"
 inp_pwd = input("password>>>: ").strip()
 if inp_user == "egon" and inp_pwd == '123':
    print('ok')
 else:
     print('no')

 (6)、切分split:把一个字符串按照某种分割符切分一个列表

 msg = "egon:18:male:10"
 res = msg.split(':')
 print(res)

res = msg.split(':',1)
print(res)
(6).1 把列表中的元素按照某种分隔符拼接成字符串
info = ['egon', '18', 'male', '10']
msg = ":".join(info)
print(msg)

(7)、循环

msg = "hello world"
for i in msg:
     print(i)

  3.2 需要掌握的操作

 1、strip,lstrip,rstrip
 print("******egon*****".strip('*'))
 print("******egon*****".lstrip('*'))
 print("******egon*****".rstrip('*'))

 2、lower,upper
 print("AbC".lower())
 print("AbC".upper())

 3、startswith,endswith
 print('hello world'.startswith('he'))
 print('hello world'.endswith('d'))

 print('hello world'.startswith('h'))
 print('hello world'.startswith('e',1,4))


 4、format的三种玩法
 print("my name is %s my age is %s" % ('egon', 18))
 print("my name is {name} my age is {age}".format(age=18,name='egon'))

 print("my name is {} my age is {}".format(18,'egon'))
 print("my name is {1} my age is {0}{0}{0}".format(18,'egon'))


 x = 'egon'
 y = 18
 print(f'my name is {x} ,my age is {y}')

 5、split,rsplit
 msg = 'egon:18:male'
 print(msg.split(':',1))
 print(msg.rsplit(':',1))


 6、join
 7、replace
 8、isdigit
 print("18".isdigit())

 age = input('>>>: ').replace(' ', '')
if age.isdigit():
     age = int(age)
     if age > 18:
         print('too big')
     elif age < 18:
         print('too small')
     else:
         print('you got it')
else:
     print("必须输入数字,小垃圾")

  3.3需要了解的操作

1、find,rfind,index,rindex,count
msg = 'hello egon ahahah egon xxx egon'
print(msg.find('egon1'))   在索引中找egon1,找不到显示-1(false) print(msg.index('egon1')) 在索引中找egon1,同find,但在找不到时会报错 print(msg.rfind('egon')) 2、center,ljust,rjust,zfill print('egon'.center(50,'*')) print('egon'.ljust(50,'*')) print('egon'.rjust(50,'*')) print('egon'.rjust(50,'0')) print('egon'.zfill(50)) 4、captalize,swapcase,title print('abcd'.capitalize()) print('AbCd'.swapcase()) print('my name is egon'.title()) 5、is数字系列 # print('18'.isdigit()) # print('Ⅳ'.isnumeric()) 6、is其他 name='egon' print(name.isalnum()) #字符串由字母或数字组成 print(name.isalpha()) #字符串只由字母组成 print(name.islower())#将英文字符串全部变小写 print(name.isupper())#将英文字符串全部变大写 name=' ' print(name.isspace()) name = 'My Is Egon' print(name.istitle()) #每个单词的首字母大写

 该类型总结:存一个值,有序,不可变

四、列表

1.定义方式:在[]用逗号分隔开多个任意类型的元素
 l = [111,33.333,"aaaa",[666,7777]] # l = list(...)
 print(type(l))
2.list数据类型转换

   res = list("hello")
    print(res)

3.常用操作+内置的方法

3.1优先掌握的操作:

 
1、按索引存取值(正向存取+反向存取):即可存也可以取
  l = [111,222,333,444,555,666,777]
  print(l[0])
  print(l[-1])
  print(id(l))
  l[0] = 999
  print(id(l))

  l[7] = 888  # 如果索引不存在则报错

 2、切片(顾头不顾尾,步长)
   l = [111,222,333,444,555,666,777]
   print(l[1:4:1])  # 拷贝

   print(l[:])
   print(l[::-1])

 3、长度
   l = [111,222,333,444,555,666,777]
   print(len(l))

 4、成员运算in和not in
  l = [111,222,333,444,555,666,777]
  print(333 in l)

 5、追加、插入
   l = [111,222,333,444,555,666,777]
   l.append(888)

  l.insert(3,999999999999)
  print(l)


 6、删除
  l = [111, 222, 333, 444, 555, 666, 777]
 (1)万能删除
   del l[1]
   print(l)

  (2) l.remove()
   v = l.remove(333)
   print(l)
   print(v)

 (3) l.pop()
   v = l.pop(2)
   print(l)
   print(v)

  7、循环
   l = [111,222,333,444,555,666,777]
   for i in l:
       print(i)
3.2需要掌握
 
 l = [111,222,333,444,333,555,666,777]
 print(l.index(333))
 print(l.index(9999))

 print(l.count(333))

 l = [111,'a',333,'dddd']
 l.reverse()
 print(l)

 l = [111,'a',333,'dddd']
 l = [111, -3, 99, 27]
 l.sort(reverse=True)
 print(l)

 l = [111, -3, 99, 27]
 nums = [333,444,555]
 l.append(nums)
 l.extend(nums)
 print(l)



 print(len(l))  # l.__len__()

 new_l = l.copy()
 new_l = l[:]
 

五、元组

1、用途:元组就是一种不可变的列表
2.定义方式:在()内用逗号分割开多个任意类型的元素
 t = (11,11.333,"aaa",[666,777])  # t = tuple()
 print(type(t))

 t[0] = 999

 t = (1111,)
 print(type(t))

3.tuple数据类型转换
   print(tuple("hello"))

4.常用操作+内置的方法

4.1优先掌握的操作:

 

1、按索引取值(正向取+反向取):只能取
2、切片(顾头不顾尾,步长)
3、长度
4、成员运算in和not in

4.2需要了解的

t = (111,222,222,222,333)
print(t.count(222))
print(t.index(222))

元组总结:元组是可以存多个值、有序、不可变的