python数据类型

Python Data Types are used to define the type of a variable. Previously we learned about statement and comment in Python. If you want then you can find it from Python Comment & Statement.

Python数据类型用于定义变量的类型。 之前,我们了解了Python中的语句和注释。 如果需要,可以从Python Comment&Statement中找到它。

Python数据类型 (Python Data Types)

There are different types of python data types. Some built-in python data types are:

有不同类型的python数据类型。 一些内置的python数据类型是:

  1. Python数据类型–数值 (Python Data Type – Numeric)

    Python numeric data type is used to hold numeric values like;

    1. int – holds signed integers of non-limited length.
    2. long- holds long integers(exists in Python 2.x, deprecated in Python 3.x).
    3. float- holds floating precision numbers and it’s accurate upto 15 decimal places.
    4. complex- holds complex numbers.

    In Python we need not to declare datatype while declaring a variable like C or C++. We can simply just assign values in a variable. But if we want to see what type of numerical value is it holding right now, we can use type(), like this:

    #create a variable with integer value.
    a=100
    print("The type of variable having value", a, " is ", type(a))#create a variable with float value.
    b=10.2345
    print("The type of variable having value", b, " is ", type(b))#create a variable with complex value.
    c=100+3j
    print("The type of variable having value", c, " is ", type(c))

    If you run the above code you will see output like the below image.

    Python数字数据类型用于保存数字值,例如;

    1. int –保留无限制长度的有符号整数。
    2. long-保持长整数(在Python 2.x中存在,在Python 3.x中已弃用)。
    3. float-保留浮点精度数字,它的精度最高为15个小数位。
    4. 复数-包含复数。

    在Python中,我们无需在声明变量(例如C或C ++)时声明数据类型。 我们可以简单地在变量中赋值。 但是,如果我们想看看它现在持有什么类型的数值,可以使用type() ,如下所示:

    #create a variable with integer value.
    a=100
    print("The type of variable having value", a, " is ", type(a))#create a variable with float value.
    b=10.2345
    print("The type of variable having value", b, " is ", type(b))#create a variable with complex value.
    c=100+3j
    print("The type of variable having value", c, " is ", type(c))

    如果运行上面的代码,您将看到如下图所示的输出。

  2. Python数据类型–字符串 (Python Data Type – String)

    The string is a sequence of characters. Python supports Unicode characters. Generally, strings are represented by either single or double quotes.

    a = "string in a double quote"
    b= 'string in a single quote'
    print(a)
    print(b)# using ',' to concatenate the two or several strings
    print(a,"concatenated with",b)#using '+' to concate the two or several strings
    print(a+" concated with "+b)

    The above code produce output like below picture-

    字符串是一个字符序列。 Python支持Unicode字符。 通常,字符串用单引号或双引号表示。

    a = "string in a double quote"
    b= 'string in a single quote'
    print(a)
    print(b)# using ',' to concatenate the two or several strings
    print(a,"concatenated with",b)#using '+' to concate the two or several strings
    print(a+" concated with "+b)

    上面的代码产生如下图所示的输出-

  3. Python数据类型–列表 (Python Data Type – List)

    The list is a versatile data type exclusive in Python. In a sense, it is the same as the array in C/C++. But the interesting thing about the list in Python is it can simultaneously hold different types of data. Formally list is an ordered sequence of some data written using square brackets([]) and commas(,).

    #list of having only integers
    a= [1,2,3,4,5,6]
    print(a)#list of having only strings
    b=["hello","john","reese"]
    print(b)#list of having both integers and strings
    c= ["hey","you",1,2,3,"go"]
    print(c)#index are 0 based. this will print a single character
    print(c[1]) #this will print "you" in list c

    The above code will produce output like this-

    该列表是Python独有的通用数据类型。 从某种意义上说,它与C / C ++中的数组相同。 但是Python列表的有趣之处在于它可以同时保存不同类型的数据。 正式列表是使用方括号([])和逗号(,)编写的一些数据的有序序列。

    #list of having only integers
    a= [1,2,3,4,5,6]
    print(a)#list of having only strings
    b=["hello","john","reese"]
    print(b)#list of having both integers and strings
    c= ["hey","you",1,2,3,"go"]
    print(c)#index are 0 based. this will print a single character
    print(c[1]) #this will print "you" in list c

    上面的代码将产生如下输出:

  4. Python数据类型–元组 (Python Data Type – Tuple)

    Tuple is another data type which is a sequence of data similar to list. But it is immutable. That means data in a tuple is write protected. Data in a tuple is written using parenthesis and commas.

    #tuple having only integer type of data.
    a=(1,2,3,4)
    print(a) #prints the whole tuple#tuple having multiple type of data.
    b=("hello", 1,2,3,"go")
    print(b) #prints the whole tuple#index of tuples are also 0 based.print(b[4]) #this prints a single element in a tuple, in this case "go"

    The output of this above python data type tuple example code will be like below image.

    元组是另一种数据类型,是类似于列表的一系列数据。 但这是一成不变的。 这意味着元组中的数据受到写保护。 元组中的数据使用括号和逗号写入。

    #tuple having only integer type of data.
    a=(1,2,3,4)
    print(a) #prints the whole tuple#tuple having multiple type of data.
    b=("hello", 1,2,3,"go")
    print(b) #prints the whole tuple#index of tuples are also 0 based.print(b[4]) #this prints a single element in a tuple, in this case "go"

    上面的python数据类型元组示例代码的输出将如下图所示。

  5. 字典 (Dictionary)

    Python Dictionary is an unordered sequence of data of key-value pair form. It is similar to the hash table type. Dictionaries are written within curly braces in the form key:value. It is very useful to retrieve data in an optimized way among a large amount of data.

    #a sample dictionary variablea = {1:"first name",2:"last name", "age":33}#print value having key=1
    print(a[1])
    #print value having key=2
    print(a[2])
    #print value having key="age"
    print(a["age"])

    If you run this python dictionary data type example code, output will be like below image.

    Python字典是键值对形式的无序数据序列。 它类似于哈希表类型。 字典以大括号内的形式写成key:value 。 以最佳方式从大量数据中检索数据非常有用。

    #a sample dictionary variablea = {1:"first name",2:"last name", "age":33}#print value having key=1
    print(a[1])
    #print value having key=2
    print(a[2])
    #print value having key="age"
    print(a["age"])

    如果运行此python字典数据类型示例代码,则输出将如下图所示。

