Transferred from: http://www.cnblogs.com/graceting/p/3875438.html
Input is simple
- x = input ("Please input x:")
- Please input x:
The output print function summarizes:
1. String and numeric types
can be directly output
- >>> Print (1)
- 1
- >>> Print ("Hello World")
- Hello World
2. Variables
No matter what type, value, Boolean, List, dictionary ... can be directly output
- >>> x =
- >>> Print (x)
- 12
- >>> s = ' Hello '
- >>> Print (s)
- Hello
- >>> L = [1,2,' a ']
- >>> Print (L)
- [1, 2, ' a ']
- >>> t = (1,2,' a ')
- >>> print (t)
- (1, 2, ' a ')
- >>> d = {' a ':1, ' B ':2}
- >>> print (d)
- {' a ': 1, ' B ': 2}
3. Formatted output
Similar to printf in C
- >>> s
- ' Hello '
- >>> x = Len (s)
- >>> Print ("The length of%s is%d"% (s,x))
- 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)
- >>> pi = 3.141592653
- >>> Print ('%10.3f '% pi) #字段宽10, accuracy 3
- 3.142
- >>> print ("PI =%.*f"% (3,pi)) #用 * Read field width or precision from subsequent tuples
- Pi = 3.142
- >>> Print ('%010.3f '% pi) #用0填充空白
- 000003.142
- >>> Print ('%-10.3f '% pi) #左对齐
- 3.142
- >>> Print ('%+f '% pi) #显示正负号
- +3.141593
4. How to make print non-line wrap
Always default line wrapping in Python
- >>> for x in range (0,10):
- print (x)
-
-
- 0
- 1  
- 2
- 3  
- 4  
- 5  
- 6  
- 7  
- 8  
- 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 = ")
- >>> for x in range (0,Ten):
- print (x,end = ")
- 0123456789
Stitching strings:
- >>> "Hello""World"
- ' HelloWorld '
- >>> x = "Hello"
- >>> y = "World"
- >>> XY
- Traceback (most recent):
- File "<pyshell#10>", line 1, in <module>
- Xy
- Nameerror:name ' xy ' is not defined
- >>> X+y
- ' Helloworld '
Pow function:
- # 2**3%5 (2 of 3 power to 5 modulo)
- >>> Pow (2,3,5)
- 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.
- >>> x = 2
- >>> type (x)
- <class ' int ' >
- >>> x = 2.3
- >>> type (x)
- <class ' float ' >
- >>> x = [2,3]
- >>> type (x)
- <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
A
=
4
B
=
9
print
(
str
(A)
+
"\n"
+
str
(B))
#或者
print
(
"%d\n%d"
%
(A, B))
Software--machine learning and python, usage of input and output