Python learning-data operations, python learning operations

Source: Internet
Author: User

Python learning-data operations, python learning operations

There are abundant arithmetic operations in Python, which makes Python very popular in the Field of Scientific Computing. Python can provide various arithmetic operations including four arithmetic operations.

1 a = 10 2 B = 20 3 print (a + B) #30 4 print (a-B) #-10 5 print (a * B) #200 6 print (a * B) #200 7 print (a/B) #0.5 8 print (a % B) #10, return the remainder 9 print (a ** B) #10 ^ 2010 print (a/B) #0, rounded up, returns the integer part of the quotient 11 12 print (a and B) # With the operation, the returned value is 2013 print (a or B) # or operation. The returned value is 1014 print (not (a and B) # non-operation. The returned value is false.

Trielement operation:

Result = value 1 if condition else value 2

The value of the result is controlled by the condition. If the condition is true, that is, true, value 1 is assigned to the result. If the condition is false, that is, it is not true, and value 2 is assigned to the result.

1 # ternary computation: result = value 1 if condition else value 22 a, B, c = 1, 3 d = a if a> B else c4 print (d) #5

Hexadecimal conversion:

Represents the hexadecimal symbols: B: binary, o: octal, d: decimal, x: hexadecimal bin (), oct (), hex () return values are strings, and prefix with 0b, 0o, and 0x respectively.
The hex function is slower than the format of the formatted string function. It is not recommended. Eval functions are slower than int functions and are not recommended. To convert binary, hexadecimal, and octal values, you can use the median value of decimal to convert the value to decimal and then to other hexadecimal values. You can also directly use a function to convert the value.
1 # Binary Conversion 2 print (bin (10) # decimal to binary: 0b1010, where 0 B indicates binary 3 print ('{0: B }'. format (10) #1010 4 5 print (oct (12) # convert decimal to octal: 0o14, where 0o indicates octal 6 print ('{0: o }'. format (12) #14 7 8 print (hex (12) # convert decimal to hexadecimal: 0xc, where 0x indicates hexadecimal 9 print ('{0: x }'. format (12) # c10 11 print (int ('20140901', 2) # convert binary to decimal: 10, the two parameters in int are respectively the number to be converted and the hexadecimal 12 print (eval ('0b101010') of the number) #1013 14 print (int ('014 ', 8) # convert octal to decimal: 1215 print ('{0: d }'. format (0o14) #12, which must be 14 of the octal values, so it should be expressed as 0o1416 17 print (int ('0xc', 16) # hexadecimal to decimal: 1218 print (eval ('0xc') #12

When formatted output is used for hexadecimal conversion, for example, '{0: d }'. format (0o14). 'D' indicates the destination hexadecimal symbol, that is, the hexadecimal symbol to be converted into. The number of converted characters is not in decimal format, the hexadecimal symbols such as 0x, 0o, and 0b must be added before the number to be converted, which respectively indicate the number of converted hexadecimal, octal, and binary formats.

 

  

  

 

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.