美文网首页
python的基本数据类型_int

python的基本数据类型_int

作者: 闲云野鹤_23dd | 来源:发表于2020-12-27 20:06 被阅读0次

python的基本数据类型

基本数据类型 也称作 基本变量类型, python 共有 9 种 基本数据类型,分别是:

  • 整数型 : int
  • 浮点型(小数型): float
  • 复数 : complex
  • 字符串: str
  • 布尔型: bool
  • 列表类型 : list
  • 元组类型 : tuple
  • 字典类型 : dict
  • 空类型: None

基本变量类型 将分为 5 天来学习.
在学习基本变量类型之前呢 , 先了解一下type函数

type函数

type 函数可以返回 变量的类型
语法:

type(变量名)


<mark style="box-sizing: border-box; background-color: rgb(255, 255, 0); color: rgb(0, 0, 0);">今天来学习数字相关的三种类型</mark>

整数

# def: 声明方法的关键字 ;int_demo: 方法的名字; (): 下面写方法的内容
def int_demo():
# 声明一个变量 =前面是变量名 aint, = 后面是变量值,int类型的5
    aint = 5
    # type(aint) : 获取 aint的数据类型(变量类型), print: 打印出他的类型
   print(type(aint))` 

小数

def float_demo():
    afloat = 1.1
    print(type(afloat))
    print(a)` 

复数

def complex_demo():
    # 复数类型 complex   实数 虚数
    acomplex = 1 + 2j
    print(acomplex)
    print(type(acomplex))` 

就是数学上的复数, 加号前面是 实数, 后面是虚数,虚数后面要带 j

类型转换

将其他类型转换为 int 类型

使用int()函数,如果是小数则会被向下取整,不管小数位,只保留整数位

int(值)

将其他类型转换为 float类型

使用float()函数

float(值)

小数位四舍五入

使用round()函数

round( x [, n]  )

参数

  • x – 要四舍五入的数值。
  • n – 保留几位小数。默认为0

例子:

def round_demo():
    print('round(8.655):',round(8.655))
    print('round(1.356,2):',round(1.356,2))
    print('round(1.8887,3):', round(1.8887,3))

缺点:

由于 计算机 是二进制的,在有些数字下 会有误差: 比如:

round(1.115,2)

这不是bug,而是一种常见的舍入法,名称是“银行家式舍入法”,
用意是一半舍一半入,如果碰到0.5全入,那么银行觉得自己亏了,
银行希望和用户要风险对半。不光Python,其他的计算机语言都是这个方法
python数字相关的数据类型是哪几个?
整数型 : int
浮点型(小数型): float
复数 : complex
变量转换为整数类型用哪个函数?int()
小数: 4.66转换为 整数后 是多少? 4
写如下代码: 将 小数 1.336 保留两位小数 四舍五入后 打印出来 1.34
如果代码基础薄弱,请将课件中的例子代码,手写一遍

相关文章

  • Python 基本语法

    1Python基础 1.1 Python七种基本数据类型: 1.2 数据类型转化: int(),float(),c...

  • 「Python」Python 01 基础语法

    ① Python 基本数据类型 Number「int、float、bool、complex」 String(字符串...

  • Python基础语法

    1. Python3 基本数据类型 Python3 中的6个标准数据类型: Number Python3支持int...

  • Python基本数据类型

    Python基本数据类型 在了解基本数据类型的时候,我们需要了解基本数据类型有哪些?数字int、布尔值bool、字...

  • Python学习笔记三(容器)

    基本数据类型 int Python3中的整数只有int,没有long类型,int最大值如下, float Pyth...

  • 02序列类型自带方法笔记

    python 基础,序列类型自带方法 基本数据类型 int float bool complex +、-、 *、 ...

  • python day2

    Day 2:Python 四大数据类型总结基本数据类型数值型Python 中的数据皆是对象,比如被熟知的 int ...

  • 初识python - 2

    初识python基本数据类型 数字 int(整型) long(长整型) -- python3 中已经将类型取消 f...

  • 第四章 Python3_基本数据类型

    一,七种基本数据类型 1,整型 -- int 进制在 Python 中的表示形式11 ...

  • 不可不知的python模块: collections

    基本介绍 Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, co...

网友评论

      本文标题:python的基本数据类型_int

      本文链接:https://www.haomeiwen.com/subject/poyqgktx.html