Python Learning Notes (iii) data types

Source: Internet
Author: User

There are several types of data that can be stored in memory, and there are several types of data that can be processed directly in Python:

Number (Numbers)

Strings (String)

Listing (list)

Tuple (tuple)

Dictionary (Dictionary)

Collection (SET)

Boolean value

One, number (int, long, float, complex)

Numeric data types are used to store numeric values; they are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created:

View Code

You can use the DEL statement to delete references to some objects

The syntax of the DEL statement:

Del Var1[,var2[,var3[....,varn] []]

You can delete a reference to a single or multiple objects by using the DEL statement

Del vardel var_a, Var_b 

Integer (Int,long):

Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1 100 , and so on -8080 0 .

Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x represented by prefixes and 0-9,a-f, for example: 0xff00 ,, and 0xa5b4c3d2 so on.

Long integers can also use lowercase "l", but it is recommended that you use uppercase "L" to avoid confusion with the number "1". Python uses "L" to display the long integer type.

Floating point number:

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal. Floating-point numbers can be written in mathematical notation, such as,, 1.23 3.14 , and -9.01 so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.

Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.

Second, string

A string is any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "xyz" so on. Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I m O K 6 characters.

If the string contains both single quotes ( ‘) and ") can be identified by an escape character \ , such as:

View Code

The escape character \ can escape a number of characters, such as a \n newline, a \t tab, and the character itself, \ so the character is the one that \\ \ can be used print() in Python's interactive command line To print a string look at:

View Code

If there are a lot of characters in the string that need to be escaped, you need to add a lot of, \ in order to simplify, Python also allows to use r‘‘ ‘‘ A string that represents the internal default does not escape

View Code

If there is a lot of line wrapping inside the string, it is not good to read in a single line, and in \n order to simplify, Python allows ‘‘‘...‘‘‘ the format to represent multiple lines of content

View Code

The above is entered in the interactive command line, note that when you enter multiple lines of content, the prompt >>> becomes ... , prompting you to enter the previous line

Multi-line strings ‘‘‘...‘‘‘ can also be preceded by the r use

The list of strings has 2 order of values:

Left-to-right index starts at default 0, with a maximum range of 1 less string lengths

Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to get a substring, you can use the variable [head subscript: Tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be empty to take the head or tail

View Code

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.

The result above contains the value L of str[1], and the maximum range taken does not include the upper boundary, or the value p of str[5].

The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation.

View Code

Three, Boolean value

Boolean values are exactly the same as Boolean algebra, with a Boolean value of only two values, either, or, True False True False in Python, you can directly use True , represent a False boolean value (note case), or you can calculate it by Boolean:

View Code

Boolean values can be used and , or and not operations.

andOperations are and operations, and only all are True , the result of the and operation is True :

View Code

orAn operation is or an operation, as long as one of them is True , the result of the or operation isTrue

View Code

notAn operation is a non-operation, which is a single-mesh operator that turns True into False False True :

View Code

Four, null value

A null value is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

V. List (Brief introduction)

List (list) is the most frequently used data type in Python, and lists can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting).

The list is identified by []. Is the most common composite data type of Python. List methods are numerous, temporary unknown table, followed by a separate record.

The list of values can also be used to split the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.

The plus sign (+) is the list join operator, and the asterisk (*) is a repeating operation

View Code

Six, the tuple (brief introduction)

A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times, which is equivalent to a read-only list.

View Code

A tuple differs from a list in that the list is variable and the tuple is immutable.

Seven, Dictionary (brief introduction)

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.

The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.

View Code

Viii. Conversion of data types

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

Function Describe
int (x [, Base])

Convert x to an integer

Long (x [, Base]) Convert x to a long integer
Float (x)

Convert x to a floating-point number

Complex (real [, Imag]) Create a complex number
STR (x) Convert an object x to a string
REPR (x) Convert an object x to an expression string
eval (str) Used to evaluate a valid Python expression in a string and return an object
Tuple (s) Converting a sequence s to a tuple
List (s)

Convert the sequence s to a list

Set (s) Convert to mutable Collection
Dict (d) Create a dictionary. D must be a sequence (key,value) tuple.
Frozenset (s) Convert to immutable Collection
Chr (x) Converts an integer to one character
UNICHR (x) Converts an integer to a Unicode character
Ord (x) Converts a character to its integer value
Hex (x)

Converts an integer to a hexadecimal string

Oct (x)

Converts an integer to an octal string

Python Learning Notes (iii) data types

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.