python数据类型

简介:

整型(int型,只保存整数)

常用函数及方法

可使用abs()函数取绝对值

abs(...)

    abs(number) -> number

    Return the absolute value of the argument.

例子:

a=-10

print (abs(a))

输出为10

可使用dir()函数查看该整型有哪些方法可以使用

dir(...)

    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.

    Else, return an alphabetized list of names comprising (some of) the attribut

es

    of the given object, and of attributes reachable from it.

    If the object supplies a method named __dir__, it will be used; otherwise

    the default dir() logic is used and returns:

      for a module object: the module's attributes.

      for a class object:  its attributes, and recursively the attributes

        of its bases.

      for any other object: its attributes, its class's attributes, and

        recursively the attributes of its class's base classes.

print (dir(a))


浮点型(float型,可以保存小数)

常用函数及方法

round函数

round(...)

    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).

    This always returns a floating point number.  Precision may be negative.

b=2.3333

print (round(b))

输出为2


布尔型

下面是python中布尔操作:
    x or y:if x is false,then y, else x
    x and y:if x is false, then x, else y
    not x:if x is false, then True, else False


python的字符串和常用方法

字符串是有下标的,常用方法

a='1234567'

print (a[0],a[5])

输出1 6


find()查找

a='1234567'

print (a.find('45'))

输出3 返回子字符串下标


join()插入

print ('99'.join('aaa'))

输出a99a99a


split()拆分

a='1234567'

print (a.split('45'))

输出['123', '67']列表


replace()替换

a='1234567'

print (a.replace('123','abc'))

输出abc4567


strip()去掉字符串前后空格

b='     a b c    '

print (b.strip())

输出 a   b   c


format()

print "{0} is {1} years old".format("FF", 99) 

print "{} is {} years old".format("FF", 99) 

print "Hi, {0}! {0} is {1} years old".format("FF", 99)

print "{name} is {age} years old".format(name = "FF", age = 99)




本文转自 粗粮面包 51CTO博客,原文链接:http://blog.51cto.com/culiangmianbao/1974767,如需转载请自行联系原作者

相关文章
|
1月前
|
存储 开发者 索引
python怎么判断变量的数据类型
python怎么判断变量的数据类型
29 0
|
22天前
|
存储 索引 Python
python数据类型
【4月更文挑战第1天】,Python有数字(整型、浮点型、复数)、布尔、字符串等基本类型,及列表、元组、字典、集合等复合类型。列表是可变有序集合,元组是不可变有序集合,字典是键值对无序集合,集合是无序唯一元素集合。还有特殊类型NoneType,仅包含值None。
25 1
python数据类型
|
7天前
|
Python
02-python的基础语法-01python字面量/注释/数据类型/数据类型转换
02-python的基础语法-01python字面量/注释/数据类型/数据类型转换
|
14天前
|
索引 Python 容器
python 数据类型之列表
python 数据类型之列表
|
16天前
|
索引 Python
Python标准数据类型-List(列表)
Python标准数据类型-List(列表)
42 1
|
16天前
|
Java 索引 Python
Python标准数据类型-字符串常用方法(下)
Python标准数据类型-字符串常用方法(下)
21 1
|
16天前
|
XML 编解码 数据格式
Python标准数据类型-String(字符串)
Python标准数据类型-String(字符串)
23 2
|
16天前
|
Python
Python数据类型学习应用案例详解
Python基础数据类型包括整数(int)、浮点数(float)、字符串(str)、布尔值(bool)、列表(list)、元组(tuple)、字典(dict)和集合(set)。整数和浮点数支持算术运算,字符串是不可变的文本,布尔值用于逻辑判断。列表是可变有序集合,元组不可变。字典是键值对的无序集合,可变,而集合是唯一元素的无序集合,同样可变。示例代码展示了这些类型的基本操作。
11 1
|
1月前
|
存储 Python
Python变量与数据类型探秘
本文介绍了Python编程中的核心概念——变量和数据类型。变量是存储数据的标识符,无需预声明类型,类型由赋值自动确定。命名规则要求变量名具有描述性,以字母或下划线开头,区分大小写。Python支持多种数据类型:数值(整数、浮点数、复数)、字符串、列表、元组、字典和集合。理解这些基本概念和类型特性对编写高效Python代码至关重要。
|
1月前
|
存储 程序员 C++
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
50 0