Python basic 8 data types and data operations

Source: Internet
Author: User
Tags arithmetic bitwise bitwise operators logical operators

The content of this section:

    1. Data type
    2. Data operations
    3. Getting Started supplements
    4. Reference pages
Data type numeric int (integer)

On a 32-bit machine, the number of integers is 32 bits, the value range is -231~231-1, that is, -2147483648~2147483647 on a 64-bit system, the number of digits of the integer is 64 bits, the value range is -263~263-1, namely -9223372036854775808~9223372036854775807

Long (integer)

Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.

Attention

Since Python2.2, if an integer overflow has occurred, Python automatically converts the integer data to a long integer, so there is no serious consequence of not adding the letter L after long integer data.

Python2 2 of the 62 is the int,2 63-time Square into a long integer. Python3 are all integral types, and there is no long-term concept.

Float (float type)

A floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol.

    • Complex (plural) complex numbers are composed of real and imaginary parts, the general form is X+yj, where x is the real part of the complex number, and Y is the imaginary part of the plural, where x and y are real numbers. Note: Small number pools exist in Python:-5 ~ 257

Add

3.23 and 52.3E-4 are examples of floating-point numbers. The e tag represents a power of 10. Here, 52.3E-4 means 52.3 * 10-4.

Boolean value

True or False 1 or 0

String

"Hello World" string concatenation: The string in Python is embodied in the C language as a character array, each time you create a string, you need to open a contiguous space in memory, and once you need to modify the string, you need to open up again, splicing + Each time it appears, it will re-open a space within it.

String formatted output
name = "cathywu"print ("I am %s " % name)

#输出: I am CATHYWU

The string is%s; integer%d; floating-point number%f

String Common functions:
    • Remove whitespace
    • Segmentation
    • Length
    • Index
    • Slice
List

List lists are an ordered set of elements that can be added and removed at any time.

To create a list:

Name_list = [' Cathywu ', ' Seven ', ' Eric ']

Basic operation:
    • Index
    • Slice
    • Additional
    • Delete
    • Length
    • Slice
    • Cycle
    • Contains
Tuples (immutable list)

Tuple tuples are another ordered list. Tuple and list are very similar, however, once a tuple is created, it cannot be modified.

To create a tuple:

ages = (11, 22, 33, 44, 55)

Dictionary (unordered)

Dictionary lookup is fast, unordered, as key elements must be immutable, Python's basic types such as strings, integers, floating-point numbers are immutable, can be used as key. But the list is mutable and cannot be a key.

To create a dictionary:

person = {"Name": "Mr.wu", ' Age ': 18}

Common operations:
    • Index
    • New
    • Delete
    • Key, value, key-value pairs
    • Cycle
    • Length
Collection

Collections are unordered, non-repeating sets of elements, like collections in mathematics, that can be performed in logical and arithmetic operations

Create a Collection

s = set ([' A ', ' B ', ' C '])

Arithmetic operation of data arithmetic

The following hypothetical variables: a=10,b=20:

operator Description Example
+ Add two additional objects A+b Output 30
- Number of negative or reduced or one number minus another number -A or a-B output-10
* Multiply by two number or return a string that is repeated several times A*B Output 200
/ Except B/A Output 2
% Modulo returns the remainder of a division B%a Output 0
** Power returns the Y power of X A**b is 10 of 20 power
// Rounding the integer part of the returned quotient 9//2 Output 4,9.0//2.0 Output 4.0
Comparison operation
operator Description Example
== Equals comparison objects are equal (a==b) returns false
!= does not equal the comparison of two objects that are not equal (a!=b) returns True
<> does not equal the comparison of two objects that are not equal (a<>b) returns True
> Greater than the return x is greater than Y (A>B) returns false
< Less than return x is less than Y (a<b) returns True
>= Greater than or equal to return x is greater than or equal to Y (a>=b) returns false
<+ Less than or equal to return x is less than or equal to Y (a<b) returns True
Assignment operations
operator Description Example
= Simple assignment operator C=a+b assigns the result of the a+b operation to C
+= Addition assignment operator C+=a equivalent to c= c+a
-= Subtraction assignment operator C-=a equivalent to c= c-a
*= Multiplication assignment operator C*=a equivalent to c= c*a
/= Division assignment operator C/=a equivalent to c= C/A
%= Modulo assignment operator C%=a equivalent to c= c%a
**= Power assignment operator C**=a equivalent to c= c**a
//= Take the divisible assignment operator C//=a equivalent to c= c//a
logical operators
operator Logical Expressions Description Example
and X and Y Boolean "and", if X is false, X and Y return false, otherwise the computed value of Y is returned. (A and B) returns 20.
Or X or Y Boolean "or"-if X is non-0, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not Not X Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False
Member operations
operator Description Example
Inch Returns False if the value found in the specified sequence returns True. x in the y sequence, if X returns True in the y sequence.
Not in Returns True if no value is found in the specified sequence, otherwise False. X is not in the Y sequence if x does not return True in the y sequence.
Identity operations
operator Description Example
Is is to determine whether two identifiers are referenced from an object X is y, if ID (x) equals ID (y), is returns result 1
is not Is does not determine whether two identifiers are referenced from different objects X is not y if the ID (x) does not equal the ID (y). is not returning result 1
Bitwise operators

