Python Practice path __ Data type

Source: Internet
Author: User

There are six standard data types in the Python3:

    • Number (numeric)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)
    • Set (SET)

In Python, the most basic data structure is the sequence (sequence). Each element of the sequence is assigned an ordinal-that is, the index (the position of the element), the first index is 0, the second is 1, and so on.

    • Number (numeric)

Numeric type:

Type Describe Syntax examples
int (integral type) Number without fractional part (only one integer type int (Long integer) in Python3, without long in Python2) 42
Float (float type) A number with a small number of parts

42.5,

42.5e-2

Complex (compound type) Real numbers (integers or floating-point numbers) and imaginary and 38+42j,42j
BOOL (Boolean type) There is no bool type in Python2, which is represented by the number 0 false,1 for true.

Numeric operators:

Operator Describe
+ Addition
- Subtraction
* Multiplication
/ Division (surplus number)
// Division (divisible, no number)
% Take surplus
** Power (exponentiation)

Example:

1>>> 7 + 3# addition2103>>> 7-3# Subtraction445>>> 7 * 3# multiplication621st7>>> 2/4# Division, get a floating-point number80.59>>> 2//4# Division, get an integerTen 0 One>>> 7% 3# take the remainder A1 ->>> 7 * * 3# Power (exponentiation) -343

    • String strings

Strings are a string of characters that consist of numbers, letters, and underscores.

The strings in Python are enclosed in single quotation marks (') or double quotation marks (""), and Python uses a backslash (\) to escape characters when special characters are required.

Escape characters:

escape character description
\ (at end of line)
\ \ backslash symbol
\ ' single quotation marks
\ " double quotes
\a ring
\b backspace (Backspace)
\e escape
\000 Terminator, the string after \000 all ignore
\ n line break
\v portrait tab
\ t horizontal tab (tab)
\ r carriage return
\f page Break
\oyy octal number, yy for character, for example: \o12 for line break
\xyy hexadecimal number, yy for character, for example: \x0a for line break
\other Other characters output in normal format

String operators

Example a = "Hello", B = "Python"

Operator Describe Example
+ String connection
>>> A + b'hellopython'

* Repeating output string
>>> A * 2'hellohello'

[] Getting characters in a string by index
>>> a[1]'e'

[:] Intercept part of a string
>>> a[1:4]'ell'

Inch Member operator-Returns TRUE if the string contains the given character
" H " inch atrue

Not in Non-member operator-returns TRUE if the string does not contain the given character
" a "  not inch atrue

R/r

Raw string

% format string

String formatting

Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.

Python Practice path __ Data type

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.