So that’s all for today about python data types. Don’t forget to run every piece of code in your own machine. Also don’t just copy-paste. Try to write the lines of code on your own.
#happy_coding

python数据类型_Python数据类型相关推荐

  1. python获取数据类型_python数据类型详解

    python数据类型:int.string.float.boolean 可变变量:list 不可变变量:string.元组tuple 1.list list就是列表.array.数组 列表根据下标(0 ...

  2. python序列数据类型_python 数据类型 序列——列表

    python 数据类型 序列--列表 浏览次数: 发布时间:2015-08-21 11:38 python 数据类型 序列--列表 **列表** list是处理一组有序项目的数据结构,即你可以在一个列 ...

  3. 写出python中的六种数据类型_python 数据类型1

    一.字典 一组键(key)和值(value)的组合,通过键(key)进行查找,没有顺序, 使用大括号"{}"; 1.1 现有字典 d={'a':24,'g':52,'i':12,' ...

  4. 实数是不是python数据类型_Python数据类型之数字(Numbers)和运算符

    # Numbers(数字)类型分类 # 1.整数 int # 2.浮点数 float # 3.复数 complex # 整型:通常被称为整数,可以是正整数或负整数,不携带小数点:Python3中整型是 ...

  5. python如何强制转换数据类型_python数据类型强制转换实例详解

    如果是字符串进行强制转换, 仅仅就是在原数据类型的两边套上引号 2.list : 强制转换成列表""" 如果是字符串,会把每一个字符都单独作为一个元素放到新的列表中 如果 ...

  6. python 分类_Python数据类型分类

    数据类型是每种编程语言必备属性,只有给数据赋予明确的数据类型,计算机才能对数据进行处理运算,因此,正确使用数据类型是十分必要的,不同的语言,数据类型类似,但具体表示方法有所不同,以下是Python编程 ...

  7. python中包含的标准数据类型_Python数据类型基础

    1. Python标准数据类型 Python3 中有六个 标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字 ...

  8. python中集合的元素可以是任意数据类型_Python数据类型之列表

    列表 列表是由一系列按特定循序排列的元素组成(即有序集合).使用中括号[]来表示,并用逗号来分隔其中的元素. 列表的元素可以是任意类型. list 列表(序列)中的每个元素都分配一个数字索引,从 0 ...

  9. python的内置字典数据类型_python 数据类型元组与字典内置方法

    1.元组 (1)元组是不可变的列表,能存多个值:如果多个值只有取得需求,没有改的需求,用元组最合理 (2)定义:在()内用逗号隔开,可以存任意类型的值 注意:当元组只有一个元素时,要在后面加逗号 # ...

最新文章

  1. SBB:pH主导土壤中固氮群落的共存与装配
  2. 如何把字符串类型转换成整型?
  3. C#2.0泛型-Dictionary,List的用法
  4. 从本地上传项目到 github 以及从github 下载项目到本地环境
  5. css 浮动问题 display显示 和 光标设置cursor
  6. visual studio 2013 编译DCMTK3.6.3
  7. Java Language Changes for Java SE 9
  8. 面试精讲之面试考点及大厂真题 - 分布式专栏 08 Redis中有哪些数据结构及底层实现原理
  9. comsol和java_COMSOL java API——编译comsol模型java文件
  10. 数据结构期末考题总结(附答案)
  11. golang报错fatal error: all goroutines are asleep - deadlock
  12. UFS和eMMC简介与区别
  13. 网站优化怎样的外链能轻松收录,网站外链优化攻略
  14. 四元数船舶领域Quaternion ship domain
  15. db4o的SODA查询方式
  16. java国王毒酒答案,囚犯与毒酒问题
  17. 中国电信骨干网南北互通异常 现已修复
  18. How to use Clang Static Analyzer
  19. 如何用计算机算精馏塔理论板,精馏理论塔板计算软件
  20. cs131 第二讲 颜色与线性代数

热门文章

  1. Android开发:keytool' 不是内部或外部命令 也不是可运行的程序
  2. 【Python】Scrapy入门实例
  3. C#分布式缓存二:Asp.Net中使用Couchbase
  4. Java学习笔记——JDBC读取properties属性文件
  5. Guacamole-HTML5无客户端远程桌面
  6. 白领最低生存技能手册
  7. [转载] opencv学习笔记7:图像加法与图像融合
  8. verilog 中生成块的相关知识
  9. Eclipse下创建Spring MVC web程序--非maven版
  10. JavaScript小技巧总结