A bitwise operator computes a number as a binary. The variable A is 60,b 13, and the binary format is as follows:

a = 0011 1100b = 0000 1101-----------------a&b = 0000 1100a|b = 0011 1101a^b = 0011 0001~a =  1100 0011           
operator Description Example
& Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0 (A & B) Output result 12, binary interpretation: 0000 1100
Bitwise OR Bitwise OR operator: As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. (A bitwise or b) Output result 61, Binary interpretation: 0011 1101
^ Bitwise XOR Operator: When two corresponding binary differences, the result is 1 (a ^ b) output result 49, binary interpretation: 0011 0001
~ Bitwise inverse operator: each bits of the data is reversed, which turns 1 to 0 and 0 to 1 (~a) Output result-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< Left move operator: The operands of the operands all shift left several bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0. A << 2 output results 240, binary interpretation: 1111 0000
>> Right move operator: Move all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move A >> 2 output results 15, binary interpretation: 0000 1111
Operator Precedence
operator Description
** Index (highest priority)
~ + - Bitwise flip, unary Plus and minus (the last two methods are called [email protected] and [email protected])
* / % // Multiply, divide, modulo, and divide
+ - Addition subtraction
>> << Shift right, left shift operator
& Bit ' and '
^ Bitwise OR Bitwise operators
<= < > >= Comparison operators
<> = = = equals operator
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators
Getting started picking up missing ternary operations

Ternary operation is a fixed format in software programming.

    • The format syntax is: conditional expression? Expression 1: Expression 2. Description: The position in front of the question mark is the condition to be judged, the result is bool type, true when the expression 1 is called, and the expression 2 is called when false. The logic is: "If the first is executed for true, the second is executed." ”
    • Example 1 if 5>3 else 0 output 1, if 5 is greater than 3, otherwise output 0
Binary hex

0 1 2 3 4 5 6 7 8 9 A B C D E F

hexadecimal and binary correspondence relationship Binary Binary
hexadecimal hexadecimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F
Binary conversion to hexadecimal

Binary conversion to 16 binary method is to take the four-in-one method, that is, from the binary decimal point to the point of demarcation, left (or right) every four bits into a binary: 11101110011011.1001 to hexadecimal: 3b9b.9

hexadecimal conversion to binary binary

After the component is good, the control binary and hexadecimal number of the corresponding table, the four-bit binary by the right to add, the number is a hexadecimal number, and then in order, the position of the decimal point is not changed oh, and finally get the hexadecimal number. Hex: f8c.6 binary: 111110001100.011

Note that the 16-decimal notation, denoted by the letter H suffix, such as BH, represents 16 binary number 11, or it can be represented by a 0X prefix, for example, 0x23 is 16 binary 23.

Bytes Type

Python2 bytes and characters are the same. There is a special data type bytes in Python3. All data transmissions in the Python3 are transmitted in binary form. The python2 can be transmitted as a string.

There is a clearer distinction between text and binary data in Python 3. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between them particularly clear. You cannot stitch strings and byte packets, search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa). It's a good thing. All data transmissions in the Python3 are transmitted in binary form. The python2 can be transmitted as a string. A string can be encoded into a byte packet, and a byte packet can be decoded into a string. Such as:

Stringbytesencodedecodestringbytes

Example:

>>>‘€20‘.encode(‘utf-8‘)b‘\xe2\x82\xac20‘>>> b‘\xe2\x82\xac20‘.decode(‘utf-8‘)‘€20‘
Reference pages

Http://www.runoob.com/python/python-operators.html

http://blog.csdn.net/wenxinwukui234/article/details/42119265

Http://www.cnblogs.com/txw1958/archive/2012/07/19/2598885.html

Python basic 8 data types and data operations

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.