python的数据对象

一.简介

1.在python中数据以对象的形式出现。从更具体的角度来说python可以分解成模块、语句、表达式以及对象。

2.四者之间的关系如下:(1)程序由模块构成(2)模块包含语句(3)语句包含表达式(4)表达式建立并处理对象。

3.python的核心数据类型:数字、字符串、列表、元组、字典、文件、集合、其它类型(bool、None)、编程单元类型(函数、模块、类)。

二python核心数据类型

1.数字

数字类型种类:

  • 整形——int
  • 长整形——long
  • 浮点型——float
  • 复数——cmoplex

python中的数字类型支持一般的数学运算

示例代码

  • 加号        >>> print 3+2        5
  • -(减法)>>> print 4-7     -3
  • “**”(幂运算)
  • *(乘法)>>> print 34*67    2278
  • /(除)   >>> print 60/2    30
  • %(取余数)>>> print 60%7   4
  • //(取整除)>>> print 60//9   6

查看字符串长度:使用    len() 函数

2.字符串

  • 字符串是任意字符的集合,他在python中是一种有序的序列。
  • 元组和列表也是一种序列。

序列的操作

内置的len()检验字符串的长度

示例代码
>>> a='uisduosduo'
>>> len(a)
10
>>> 'pppsippa'
'pppsippa'
>>> len('pppsippa')
8

字符串的索引格式为a[开始:结束]

>>> a[2]

's'

字符串的切片

>>> a[:]
'uisduosduo'


>>> a[:-1]
'uisduosdu'
>>> a[:-4]
'uisduo'
>>> a[-4:-1]
'sdu'
>>>

字符串的切片开始部分都不会取。
字符串可以进行加号运算

示例代码

>>> a='python'
>>> c=a+'hello'
>>> print(c)
pythonhello
>>> a='python'
>>> a+'xyt'
'pythonxyt'
>>> print(a)
python
>>>
虽然字符串a增加了字符,但是原始的a并没有增加元素。

特定的类型方法

  • a.find(对象)把字符串变成列表
  • upper()把字母变成大写的
  • replace
  • 其它函数...

占位符的使用

%s和{}的使用

示例代码

>>> '%s,egg,%s'%('hello','world!')
'hello,egg,world!'
>>> '{0},egg,{1}'.format('hello','world')
'hello,egg,world'
>>>

3.寻求帮助

使用函数dir(对象);返回的参数受对象的影响,但返回的都是一个列表,该列表包含函数属性和一些该对象自带的函数。

示例代码

>>> dir('split')
['__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']

help()函数

Help on built-in function replace:

replace(...)
    S.replace(old, new[, count]) -> string
   
    Return a copy of string S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

 

 

 

转载于:https://my.oschina.net/u/3754854/blog/1594023

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值