Python 3.x Print Summary

Source: Internet
Author: User
Tags pow square root string format

Python thought:

"Everything is an Object!" ”

[Python]View PlainCopyprint?
    1. Input ("Press Enter")

You can stop the program after it's finished running.

The output print function summarizes:

1. String and numeric types
can be directly output

[Python]View PlainCopyprint?
    1. >>> Print (1)
    2. 1
    3. >>> Print ("Hello World")
    4. Hello World


2. Variables
No matter what type, value, Boolean, List, dictionary ... can be directly output

[Python]View PlainCopyprint?
  1. >>> x =
  2. >>> Print (x)
  3. 12
  4. >>> s = ' Hello '
  5. >>> Print (s)
  6. Hello
  7. >>> L = [1,2,' a ']
  8. >>> Print (L)
  9. [1, 2, ' a ']
  10. >>> t = (1,2,' a ')
  11. >>> print (t)
  12. (1, 2, ' a ')
  13. >>> d = {' a ':1, ' B ':2}
  14. >>> print (d)
  15. {' a ': 1, ' B ': 2}


3. Formatted output
Similar to printf in C

[Python]View PlainCopyprint?
    1. >>> s
    2. ' Hello '
    3. >>> x = Len (s)
    4. >>> Print ("The length of%s is%d"% (s,x))
    5. The length of Hello is 5


Look at the summary of formatted output in Python BASIC programming:

(1).% character: The beginning of the token conversion specifier


(2). Convert flag:-Indicates left alignment, + indicates a positive sign before the value is converted, "" (white space character) to retain a white space before, and 0 for the conversion value if the number of digits is not enough. 0 padding


(3). Minimum field width: The converted string should have at least the width specified by the value. If it is *, the width is read from the value tuple.


(4). dot (.) Heel Precision Value: If the conversion is real, the precision value represents the number of digits after the decimal point. If the string is converted, the number represents the maximum field width. If it is *, then the precision will be read from the tuple

(5). string format conversion type


Conversion type meaning

D,i signed Decimal integer
o unsigned octal
U non-signed decimal
x hexadecimal without symbol (lowercase)
X hexadecimal without symbol (uppercase)
Floating point number represented by the e-scientific notation (lowercase)
The floating-point number represented by the E-scientific notation (uppercase)
F,f decimal Floating-point number
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
G if the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
C Single-character (accepts integers or single character strings)
R string (convert any Python object using repr)
s string (convert any Python object using str)

[Python]View PlainCopyprint?
  1. >>> pi = 3.141592653
  2. >>> Print ('%10.3f '% pi) #字段宽10, accuracy 3
  3. 3.142
  4. >>> print ("PI =%.*f"% (3,pi)) #用 * Read field width or precision from subsequent tuples
  5. Pi = 3.142
  6. >>> Print ('%010.3f '% pi) #用0填充空白
  7. 000003.142
  8. >>> Print ('%-10.3f '% pi) #左对齐
  9. 3.142
  10. >>> Print ('%+f '% pi) #显示正负号
  11. +3.141593


4. How to make print non-line wrap
Always default line wrapping in Python

[Python]View PlainCopyprint?
    1. >>> for x in range (0,10):   
    2.     print (x)   
    3.   
    4.       
    5. 0   
    6. 1  
    7. 2   
    8. 3  
    9. 4  
    10. 5  
    11. 6  
    12. 7  
    13. 8  
    14. 9  


If you want to not wrap, the previous version of 2.x can print x, plus at the end,
But it doesn't work in 3.x.
To change lines you should write print (X,end = ")

[Python]View PlainCopyprint?
    1. >>> for x in range (0,Ten):
    2. print (x,end = ")
    3. 0123456789



Stitching strings:

[Python]View PlainCopyprint?
  1. >>> "Hello""World"
  2. ' HelloWorld '
  3. >>> x = "Hello"
  4. >>> y = "World"
  5. >>> XY
  6. Traceback (most recent):
  7. File "<pyshell#10>", line 1, in <module>
  8. Xy
  9. Nameerror:name ' xy ' is not defined
  10. >>> X+y
  11. ' Helloworld '

Pow function:

[Python]View PlainCopyprint?
    1. # 2**3%5 (2 of 3 power to 5 modulo)
    2. >>> Pow (2,3,5)
    3. 3


Then it's important that the type is free to convert, what value you assign, what type of variable, and Python will automatically manage it for you.

That's really turning my C + + thinking around.

[CPP]View PlainCopyprint?
    1. >>> x = 2
    2. >>> type (x)
    3. <class ' int ' >
    4. >>> x = 2.3
    5. >>> type (x)
    6. <class ' float ' >
    7. >>> x = [2,3]
    8. >>> type (x)
    9. <class ' list ' >


Some functions:

ABS (number), returns the absolute value of the digit

CMATH.SQRT (number), returns the square root, can also be applied to negative numbers

Float (object), convert string and number to floating point

Help (), providing interactive assistance

Input (prompt), get user input

Int (object) to convert strings and numbers to integers

Math.ceil (number), returns the count of the top integer, the type of the return value as a floating-point

Math.floor (number), returns the value of the lower integer, the type of the return values as floating-point numbers

MATH.SQRT (number), the return square root does not apply to negative numbers

Pow (x,y[.z]), returns the Y-power of X (Z-modulo)

Repr (object), the string representation of the return value

Round (Number[.ndigits]), rounding numbers based on a given precision

Str (object), converts the value to a string

Python 3.x Print Summary

Related Article

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.