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,如需转载请自行联系原作者

相关文章
|
2月前
|
人工智能 Python
python基本数据类型简介
本文简要介绍了Python的基本数据类型,包括整型、浮点型、字符串、列表、字典和布尔类型,帮助读者对Python数据类型有初步了解。
|
2月前
|
存储 安全 开发者
Python中的数据类型详解
Python是一种动态类型编程语言,具备丰富的数据类型,包括数值类型、序列类型、映射类型和集合类型等。这些类型为高效编程提供了强大支持。
|
4月前
|
Python
Python技术解析:了解数字类型及数据类型转换的方法。
在Python的世界里,数字并不只是简单的数学符号,他们更多的是一种生动有趣的语言,用来表达我们的思维和创意。希望你从这个小小的讲解中学到了有趣的内容,用Python的魔法揭示数字的奥秘。
114 26
|
5月前
|
存储 程序员 Python
Python 变量和简单数据类型
本文介绍了 Python 编程的基础知识,从创建第一个 Python 文件 `hello_world.py` 开始,讲解了 Python 文件的运行机制及解释器的作用。接着深入探讨了变量的定义、命名规则和使用方法,并通过示例说明如何修改变量值。同时,文章详细解析了字符串的操作,包括大小写转换、变量插入及空白字符处理等技巧。此外,还涵盖了数字运算(整数与浮点数)、常量定义以及注释的使用。最后引用了《Python 之禅》,强调代码设计的美学原则和哲学思想。适合初学者快速掌握 Python 基础语法和编程理念。
|
5月前
|
Python
探索Python的各式数据类型
以上就是Python数据类型的一次简单而有趣的游览。和她继续接触,你会发现她还有更多有趣的面象,例如集合里的冰冻集合(Frozenset),序列里的字符串(String)和字节序列(Bytes)等等。希望这次游览能对你有所启发,让你更好地理解和使用Python。
78 21
|
10月前
|
Python
Python中不同数据类型之间如何进行转换?
Python中不同数据类型之间如何进行转换?
242 62
|
10月前
|
存储 开发者 Python
Python 的数据类型
Python 的数据类型
187 61
|
9月前
|
Python
Python 中一些常见的数据类型
Python 中一些常见的数据类型
477 8
|
11月前
|
Python
【10月更文挑战第7天】「Mac上学Python 13」基础篇7 - 数据类型转换与NoneType详解
本篇将详细介绍Python中的常见数据类型转换方法以及 `NoneType` 的概念。包括如何在整数、浮点数、字符串等不同数据类型之间进行转换,并展示如何使用 `None` 进行初始赋值和处理特殊情况。通过本篇的学习,用户将深入理解如何处理不同类型的数据,并能够在代码中灵活使用 `None` 处理未赋值状态。
152 2
【10月更文挑战第7天】「Mac上学Python 13」基础篇7 - 数据类型转换与NoneType详解
|
11月前
|
编译器 数据安全/隐私保护 Python
Python--基本数据类型
【10月更文挑战第4天】
132 5

热门文章

最新文章

推荐镜像

更多