zoukankan      html  css  js  c++  java
  • python番外篇---变量与数据类型

                                                 一.变量

    声明变量:

    name = 'Tom'     #name是变量名,‘Tom’是变量对象
    
    age = 20
    
    
    ##Python中,变量本身没有数据类型。之所以说,‘有数据类型’,因为 取决变量的对象(Python会自动甄别输入的对象 是什么类型)
    

    作用:

    1.当是不确定的内容

       2.当一串内容特别长的时候

          3.当发生重用的时候


                                                二.数据类型

    python数据类型:

                  1.数字

                      2.str    字符串

                          3.list     列表

                               4.tuple  元组

                                    5.dict    字典  

                                           6.set     集合 

    数据类型介绍:

    1.数字

    分为:整型,长整型,布尔,浮点,复数

    2.str    字符串

    2.1索引,切片

    s = 'hello,world!!!'
    
    print(s[1])
    print(s[:5])
    
    
    #结果
    e
    hello
    

    2.1.strip,rstrip,lstrip

    去除空格,去除字符串(首尾)的字母

    s = '   hello  '      #去空格
    re = s.strip()
    print(re)
    
    
    s = '   hello  '    
    re = s.strip()
    re2 = re.lstrip('h')       #去除首字母
    print(re2)
    

    2.2.startswith,endswith

    判断首尾字母,返回 bool值

    s = 'pagndi'
    
    print(s.startswith('p'))
    print(s.endswith('i'))
    
    
    #输出结果
    True
    True
    

    2.3.replace

    替换

    s = 'hello alex,age is 18,myname is alex'
    
    re = s.replace('alex','egon')
    re1 = s.replace('alex','egon',1)
    
    print(re)
    print(re1)
    
    
    ##输出结果
    hello egon,age is 18,myname is egon
    hello egon,age is 18,myname is alex     #只替换第一个
    

    2.4.format,%

    格式化

    name = 'Tom'
    age = 18
    
    print('hello {},age is {}'.format(name,age))
    
    print('hello %s,age is %s'%(name,age))
    

    2.5.split

    分割

    s = 'hello Tom,age is 18,myname is Tom'
    
    l = s.split(' ')  #以空格做分割
    print(l)
    
    l2 = s.split(',')  #以 逗号 做分割
    print(l2)
    
    
    ##输出结果
    ['hello', 'Tom,age', 'is', '18,myname', 'is', 'Tom']
    ['hello Tom', 'age is 18', 'myname is Tom']
    

    2.6.join

    拼接

    s = 'hello Tom,age is 18,myname is Tom'
    
    l = s.split(' ')  #以空格做分割
    print(l)
    
    
    t = tuple(l)
    s2 = ' '.join(t)    #字符串拼接
    print(s2)
    
    
    ##输出结果
    ['hello', 'Tom,age', 'is', '18,myname', 'is', 'Tom']
    hello Tom,age is 18,myname is Tom
    

    2. 7.count

    计数

    s = 'hello world'
    
    print(s.count('o'))   #2
    
    
    
    s = '123hello3333'
    
    print(s.count('3'))  #5
    

     2.8.center

    返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

    s = 'hello'
    s1 = 'K K'
    print(s.center(10,'*'))
    print(s1.center(3,'$'))
    
    #结果
    **hello***
    K K
    

    2.9.find

    find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

    语法:str.find(str, beg=0, end=len(string))

    str1 = "hell world"
    str2 = "r"
    
    print (str1.find(str2))
    print (str1.find(str2, 7))
    print (str1.find(str2, 10))
    
    #结果
    7
    7
    -1
    

    2.10.index

    ndex() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

    str1 = "hell world"
    str2 = "r"
    
    print (str1.index(str2))
    print (str1.index(str2, 7))
    print (str1.index(str2, 10))  #会报错
    
    #结果
    7
    7
    ValueError: substring not found    
    

    2.11.salnum

    检测字符串是否由  字母和数字  组成。

    返回值:如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False

    s = "hello123"      # 字符串没有空格
    s1 = "hello 123"    #有空格
    s2 = "hello-world"  #有特殊符号
    
    print(s.isalnum())
    print(s1.isalnum())
    print(s1.isalnum())
    
    #结果
    rue
    False
    False
    

    2.12.isalpha

    检测字符串是否只由字母组成。

    s = 'hello'
    s1 = 'hello--world'
    
    print(s.isalpha())
    print(s1.isalpha())
    
    #结果
    True
    False
    

    2.13.isdigit

    检测字符串是否只由数字组成

    s = '123'
    s1 = 'hello'
    
    print(s.isdigit())
    print(s1.isdigit())
    
    
    #结果
    True
    False
    

    2.14.islower

    检测字符串是否由小写字母组成

    s = 'hello'
    s1 = 'HELLO world'
    
    print(s.islower())
    print(s1.islower())
    
    
    # #结果
    True
    False
    

    2.15.isupper

    检测字符串中所有的字母是否都为大写

    s = 'HELLO'
    s1 = 'HELLO world'
    
    print(s.isupper())
    print(s1.isupper())
    
    
    # #结果
    True
    False
    

    2. 16.maketrans

    用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。

    注:两个字符串的长度必须相同,为一一对应的关系

    n = "acegi"
    m  = "12345"
    t = str.maketrans(n, m)
    
    str = "abcdefghij"
    print (str.translate(t))
    
    
    #结果
    1b2d3f4h5j
    

     2.17.lower(),upper()

    转换字符串中所有字符为 大小  写。

    s = 'HELLO'
    S1 = 'hello'
    
    print(s.lower())  #转小写
    print(s.upper())  #转大写
    
    #结果
    hello
    HELLO
    

     2.18.isdecimal

    检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。

    注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可

    s = "hello2017"
    s1  = "23443434"
    
    print(s.isdecimal())
    print(s1.isdecimal())
    
    #结果
    False
    True
    

     3.list 列表

    3.1 append,insert,extent

    append

    方法用于在列表末尾添加新的对象

    l = [1,2,3,'a','b','c']
    
    n = l.append(333)
    print(n,l)
    
    #结果
    None [1, 2, 3, 'a', 'b', 'c', 333]      
    View Code

    insert

    函数用于将指定对象插入列表的指定位置

    l = [1,2,3,'a','b','c']
    
    l.insert(2,'AAA')
    print(l)
    
    #结果
    [1, 2, 'AAA', 3, 'a', 'b', 'c']
    View Code

    extend

    用新列表扩展原来的列表

    l = [1,2,3,'a','b','c']
    
    #示范一
    # l.extend([5,6])
    # print(l)
    #
    # #结果
    # [1, 2, 3, 'a', 'b', 'c', 5, 6]            #5,6的位置
    
    
    #示范二
    l2 = [5,6]
    
    l2.extend(l)
    print(l2)
    
    #结果
    [5, 6, 1, 2, 3, 'a', 'b', 'c']            #5,6的位置
    View Code

    3.2 pop

    函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

    l = [1,2,3,'a','b','c']
    
    n = l.pop()    
    print(n)      #输出删除的 值
    
    print(l)
    
    
    #结果
    c
    [1, 2, 3, 'a', 'b']         # c 已删除
    View Code

    3.3 remove

    函数用于移除列表中某个值的第一个值

    l = [1,2,3,'a','b','c',3,3]
    
    n = l.remove(3)
    print(n,l)
    
    #结果
    None [1, 2, 'a', 'b', 'c', 3, 3]    #只会删除 第一个 3
    View Code

    3.4 clear

    函数用于清空列表

    l = [1,2,3,'a','b','c',3,3]
    
    l.clear()
    print('清空后的列表为:',l)
    
    #结果
    清空后的列表为: []
    
    
    
    # del  清空列表
    del l[:]
    print('使用del 的结果:',l)
    
    #结果
    使用del 的结果: []
    View Code

    3.5 count

    统计某一元素出现次数

    l = [1,2,'a','b','c','a','a']
    
    n = l.count('a')
    print(n)    # 3
    View Code

    3.6 index

    通过元素,找出其的 索引位置

    
    
    l = [1,2,'a','b','c']
    
    n = l.index('b')
    
    print(n)       #索引位置: 3
    View Code
     

    #拓展

    通过索引位置,找出 中间的元素

    #例一
    
    l = [1,2,'a','b','c',]
    
    if len(l) % 2 != 0:
        print(l[len(l)//2])
    
    else:
        print(l[len(l)//2 - 1],l[len(l)//2])
    
    #结果
    a
    
    
    #例二
    l = [1,2,'a','b','c','KKK']
    
    if len(l) % 2 != 0:
        print(l[len(l)//2])
    
    else:
        print(l[len(l)//2 - 1],l[len(l)//2])
        
    
    #结果
    a b
    View Code

    range

    #例一
    r = range(100)
    print(list(r))
    
    
    #例二
    r = range(50,100)
    print(list(r))
    
    
    #例二
    r = range(0,101,2)
    print(list(r))
    

    4.tuple 元组

    元组的元素不能修改

    4.1 索引,切片(查)

    t = (1,2,3,'a','b')
    print(t[2])
    print(t[3:5])
    
    #结果
    3
    ('a', 'b')
    
    t = (1,2,3,'a','b')
    
    print(t.index('a'))
    
    #结果
    3
    

     4.2len,count

    t = (1,2,3,'a','b',3,'c')
    
    print(len(t))
    print(t.count(3))
    
    #结果
    7
    2
    

    4.3元组合并

    t = (1,2,3)
    t1 = ('a','b','c')
    
    n_t = t+t1
    print(n_t)
    
    #结果
    (1, 2, 3, 'a', 'b', 'c')
    

    4.4 max,min,sum

    t = (1,20,2)
    print(max(t))
    print(min(t))
    print(sum(t))
    
    #结果
    
    20
    1
    23
    

     5.字典

    5.1 增

    d = {'name':'Tom','age':18,'place':'北京'}
    
    d['hobby'] = '打篮球'
    
    print(d)
    
    #结果
    {'name': 'Tom', 'age': 18, 'place': '北京', 'hobby': '打篮球'}
    View Code

    5.2删

    d = {'name':'Tom','age':18,'place':'北京'}
    
    di = d.pop('place')
    
    print(di)
    print(d)
    
    #结果
    北京
    {'name': 'Tom', 'age': 18}
    View Code

    5.3改

    d = {'name':'Tom','age':18,'place':'北京'}
    
    d['place'] = '上海'
    
    print(d)
    
    #结果
    {'name': 'Tom', 'age': 18, 'place': '上海'}
    View Code

    5.4查

    d = {'name':'Tom','age':18,'place':'北京'}
    
    print(d['age'])
    print(d['place'])
    
    #结果
    18
    北京
    View Code

    5.5 update

    将一个字典更新至另一个字典中

    d = {'name':'Tom','age':18,'place':'北京'}
    
    di = {'hobby':'打篮球','professional':'IT'}
    
    
    d.update(di)
    
    print(d)
    
    #结果
    {'name': 'Tom', 'age': 18, 'place': '北京', 'hobby': '打篮球', 'professional': 'IT'}
    View Code
    #范例
    d = {'    name':'Tom','age':18,'place':'北京','hobby':'打篮球'}
    
    'name'
    
    for k in d:
        if k.strip() == 'name':
            print(d[k])
    
    #结果
    Tom
    View Code
    
    
    

    set集合:

    s = {1,2}
    
    # s.add(33)
    s.update('asd')
    
    # 集合pop随机移除某个元素并且获取那个参数,集合pop没有参数
    # s.pop()
    # 移除元素 如果元素不存在,不会报错 remove 如果元素不存在,会报错
    # s.remove(1)
    s.discard(2)
    
    
    print(s)
    View Code

  • 相关阅读:
    Remove Element
    C++ 一些STL
    Two Pointers/hash/3Sum/4Sum类题目
    动态规划
    UVa 12657 双向链表
    并行运行环境
    多线程编程
    HTML XML CSS JS 迅速学习
    UVa 11988 数组模拟链表
    静态链表
  • 原文地址:https://www.cnblogs.com/zhaochangbo/p/6770289.html
Copyright © 2011-2022 走看看