博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 数据类型-1
阅读量:6686 次
发布时间:2019-06-25

本文共 7845 字,大约阅读时间需要 26 分钟。

数据类型相关函数

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:\Python27\python.exe D:/Python/type_of-data.pyPlease input your name: lijunjianglijunjiang
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:\Python27\python.exe D:/Python/type_of-data.pyPlease input your name: lijunjianglijunjiang
Please input your age: 1818
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:\Python27\python.exe D:/Python/type_of-data.pyPlease input your age: 1818
Please input your name: lijunjiangTraceback (most recent call last): File "D:/Python/type_of-data.py", line 26, in
name = input("Please input your name: ") File "
", line 1, in
NameError: name 'lijunjiang' is not definedProcess finished with exit code 1

abs() 或 __abs__ 取绝对值

a = 100b = -30print(a)print(b)print(a - b)print(a.__abs__() + b.__abs__())print(abs(a) + abs(b))执行结果:C:\Python27\python.exe D:/Python/type_of-data.py100-30130130130Process finished with exit code 0

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

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

a = 18b = "lijunjiang"print(dir())print(dir(a))print(dir(b))执行结果:C:\Python27\python.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 = 100b = 20print(type(a))print(type(b))执行结果:C:\Python27\python.exe D:/Python/type_of-data.py
Process finished with exit code 0

浮点型(float)

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

a = 10b = 10.0print(type(a))print(type(b))执行结果:C:\Python27\python.exe D:/Python/type_of-data.py
Process finished with exit code 0

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

a = 10b = 11.455c = 15.345print(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:\Python27\python.exe D:/Python/type_of-data.py10.010.010.011.011.511.4615.015.315.35Process finished with exit code 0

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

print(round(0.4))print(round(-0.4))print(round(0.5))print(round(-0.5))执行结果:C:\Python27\python.exe D:/Python/type_of-data.py0.0-0.01.0-1.0Process finished with exit code 0

round 的陷阱

print(round(1.675, 2))print(round(2.675, 2))执行结果:C:\Python27\python.exe D:/Python/type_of-data.py1.682.67Process finished with exit code 0

布尔型()

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

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

print(not True)print(not False)a = 10b = 20c = 50print(a > b  and c > a)print(not(a > b  and c > a))执行结果:C:\Python27\python.exe D:/Python/type_of-data.pyFalseTrueFalseTrueProcess 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:\Python27\python.exe D:/Python/type_of-data.pyaaaaaa 
bbbbbb
cccccc
Process finished with exit code 0

字符串常用方法

0、''''''

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

1、下标

a = 'abcde'print(a[0], a[1], a[2], a[3], a[4])执行结果:C:\Python27\python.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:\Python27\python.exe D:/Python/type_of-data.py2-1Process 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:\Python27\python.exe D:/Python/type_of-data.pyaaffcbcccaabqqqqccaabbRRbRRRRRRProcess finished with exit code 0

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

a = 'aaTbbTccTbb'print(a.split('T'))执行:C:\Python27\python.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:\Python27\python.exe D:/Python/type_of-data.pyaa||bb||cc||bbaa bb cc bbProcess finished with exit code 0等价于:a = 'aaTbbTccTbb'a = a.split('T')print(a)print("||".join(a))print(' '.join(a))执行:C:\Python27\python.exe D:/Python/type_of-data.py['aa', 'bb', 'cc', 'bb']aa||bb||cc||bbaa bb cc bbProcess 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:\Python27\python.exe D:/Python/type_of-data.py aa bb cc  aa bb ccaa bb cc   aa bb cc     aa   bb   cc    aa   bb   ccaa   bb   cc         aa   bb   ccProcess 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') % aprint('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:\Python27\python.exe D:/Python/type_of-data.pyhellopythonhello pythonhello %pythonhello pythonhello pythonhello python############################################################################################################################################################################################################################Hi, hello pythonHi,  hello python%Hi,  hello %pythonHi,  hello pythonHi,  hello pythonHi, hello pythonProcess finished with exit code 0

转载于:https://www.cnblogs.com/lijunjiang2015/p/7704069.html

你可能感兴趣的文章
C#窗体的加载等待(BackgroundWorker控件)实现
查看>>
Windows Server 2016
查看>>
为什么说invalidate()不能直接在线程中调用
查看>>
Centos7中systemctl命令详解
查看>>
windows使用git时出现:warning: LF will be replaced by CRLF
查看>>
QuickStart OpenvirteX
查看>>
spring- properties 读取的五种方式
查看>>
各种加载(转载)
查看>>
Python Scrapy 自动爬虫注意细节(3)
查看>>
在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
查看>>
3 django系列之Form表单在前端web界面渲染与入库保存
查看>>
Shell脚本与vi编辑器:vi启动与退出、工作模式、命令大全
查看>>
linux设备驱动归纳总结(六):1.中断的实现【转】
查看>>
可重入函数与不可重入函数【转】
查看>>
js yield
查看>>
Docker 传奇之 dotCloud
查看>>
迅雷下载精简版
查看>>
ElasticSearch 基础<转载>
查看>>
如何使用SVN协调代源代码,多人同步开发
查看>>
shell脚本练习【转】
查看>>