zoukankan      html  css  js  c++  java
  • 8.pyhton内置数据--数据类型

    数值型:int、float、complex、bool

    • int、foat、 complex、bool都是 class,1、5.0、2+3j都是对象即实例
    • int:python3的int就是长整型,目没有大小限制受限于内存区域的大小
    • float:有整数部分和小数部分组成。支持进制和料学计数法表示。只有双精度型
    • comlex:有实数和虚数部分组成,实数和虚数部分都是浮点数,3+4.2j
    • bool:int的子类,仅有2个实例True、 False对应1和0,可以和整数直接运算

    类型转换:int(x)返回一个整数;float(x)返回一个浮点数;comlex(x)、 complex(xy)返回一个复数;bool(X)返回布尔值,前面讲过 False等价的对象。

    与数值相关的某些函数:

     1 >>> int(1.5)
     2 1
     3 >>>3//2
     4 1
     5 # int()和//一样,都是只取整数部分
     6 
     7 >>> import math
     8 >>> math.floor(1.7)
     9 1
    10 >>> math.ceil(1.3)
    11 2
    12 # floor是往下取整,ceil是往上取整,这里要使用math模块
    13 
    14 >>> round(1.5)
    15 2
    16 >>> round(4.5)
    17 4
    18 >>> round(4.4)
    19 4
    20 >>> round(4.6)
    21 5
    22 # round()是4舍6入5取偶
    23 
    24 >>> min(1,5),max(1,5)
    25 (1, 5)
    26 >>> min([1,4,2]),max([1,4,2])
    27 (1, 4)
    28 # 最大最小值的比较
    29 
    30 >>> pow(5,7)
    31 78125
    32 >>> pow(49,0.5)
    33 7.0
    34 # pow(x,y)等同于x**y,x的y次方
    35 
    36 >>> bin(10),hex(10),oct(10)
    37 ('0b1010', '0xa', '0o12')
    38 # 进制函数,返回值是字符串
    39 # bin()是十进制,hex()是十六进制,oct()是十二进制
    40 
    41 >>> math.pi
    42 3.141592653589793
    43 # π的取值
    44 
    45 >>> math.e
    46 2.718281828459045
    47 # 自如常数

    类型判断的某些函数:

     1 >>> a = 10
     2 >>> b = 'abc'
     3 >>> type(a)
     4 <class 'int'>
     5 >>> type(b)
     6 <class 'str'>
     7 # type()是判定类型的函数,返回的是类型而不是字符串
     8 >>> type(type(a))
     9 <class 'type'>
    10 # 这里等同于type(int),结果显示int是个type类型
    11 >>> type(a) == str
    12 False
    13 >>> type(a) == int
    14 True
    15 # 这里右边的str和int并没有加引号,并不表示字符串,而是表示是什么类型
    16 >>> isinstance(a,int)
    17 True
    18 >>> isinstance(a,str)
    19 False
    20 >>> isinstance(a,(int,str))
    21 True
    22 # 在前面定义了a为数字10,isinstance(obj,class_or_tuple)中obj是变量或者常量或者字符串等等,后面跟一个类型或者一些类型,只要匹配一个,返回结果即为True
    23 >>> 1+True
    24 2
    25 >>> type(1+True)
    26 <class 'int'>
    27 # True和Flase都是bool型,而bool是int类型的一种。
    28 >>> 1+True+0.5
    29 2.5
    30 >>> type(1+True+0.5)
    31 <class 'float'>
    32 # 整数型+浮点型会隐式转换成浮点型

    序列对象:字符串str、列表list、tuple

    键值对:集合set、字典dict

  • 相关阅读:
    学习使用资源文件[4] 用资源中的图片做背景、使用 LoadFromResourceID
    WinAPI: ShellExecute 打开外部程序或文件
    学习使用资源文件[8] 关于 HInstance
    学习使用资源文件[3] 用 Image 显示资源中的图片
    薛定谔之猫_百度百科
    美国创业公司招聘工程师
    Two star programming
    vector 自定义排序
    Data Structures, Algorithms, & Applications in Java Suffix Trees
    Three Star Programmer
  • 原文地址:https://www.cnblogs.com/linfengs/p/11662485.html
Copyright © 2011-2022 走看看