Python 数据类型

0 股票
0
0
0
0

介绍

Python 数据类型用于定义变量的类型。本文将列出所有数据类型并讨论每种数据类型的功能。.

不同类型的 Python 数据类型

Python 中有很多不同的数据类型。Python 的一些内置数据类型包括:

  • 数值数据类型:整数 (int)、浮点数 (float)、复数 (complex)。
  • 字符串数据类型:str
  • 序列类型:列表、元组、范围
  • 二进制类型:字节、字节数组、内存视图
  • 映射数据类型:字典
  • 布尔类型:bool
  • 数据集数据类型:Set、Frozenset

Python 数值数据类型

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 up to 15 decimal places.
4. complex- holds complex numbers.
In Python, we need not declare a 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 data types, use of type function](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2017/05/datatypes_useOf_type.png)

Python 字符串数据类型

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 produces output like the below picture- ![python data type, python string data type example](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2017/05/string_example.png)

Python 列表数据类型

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 Data Type - list example output](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2017/05/python_list_example_output.png)

Python 元组

The tuple is another data type which is a sequence of data similar to a 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 the below image. ![Python Data Type - tuple example output](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2017/05/python_tuple_example_output.png)

Python字典

[Python Dictionary](/community/tutorials/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, the output will be like the below image. ![Python Data Type - python dictionary example output](https://journaldev.nyc3.cdn.digitaloceanspaces.com/2017/05/python_dictionary_example_output.png)

今天关于Python数据类型的内容就到这里。别忘了在你的机器上运行每一段代码。还有,不要只是复制粘贴,尝试自己编写这些代码。.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

您可能也喜欢