Python input input, output print

1. Enter input

  • It is input to the variable input data. By keyboard input string, if need other formats, you need to do the conversion. For example int, float type data, int ()
  • The following is an example:

  If a conversion is not performed int, then the input value is a 9, when a + 7 outputs the error. 9 is because the input string, the different types of operation can not be done.

a = input ( "Enter a value:") 
Print (A)
A = int (A)
Print (A +. 7)

2. Output print

  • Formatted output

% d% s and when called placeholder variables stand for the position, the display will be replaced with the value of the variable placeholder
variable placeholders and rear and the parentheses correspond

Common placeholder
% s string placeholder
% d integer placeholder
% f float placeholder

name = "Xiao Hou" 
Age = 28
Print ( 'I am% s, I am% d, I am learning% s, f scores%'% (name, Age, 'Python', 100))
Print ( 'I % 4s, I am% d, I am learning% s, achievement% .3f '% (name, age ,' python ', 100))

are shown below:

I am Xiao Hou, I am 28, I am learning python, performance 100.000000
I Xiao Hou, I am 28, I am learning python, score 100.000


#% S is a string placeholder,% d is an integer of placeholder,% f is the float placeholders.
# Default real placeholder after six decimal places, if the fixed number of bits to be displayed, with 2% .2f represent decimal, so
#% 5s 5 shows a display character string, if not supplemented by a space in front of
  • String Output

print("hello!")

  • Variable output

a = 12

print(a)

  • A plurality of string or variable output

a = 1

age = 28

print("hello", a, age)

3. Constant

The median running process called constants never change

python is no special type of constant, expressed general agreement commonly known constants in uppercase. For example: PI = 3.14

4. Comment

python interpreter does not explain, does not execute comments

Notes advantages:

  • Comments can improve code readability and maintainability.

  • Code reserved

  • Convenient debugging

Notes written format:

  • Single-line comments begin with #, until the end of the line
  • Multi-line comments  
  1. Use three single quotes Comment

'' '
Any intermediate content is a comment,
be multiple lines
but can not have three intermediate single quotes

'''

  2. Note three pairs quotes

"" "
The middle is a comment content
but can not, there are three double quotes
" ""

note:

  • Single-line comments and generally appear in the upper right code comments

  • Good comment is the basic quality of a good programmer

  • Do each line of code to add a comment, the comment only more difficult to understand the code or description of variable

 

Exercise:

  1. Console prints the contents of the format as shown below

********************************
  There are five seconds to reach the battlefield enemy!
********************************
 
Print ( '***************************************') 
Print ( "enemy further there are five seconds to reach the battlefield! ")
Print ( '*************************************** ')
  1. Write a program that prints: Hello everyone, I am XXX, I'm from xxxx. Claim:

  • Where xxx must be a variable

  • Use the format string to print

name = input ( "Please enter the name:") 
addr = the INPUT ( "Please enter the address:")
Print ( "Hello everybody, I'm% s, I'm from% s"% (name, addr ))
  1. Two integers input from the keyboard, and print their difference, product, supplier.

a = input ( "Enter a number:") 
B = INPUT ( "Enter a number:")
A = int (A)
B = int (B)
Print ( "% D +% D =% D"% (A , B, A + B))
( "% D% =% D- D"% Print (A, B, ab &))
Print ( "% D% = D * D%"% (A, B, A * B ))
Print ( "% D / D =% D%"% (A, B, A / B))
  1. Input from the keyboard radius of a circle, the circle volume calculated, the results as shown below.

  • Radius entered string to be converted into numerical

  • π values ​​can be a take up 3.14

= 3.14 the PI 
R & lt int = (INPUT ( "Enter a number:"))
Print (. "Area% =. 2F"% (the PI * (R & lt ** 2)))

Guess you like

Origin www.cnblogs.com/houjiashan/p/12165832.html