zoukankan      html  css  js  c++  java
  • Python 数据类型-1

    数据类型相关函数

    type() 查看数据类型

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time   : 2017/10/17 21:46
    # @Author : lijunjiang
    # @File   : test.py
    
    # type() 查看数据类型
    
    name = raw_input("Please input your name: ")
    print(name)
    print(type(name))
    
    执行结果:
    
    C:Python27python.exe D:/Python/type_of-data.py
    Please input your name: lijunjiang
    lijunjiang
    <type 'str'>
    
    Process finished with exit code 0
    

    raw_input() 接收字符串和非字符串输入,类型均为字符串

    name = raw_input("Please input your name: ")
    print(name)
    print(type(name))
    
    age = raw_input("Please input your age: ")
    print(age)
    print(type(age))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    Please input your name: lijunjiang
    lijunjiang
    <type 'str'>
    Please input your age: 18
    18
    <type 'str'>
    
    Process finished with exit code 0
    

    input () 只接收整型

    age = input("Please input your age: ")
    print(age)
    print(type(age))
    name = input("Please input your name: ")
    print(name)
    print(type(name))
    
    执行结果:
     C:Python27python.exe D:/Python/type_of-data.py
    Please input your age: 18
    18
    <type 'int'>
    Please input your name: lijunjiang
    Traceback (most recent call last):
      File "D:/Python/type_of-data.py", line 26, in <module>
        name = input("Please input your name: ")
      File "<string>", line 1, in <module>
    NameError: name 'lijunjiang' is not defined
    
    Process finished with exit code 1
    

    abs() 或 __abs__ 取绝对值

    a = 100
    b = -30
    print(a)
    print(b)
    print(a - b)
    print(a.__abs__() + b.__abs__())
    print(abs(a) + abs(b))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    100
    -30
    130
    130
    130
    
    Process finished with exit code 0
    

    dir() 列出一个定义对象的标识符

    当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的名字的列表。

    a = 18
    b = "lijunjiang"
    print(dir())
    print(dir(a))
    print(dir(b))
    
    执行结果:
    
    C:Python27python.exe D:/Python/type_of-data.py
    
    #当前模块定义的名子和列表
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b']
    
    #整型 方法
    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
    
    #字符串 方法
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    
    Process finished with exit code 0
    
    

    整型(int)

    即整数

    a = 100
    b = 20
    print(type(a))
    print(type(b))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    <type 'int'>
    <type 'int'>
    
    Process finished with exit code 0
    

    浮点型(float)

    即小数 在python中整型后加小数点可定义浮点型数据

    a = 10
    b = 10.0
    print(type(a))
    print(type(b))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    <type 'int'>
    <type 'float'>
    
    Process finished with exit code 0
    
    

    round(n[, x]) 返回浮点数n四舍五入值,x保留位数,默认为0 ,大于0对小数部分四舍五入,小于0对整数部分四舍五入,返回结果为浮点数

    a = 10
    b = 11.455
    c = 15.345
    print(round(a))
    print(round(a, 1))
    print(round(a, 2))
    print(round(b))
    print(round(b, 1))
    print(round(b, 2))
    print(round(c))
    print(round(c, 1))
    print(round(c, 2))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    10.0
    10.0
    10.0
    11.0
    11.5
    11.46
    15.0
    15.3
    15.35
    
    Process finished with exit code 0
    

    round 负数( 四舍五入是围绕着0来计算的)

    print(round(0.4))
    print(round(-0.4))
    print(round(0.5))
    print(round(-0.5))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    0.0
    -0.0
    1.0
    -1.0
    
    Process finished with exit code 0
    
    

    round 的陷阱

    print(round(1.675, 2))
    print(round(2.675, 2))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    1.68
    2.67
    
    Process finished with exit code 0
    
    

    布尔型()

    就两值 真(True)或假(False)

    一般用作一个判断的返回值

    print(not True)
    print(not False)
    
    a = 10
    b = 20
    c = 50
    print(a > b  and c > a)
    print(not(a > b  and c > a))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    False
    True
    False
    True
    
    Process finished with exit code 0
    

    字符串(str)

    定义:使用 ''、 ""、""""""、声明一个字符串,phthon默认使用单引号

    a = 'aaaaa'
    b = "bbbbb"
    c = """ccccc"""
    print(a)
    print('a %s' % type(a))
    print(b)
    print('b %s' % type(b))
    print(c)
    print('c %s' % type(c))
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    aaaaa
    a <type 'str'>
    bbbbb
    b <type 'str'>
    ccccc
    c <type 'str'>
    
    Process finished with exit code 0
    

    字符串常用方法

    0、''''''

    常用于多行注释,起解释作用

    1、下标

    a = 'abcde'
    print(a[0], a[1], a[2], a[3], a[4])
    
    执行结果:
    C:Python27python.exe D:/Python/type_of-data.py
    ('a', 'b', 'c', 'd', 'e')
    
    Process finished with exit code 0
    

    2、find() 在字符串中查找字符串,返回查找字符串中第一个字符的下标,未找到返回-1

    a = 'aabbcc'
    print(a.find('b'))
    print(a.find('e'))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    2
    -1
    
    Process finished with exit code 0
    
    

    3、replace(str1,str2) 替换 将str1替换为str2

    a = 'aabbcbccc'
    print(a.replace('bb', 'ff'))
    print(a.replace('bc','qq'))
    print(a.replace('c', 'RR'))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    aaffcbccc
    aabqqqqcc
    aabbRRbRRRRRR
    
    Process finished with exit code 0
    

    4、split(srt) 分割 以str 为分割符,返回一个列表 类似shell中awk -F

    a = 'aaTbbTccTbb'
    print(a.split('T'))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    ['aa', 'bb', 'cc', 'bb']
    
    Process finished with exit code 0
    
    

    5、join(str) 以str连接字符串

    a = 'aaTbbTccTbb'
    print('||'.join(a.split('T')))
    print(' '.join(a.split('T')))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    aa||bb||cc||bb
    aa bb cc bb
    
    Process finished with exit code 0
    
    等价于:
    a = 'aaTbbTccTbb'
    a = a.split('T')
    print(a)
    print("||".join(a))
    print(' '.join(a))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    ['aa', 'bb', 'cc', 'bb']
    aa||bb||cc||bb
    aa bb cc bb
    
    Process finished with exit code 0
    
    

    5、strip() 去除字符串两边的空格

    lstrip() 只去除左边空格 rstrip() 只去除右边空格

    a = ' aa bb cc  '
    print(a)
    print(a.strip())
    print(a.lstrip())
    print(a.rstrip())
    b = '     aa   bb   cc    '
    print(b)
    print(b.strip())
    print(b.lstrip())
    print(b.rstrip())
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
     aa bb cc  
    aa bb cc
    aa bb cc  
     aa bb cc
         aa   bb   cc    
    aa   bb   cc
    aa   bb   cc    
         aa   bb   cc
    
    Process finished with exit code 0
    
    

    6、format

    print('str0 {0} str1{1}'.format(str0, str1)) 执行效率最高

    a = 'python'
    b = 'Hi, '
    print('hello' + a)
    print('hello %s') % a
    print('hello %{0}'.format(a))
    print('hello {0}'.format(a))
    print('hello {}'.format(a))
    print('hello {a}'.format(a='python'))
    print('###########' * 20)
    
    print(b + 'hello ' + a)
    print('%s hello %s') % (b, a)
    print('%{0} hello %{1}'.format(b, a))
    print('{0} hello {1}'.format(b, a))
    print('{} hello {}'.format(b, a))
    print('{a} hello {b}'.format(a='Hi,', b='python'))
    
    执行:
    C:Python27python.exe D:/Python/type_of-data.py
    hellopython
    hello python
    hello %python
    hello python
    hello python
    hello python
    ############################################################################################################################################################################################################################
    Hi, hello python
    Hi,  hello python
    %Hi,  hello %python
    Hi,  hello python
    Hi,  hello python
    Hi, hello python
    
    Process finished with exit code 0
    
    
  • 相关阅读:
    JMeter实现登录初始化(类似LR的init函数功能实现)
    修改ini文件的批处理
    pycharm-professional-2017.1.1.exe专业版激活方法
    loadrunner下的putty和plink
    Centos下安装LoadRunner负载机
    VMware安装操作系统提示 " Intel VT-x 处于禁用状态"解决方法
    mongodb中投票节点作用
    crunch创建自己的密码字典文件
    Nessus忘记密码的解决
    shell判断文件是否为空
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7704069.html
Copyright © 2011-2022 走看看