码迷,mamicode.com
首页 > 编程语言 > 详细

自学Python全栈开发第四次笔记(Python常用数据类型,字符串)

时间:2017-12-23 23:01:39      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:位置   bool   不能   fill   new   cas   基本   wap   解释   

今天开始学Python常用数据类型了。
基本数据类型;
数字                int
字符串            str
布尔值            bool
列表                list
元组                tuple
字典                dict
 
所有字符串或者数字,字典,所具备的方法存在相对应的“值“里      按Ctrl+左击显示
 
模板是类(int,str,bool......)
模板创建不是值是对象
关系:所有对象所具备的功能都存在相对应的类。
 
 
  1. 查看对象的类或对象所具备的功能
          a.通过type
          temp = "alex"
          t = type(temp)
          print(t)
###如果显示str,Ctrl+左击找到str类,内部所有方法
  1. temp = "alex"
          b= dir(temp)
  1. 通过help,type
  2. 直接点击
            temp = "alex"
            temp.upper()
鼠标放在upper上,Ctrl+左击,自动定位到upper功能解释
 
 
 
 
ret = n1()            没有参数
ret = n1(2)          传了一个参数
ret = n1(2,abc,caa)       传了三个参数
 
 
self            表示不用传参数
self,with,fill    最多传两个参数
self star = None    默认有值(可传可不传)
 
 
 
 
 
 
基本数据类型常用功能:
  1. 整数 int
         a.
            #n1= 123
            #n2 = 456
            #print(n1+n2)
            #print(n1._add_n2)
        b.
            获取可表示二进制最短位数
            n1 = 4   #000000/00
            ret = n1.bit_length()
            print(ret)
 
  1. 首字母变大写 capitalize(self)
            a1 = "qiao" 
            ret = a1.capitalize()
            print(ret)
            ###打印Qiao
 
 
  1. center(self,width,fillchar=None)
        """内容居中,width:总长度;fillchar:空白处填充内容默认无"""
            a1 = "qiao"
            net = a1.center(20,‘*‘)
            print(ret)
            ###打印--------qiao--------
  1. count(self,sub,star=None,end=None)
            """子序列个数"""
            a1 = "qiao"
            ret = a1.count("a")
            print(ret)                                                            /ret = a1.count("a",0,3)       0 = q       1 = i       2 = a
            ###打印1(a有1次出现)
  1. endswith(self,suffix,start=None,end=None)                              
            """是否以xxx结束"""
            a1 = "qiao"
            ret = a1.endswith("0")                                       获取字符中大于0小于2的位置
            print(ret)                                                           /ret = a1.endswith("0",0,2)
            ###打印Ture/False    
  1. expandtabs(self,tabsize = None)
            """将Tab转换成空格,默认一个Tab转换成8个空格"""
            content = "hello\tqqq"
            print(content)
            print(content.expandtabs())
            print(content,expandtabs(20))
            ###hello    999
                  hello    999
                  hello                        999
  1. find(self,sub,star = None,end = None)
            """寻找子序列位置,如果没找到返回-1"""
            s = "qiao hello"
            print(s.find("ao"))
            ###打印2(从前开始向后找,找到第一个)若找不到显示"-1"
  1. format(*args,**kwargs)
            """字符串格式化,动态参数"""
            s = "hello{0},age{1}"
            print(s)
            #{0}占位符
            new = s.format("qiao",19)
            print(new1)
            ###打印hello{0},age{1}
                  hello qiao,age19
  1. join(self,iterable)
            """连接"""
            li = ["qiao","wang"]      #列表[ ]        (  )元组
            s = "_".join(li)
            print(s)
            ###打印qiao_wang
  1. lstrip(self,chars = None)    rstrip(移除右侧空白)          strip(左右两边都去掉)
            """移除左侧空白"""
            s = "qiao"
            news = s.lstrip()
            print(news)
            ###打印qiao...            ...qiao
  1. partition(self,sep)
            """分割,前,中,后三部分"""
            s = "alex SB alex"
            ret = s.partition("SB")
            print(ret)
            ###打印(‘alex‘,‘SB‘,‘alex‘)      元组类型
  1. replace(self,old,new,count = None)
            """替换"""
            s = "alex SB alex"
            ret = s.replace("al","bb")              ("al","bb",从左向右第一个)
            print(ret)
            ###打印bbex SB bbex
  1. rstip(self,chars = None)
            """分割"""
            s = "alexalex"
            ret = s.split("e")             ("e",1)
            print(ret)
            ###打印[‘al‘,‘xal‘‘x‘]
  1. swapcase(self)
            """大写变小写,小写变大写"""
            s = Qiao
            print(s.swapcase())
            ###打印qIAO
  1. title(self)
            """转换标题"""
            s = "the school"
            ret = s.title()
            print(ret)
            ###打印TheSchool
索引
s = "alex"
print(s[0])
print(s[1])
print(s[2])
print(s[3])
###打印
a
l
e
x
 
 
 
长度
s = "alex"
ret = len(s)
print(ret)
###打印
4(有4位,从0开始)
 
 
切片
s = "alex"
print(s[0:2])            >=0   <2   (0,1)
###打印
al  
 
 
for循环,break,continue可以引用
s = "alex"
for item in s :
    print(item)          (item是变量名)
###打印
a
l
e
x
 
 
                      continue
s = "alex"
for item in s :
    if item == "l"
        continue
   print(item)
###打印
a
e
x
 
 
                      break
s = "alex"
for item in s :
    if item == "l"
        break
    print(item)
###打印
a
 
 
 
for item in s :          (item是变量名,随意)(s是要循环的东西)
终于整理完了。好累,腰酸背痛。以后不能把知识积累这么多再整理blog了,一定要及时整理,晚安。

自学Python全栈开发第四次笔记(Python常用数据类型,字符串)

标签:位置   bool   不能   fill   new   cas   基本   wap   解释   

原文地址:http://www.cnblogs.com/qiaotianzi/p/8094445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!