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 Data Types)
There are different types of python data types. Some built-in python data types are:
有不同类型的python数据类型。 一些内置的python数据类型是:
(Python Data Type – Numeric)
Python numeric data type is used to hold numeric values like;
- int – holds signed integers of non-limited length.
- long- holds long integers(exists in Python 2.x, deprecated in Python 3.x).
- float- holds floating precision numbers and it’s accurate upto 15 decimal places.
- 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数字数据类型用于保存数字值,例如;
- int –保留无限制长度的有符号整数。
- long-保持长整数(在Python 2.x中存在,在Python 3.x中已弃用)。
- float-保留浮点精度数字,它的精度最高为15个小数位。
- 复数-包含复数。
在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))
如果运行上面的代码,您将看到如下图所示的输出。
(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)
上面的代码产生如下图所示的输出-
(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
上面的代码将产生如下输出:
(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数据类型元组示例代码的输出将如下图所示。
(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 variable
a = {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 variable
a = {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.
所以这就是今天有关python数据类型的全部内容。 不要忘记在自己的机器上运行每段代码。 也不要只是复制粘贴。 尝试自己编写代码行。 #happy_codingReference: Python Documentation for Data Types
参考: 数据类型的Python文档
python数据类型