4、数据类型

4、数据类型

一、for循环补充

  # for + range
  for i in range(10,3,-1):  # 顾头不顾尾
    print(i)>>>>10 9 8 7 6 5 4
  # for+ enumerate
  i,name = (0,"egon")
  for i ,name in enumerate(["egon","tom","jack"]):
    print(i , name)>>>>(0,egon)  (1,tom)  (2,jack)
  for x in enumerate({"k1":111,'k2':2222,'k3':3333}):
    print(x)  >>>>>>(0,'k1') (1,'k2') (2,'k3')

二、可变不可变类型

  ## 不可变类型:值改变,但是 id 也会改变,证明是产生了新的值,原值并没有变化  
  x = 10
  print(id(x)) >>>>>>2857250744912
  x =11
  print(id(x)) >>>>>>2857250744944       # id随着值改变了 
  ## 可变类型:值改变,但是id不变,证明就是在改变原值,原值是可变类型
  l = [111,222,333]
  print((id(l))
  l[0] = 66666666
  print(id(l))>>>>>>id 地址相同,就不打了 费时间

三、数字类型:

============== int 的基本使用 ================

1、用途:记录年龄、个数、号码、等级等整数相关的
2、 定义方式:
  age = 18 相当于  age = int(18)
    print(age,type(age))>>>>>>18,<class,int>
int 数据类型转换:可以把纯数字组成的字符串转成整型
  res = int("                         18                  ")   # 注意   18 中间不能有空格!!!
  print(res,type(res)) >>>>18,<class,int>
3、常用操作 + 内置的方法
数字运算+ 比较运算

================= 类型总结====================

================== float 的基本使用===============

1、用途:记录薪资,体重,身高等小数类型

2、定义方式:
  salary = 3.3   相等于  salary = float(3.3)

 # float 数据类型转换:可以把小数组成的字符串转成浮点型
  res = float('      3.3       ')
  print(res,type(res))>>>>>>>>3.3 <class,float>
#3、常用操作+内置方法
#数学运算+比较运算               和整型int一样

===float 类型总结=
和 int 一样
补充了解:
LONG类型


print(bin(11))
print(oct(11))
print(hex(11))

x=1-2j
print(type(x))
print(x.real)
print(x.imag)

四、字符串类型
==基本使用=
1、用途:记录描述事物的状态
2、定义方式:在" " , ' ' , """ """ ,''' '''' 内包含一串字符

  # msg = "abc"  # msg = str("abc")    
  #print(type(msg))>>>>>>>>>>>class,str

注意

      # 1、上述引号定义出来的都是 str  类型,单引号,双引号在python中没有区别。
      #2、三引号可以存放多行字符串
      #3、引号的嵌套:外层双引号,内层只能用单引号

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

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

补充

# print("abc
ddd")
# print(r"abc
ddd")
# print("abc\nddd")
# print('abc
ddd')

# file_path=r'D:
ewa.py'
# file_path='D:\new\a.py'
#
# print(file_path)

3、常用操作+内置方法

    #优先掌握的操作:(5*)
    #1、按索引取值(正向取+反向取):只能取
    #msg = ‘hello world "
    #print(msg[0],type(msg[0]))
    #print(msg[-1])
    #msg[0] = 'H' # 字符串只能取,不能改

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

# msg = 'hello woeld"
# res = mas[1:7]>>>>>>>>1 2 3 4 5 6 
#res = msg[1:7:2]>>>>>>>>>1 3 5
res = msg[:]
print(res)>>>>>>>>>>>>只有  :  相当于复制

3、长度len

# msg = "hello world"
#print(len(msg))>>>>>>11

4、成员运算 in和not in

msg = ’hello world‘
print(’he‘ in msg)>>>>>>True
print("he" not in msg)>>>>>>>>>>Fase

5、移除空白strip

msg = "              hello     "
res = msg.strip()
print(res)>>>>>>>>>>>hello
orint(msg)>>>>>>>>>>                     hello
msg = " +_*%hello******$#
print(msg.strip("+_*%#$)>>>>>>>>>>>>hello   stirp() 可移除符号,不过只能移除两边的

6、切分 split

msg = "egon:123:3000"
print(msg[0:4])>>>>>egon
res = msg.split(":",1)
print(res)>>>>>>>["egon",'123:3000]

7、循环

#for  x in msg:
  print(x)

需要掌握的操作

1、.strip(移除两端空格或指定符号) . lstrip(移除左边。。。) .rstrip(移除右边)
2、.lower ()全部变为小写 . upper()全部变为大写
3.startwith()判断xx是不是值开头 .endwith()判断xx是不是值的结尾
4、format()的三种玩法:


        # msg = "my name is %s my age is %s"%("liu",18)
        ## msg = "my name is {name} my age is {age}".format(age= 18,name="liu")
        ### msg = "my name is {1} my age is  {0}.format(18,'liu')

补充了解       


          # msg = "my name is {name} my age is {age}".format(**{"age":18,"name":"egon"})
          # msg = "my name is %(name)s my age is %(age)s" % {"age":18,"name":"egon"}
          # print(msg)

          # name = "egon"
          # age = 18
          # print(f"my name is {name} my age is {age}")   

5、split()切分,可指定以什么为切分 和 切分的次数 从左切分, rsplit() 从右切分
6、jion()和 split()相反 用一次split在用jion 值又回到了原来的样子
7、replace()替换,可指定需要替换的参数和用来替换的参数
8、isdigit() 用来判断用户输入的是否是数字
 
(了解即可)***

      #1、find,rfind,index,rindex,count
      # msg = 'hello el abc'
      # res=msg.find("el")
      # res=msg.rfind("el")

      # res=msg.index("el")
      # res=msg.rindex("el")

      # res=msg.find("xxx")
      # res=msg.index("xxx")  # 找不到则报错
      # print(res)

      2、center,ljust,rjust,zfill

      # print('hello'.center(50,'*'))
      # print('hello'.ljust(50,'*'))
      # print('hello'.rjust(50,'*'))
      # print('hello'.zfill(50))  # 'hello'.rjust(50,'0')

      3、captalize,swapcase,title

      # print("hello world".capitalize())
      # print("aAbB".swapcase())
      # print("hello world".title())

      4、is数字系列
     在python3中
      # num1=b'4' #bytes
      # num2=u'4' #unicode,python3中无需加u就是unicode
      # num3='四' #中文数字
      # num4='Ⅳ' #罗马数字

      # bytes、unicode
      # print(num1.isdigit()) # True
      # print(num2.isdigit()) # True
      # print(num3.isdigit()) # False
      # print(num4.isdigit()) # False

      # unicode
      # print(num2.isdecimal()) # True
      # print(num3.isdecimal()) # False
      # print(num4.isdecimal()) # False

      # unicode、中文数字、罗马数字
      # print(num2.isnumeric()) # True
      # print(num3.isnumeric()) # True
      # print(num4.isnumeric()) # True

      5、is其他

      name="EGON123"
      # print(name.isalpha())  # 只能由字母组成
      # print(name.isalnum())  # 字母或数字组成
      # print(name.islower())
      # print(name.isupper())
      # name="               "
      # print(name.isspace())
      # name="Hello world"
      # print(name.istitle())

五、列表类型:
  =列表的基本使用============
  1、用途:按照位置存放多个任意值
  2、定义方式:在[ ]内用逗号分隔开多个任意类型的值

                  # l = [11,11.33,"xxx",[11,22,33]] # l = list(...)
                  # print(type(l))

list数据类型:把可迭代的类型转成列表,可以被for 循环遍历的类型都是可迭代的

                  # res = list("hello")
                  # res = list({"k1":111,'K2':2222})
                                           # print(res)
                  # print(list(range(1000)))

3、常用操作+内置方法
优先掌握的操作

      1、按照索引存取值(正向存取+反向存取):可存可取
      2、切片(顾头不顾尾,步长)
      3、长度 len()
      4、成员运算in和 not in
      5、追加 .append() 插在后面
      6、插入 .insert() 可插入指定索引任意位置
      7、删除 通用删除del [ ] 和 .pop() 指定索引删除,不指定默认删最后一个

类型总结=

存一个值 或 多个值
有序 或 无序
可变 或 不可变(1,可变:值变,id 不变。 可变==不可hash 2,不可变:值变 id 就变 不可变 == 可 hash

原文地址:https://www.cnblogs.com/liuyang521/p/14175771.html