This series of examples of the use of python3.x, modified: 2019-09-03,23: 03:36
python in a "object reference" to store data in order to express the data objects, each object has a status, objects and values.
Utility functions:
- id (): see variable id address
- type (): Object Type View
- input (): Get the type of the variable character string input by the user
- eval (): used to perform a string expression, and returns the value of the expression. Used in conjunction with input (), obtaining the value of a continuous variable
- To exchange variables: x, y = y, x
num1, num2, num3 the eval = (INPUT ( " input three numbers separated by commas intermediate: " )) the try : Print (num1, num2, num3) the except Exception AS E: Print ( " error: " , E) running results: input three numbers separated by commas intermediate: 17, 18, 19 17. 19 18
Common Python built-in types:
- Value Type (Numeric Types): comprising int (integer), float (float), complex (complex)
- Sequence type (Sequence Types): There str (string), list (list), tuple (tuple)
- Iterative type (Iterator Types): to provide a container used for loop iterations
- Collection type (Set Types): with a set (set variable) and frozenset (fixed or immutable collection set)
- Mapping type (Mapping Types): Only dict (dictionary)
type of data
1. Integer type:
- Integer Interger:
Hex conversion functions:
-
- bin (int): Decimal Binary turn, converted data will be prefixed with the character 0b
- oct (int): decimal octal turn, converted data will be prefixed 0o character
- hex (int): Decimal Binary turn, the data will be converted to character prefix 0x
- int (s, base): to convert the string s base parameter according to the decimal value
- format (value [, format_spec]): This function is used to convert other binary decimal (prefix character is removed)
= 255 dec_num # decimal Print (bin (dec_num)) # binary output "0b11111111" Print (the OCT (dec_num)) # octal output "0o377" Print (hex (dec_num)) # hexadecimal output "0xff" bin_num = ' 0B ' + ' 10101 ' Print (int (bin_num, 2)) # binary decimal output 21 is NUM = 78 # decimal Print (the format (NUM, ' B ' )) # outputs a binary string "1.00111 million" Print (the format (NUM, 'O' )) # Output octet string "1 16" Print (the format (NUM, ' X ' )) # output hex string "4e"
- Boolean Boolean:
Bool (Boolean) int subclass can be used BOOL () function. It has only two values True and False.
-
- True: 1 may be used to represent the values
- False: value 0, special object None, an empty string, an empty list, empty tuple returns False
2. floating-point type:
- Decimal (float):
Common functions:
-
- float (): cast to float
- fromhex (): Object methods, hexadecimal to decimal floating point
- hex (): class method returns a string of hexadecimal floating point
- is_integer (): class method of determining whether an integer, if a scale of zero, Return True
= 71.235 float_num # define float float_num_hex float_num.hex = () # Returns a string of hexadecimal floating point Print (float_num_hex) # output "+ 0x1.1cf0a3d70a3d7p. 6" Print (float.fromhex (float_num_hex)) # rpm back to the decimal number 71.235
Special floating-point numbers:
float ( 'nan'), float ( 'Infinity'), float ( '- inf') are three special floating point numbers, representing the non-digital (Not a number), infinity (Infinity), negative infinity (Negative Infinity)
Whether using standard math library module isNaN () determines whether the data is a NaN, isinf () determines inf data or -inf
- Complex (complex):
complex (re, im): re is the real, the real number; Imagine IM is imaginary, the imaginary part of the character plus the need to 'j' or 'J'
6J. 5 = + complex_num Print (type (complex_num)) # output <class 'Complex'> Print (complex_num.real) # outputs the real part 5.0 Print (complex_num.imag) # output an imaginary part 6.0
- More precise Decimal Type:
- Decimal type need to import using the decimal module
- The Decimal () function can be specified to a string of significant digits, Decimal plurality of data are added, and the sum of the number of significant digits in the maximum significant digits; product of multiplying the number of significant digits is valid multiplier the sum of digits
- getcontext (): Get the recording Decimal arithmetic defined environment, such as accuracy, rounding rules. getcontext (). prec of accuracy, getcontext (). rounding as the rounding rule
from decimal Import * Print (a Decimal (10/3)) # output 3.333333333333333481363069950020872056484222412109375 NUM_1 = a Decimal ( ' 0.125 ' ); NUM_2 = a Decimal ( ' 8.8888 ' ) # string parameter to specify the number of significant digits Print (NUM_1 NUM_2 +) # output 9.0138 Print (NUM_1 * NUM_2) # output 1.1111000 Print (getContext (). prec) # returns the accuracy of the output 28 getContext (). prec. 3 = # setting accuracy Print (NUM_1 NUM_2 +) # output 9.01 Print (getContext (). a Rounding) # Returns the rounding rule output ROUND_HALF_EVEN getContext (). ROUND_FLOOR a Rounding = # Set the rounding rules to negative infinity rounding Print (NUM_1 * NUM_2) # Output 1.11
3. Score type:
- Using a fractional function, you need to import the module fractions. Fractions () method is as follows
Fraction(numerator, denominator):
-
- numerator: Molecular, the default value is 0
- denominator: the denominator, the default value is 1
- Whether or molecular denominator, use only a positive or negative integer, otherwise an error
- Fraction () method will automatically reduce fractions, but the parameters can not be mixed floating point and integer, otherwise an error TypeError
- With Fraction () method can be fractional arithmetic addition or multiplication
from the fractions are Import Fraction NUM_1 = Fraction (12, 36) # define a variable fraction of the molecules 12, the denominator 36 NUM_2 Fraction = (. 3,. 7 ) Print (NUM_1 NUM_2 +) # adder output 16/21 Print (NUM_1 * NUM_2) # multiplication output 1/7 num_3 Fraction = (2.1,. 3) # error: error: both arguments should be rational instances ( two parameters should be a rational number) [way as the computer stores floating-point numbers, floating point numbers can be stored accurately]
This series of blog mainly from "scratch learning Python programming" extract