A brief introduction to several data types in Python __python

Source: Internet
Author: User
Tags sublime text

The data types in Python are generally grouped into the following categories:

Number (Numbers)         includes Int,long,float,complex 
string (string)        For example: Hello, "hello", hello list  
(list)          for example: [1,2,3] , [1,2,3,[1,2,3],4]  
Dictionary (dictionary)       for example: {1: "Nihao", 2: "Hello"}  
Tuple (tuple)          for example: (1,2,3,ABC) 
Bool (Boolean)          
?

Because Python thinks everything is an object, Python does not have to actively declare the type of a variable like some other high-level language.

For example I'm going to give an implementation of the variable I assignment 100,python:

i=100
?

Implementation of C #:?

1 int i = 100;

Here are one by one simple examples of these types of data

Number Type

int and long

The reason for putting int and long together is that the python3.x has no distinction between int and long, and is unified with Int. Python2.x is still a distinction. Here I take Python2.7 as an example:

>>> i = ten
>>> type (i) 
<type ' int ' > 
  
>>> i=10000000000
>>> Type (i) 
?

So why 10 is int,10000000000 is long, of course, this is related to the maximum value of int, the int type of the maximum value of 231-1, that is 2147483647, can also use Sys.maxint.

>>> 2**31-1
2147483647L
>>> sys.maxint 
2147483647

Why the value in the above method is a long type (the number plus ' L ' means long), because the value of 2**31 is 2147483648, the value is a long, minus 1 with a long, the result is a long, But actually the maximum value of int is 2147483647?

>>> type (2147483647) 
<type ' int ' > 
>>> type (2147483648) 

Float type

Float is basically the same as the float in other languages, floating point number, in other words, is the numbers with decimal points, precision and machine-related. For example:?

>>> i = 10000.1212
>>> type (i) 

Complex: The plural type, the specific meaning and usage, can view the related documents.

String Type

There are three ways to declare a string: Single, double, and triple quotes (including three single quotes or three double quotes). For example:?

>>> str1 = ' Hello World '
>>> str2 = ' Hello World '
>>> str3 = ' Hello World '
> >> STR4 = "" "Hello World" ""
>>> print str1 
Hello world 
>>> print str2 
Hello World 
>>> print str3 
Hello world 
>>> print STR4 

Strings in Python have two types of data: STR and Unicode. The STR type uses ASCII encoding, which means it cannot represent Chinese. Unicode types are Unicode encoded to represent arbitrary characters, including Chinese and other languages. And Python does not exist as a char type in C, even a single character is a string type. The ASCII encoding that the string defaults to, and if you want to display a Unicode type, you need to precede the string with ' u ' or ' u '. For example:?

>>> str1 = "Hello"
>>> print str1 
Hello 
>>> str2 = u "China"
>>> Print str2 
China

Because of the frequent manipulation of strings in projects, and because of the many problems with string coding problems, here's how to encode strings. In the process of dealing with Python, you often encounter ASCII, Unicode, and UTF-8 three of encodings. See this article for a specific introduction. My simple understanding is that ASCII encoding is suitable for English characters, Unicode for non-English characters (such as Chinese, Korean, and so on), while Utf-8 is a stored and transmitted format for Uncode characters (in 8-bit code). For example:?

u = U ' han '
print repr (u) # u ' \u6c49 ' 
s = U.encode (' UTF-8 ') 
print repr (s) # ' \xe6\xb1\x89 ' u2 
= S.decode (' UTF -8 ') 

Explanation: Declares the Unicode string "Han", its Unicode encoding is "\u6c49", after the Utf-8 encoding conversion, its encoding becomes "\xe6\xb1\x89".

for an empirical summary of coding:

1. In python file header declaration encoding format;

#-*-Coding:utf-8-*-
2. Declare the string as Unicode type, that is, add u or u before the string;

3. For file read and write operation, it is recommended to apply Codecs.open () instead of the built-in open (), follow a principle, in which format to write, in which format to read;

Suppose there are "Chinese characters" in a text file saved in ANSI format, if you use the following code directly, and you want to print it on the GUI or in an IDE (for example, in sublime text, or in Pydev), there will be garbled or abnormal, Because codecs reads the contents according to the encoding format of the text itself:?

f = Codecs.open ("d:/test.txt") 
content = F.read () 
f.close () 

Instead, use the following method (which works only with Chinese):?

#-*-Coding:utf-8-*- 
  
import codecs 
  
f = Codecs.open ("d:/test.txt") 
content = F.read () 
f.close 
  
() If Isinstance (content,unicode): 
  print Content.encode (' utf-8 ') 
  print "Utf-8"
Else: 
  

List Type

A list is a modifiable collection type whose elements can be basic types such as numbers, strings, or a collection object such as lists, tuples, dictionaries, or even custom types. The way it is defined is as follows:?

>>> nums = [1,2,3,4] 
>>> type (nums) 
<type ' list ' > 
>>> print nums 
[1, 2, 3, 4] 
>>> strs = ["Hello", "World"] 
>>> print STRs 
[' Hello ', ' world '] 
>>> lst = [1, "Hello", False,nums,strs] 
>>> type (LST) 
<type ' list ' > 
> >> Print LST 

Indexed access to list elements, starting at 0, supporting negative indexes, and-1 being the last.

>>> LST = [1,2,3,4,5] 
>>> print lst[0] 
1
>>> print lst[-1] 
5
>> > Print Lst[-2] 
4
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.