2-1
Print to display the contents of the variable, the output string is enclosed in single quotation marks only when the variable name is used, so that non-string objects can also be displayed as strings on the screen, the print statement uses the STR () function to display the object, and the interaction interpreter calls the REPR () function to display the object
2-2 (a) operation 1+2*4 (b) will only do the operation does not output (c) operation does not display the result (d) The interaction interpreter will return a statement result after entering a paragraph (e) print ' 1+2*4 ' 2-3 2-4 (a)
# !/usr/etc/env pythonstring =raw_input ("pleasetype the string:\n") Print string
(b)
# !/usr/etc/env pythonstring =input ("pleasetype the string:\n") Print stringprint type (string)
2-5 (a)
# !/usr/etc/env pythoni=0 while i<=10: print i I+=1
(b)
# !/usr/etc/env python for in range: Print counter
2-6
#!/usr/etc/env pythonValue=input ("Please type the value:\n")ifValue>0:Print "This number is positive"elifvalue<0:Print "This number is negative"Else: Print "This is zero"
2-7
#!/usr/etc/env python#-*-coding:utf-8-*-String =raw_input ("Please type the string:\n")Print "For loop complete character-by- character display string" forIinchstring:PrintIPrint "While loop completes character-by- character display string"J=0 whilej<len (string):PrintString[j] J+=1
2-8
#!/usr/etc/env python#-*-coding:utf-8-*-Alist =[1,2,3,4,5]j=0Print "The For loop implements the and of the output list" forIinchalist:PrintI J+=IPrintJPrint "the and of the output list is implemented by the while Loop"J=0i=0 whilei<Len (alist): J+=Alist[i]PrintAlist[i] I+=1PrintJ
2-9
#!/usr/etc/env python#-*-coding:utf-8-*-Alist = (1,3,3) J=0Print "The For loop implements the and of the output list" forIinchalist:PrintI J+=ICounter=float (len (alist))Print "The average is:", j/counterPrint "the and of the output list is implemented by the while Loop"J=0i=0 whilei<Len (alist): J+=Alist[i]PrintAlist[i] I+=1PrintJPrint "The average is:", J/counter
2-10
# !/usr/etc/env python # -*-coding:utf-8-*- while True: =input ("Pleasetype the number:\n") if(0<i<100): Print " success! " Break Else : Pass
2-11
#!/usr/etc/env python#-*-coding:utf-8-*-MyList =[]i=0sum=0 whileI<5: Counter=input ("Please enter the number:\n") MyList+=[counter] I+=1 whileTrue:Print"""(1) Take Five numbers and (2) Take five numbers of the average (3) Exit"""Choose=input ("Please enter your choose:\n") ifChoose==1: forJinchRange (len (myList)): Sum+=Mylist[j]Printsumelifchoose==2: forJinchRange (len (myList)): Sum+=Mylist[j]Printsum/(Len (myList) +1) elifChoose==3: Break
2-12a. Get the list of properties for the current module [' __builtins__ ', ' __doc__ ', ' __name__ ']b.dir<built-in function dir> will display the built-in function Dirc. See type as builtin function method <type ' Builtin_function_or_method ' >d. 2-13a. B. C. 2-14
Print ( -2*4) + (3**2)
2-15
#!/usr/etc/env python#-*-coding:utf-8-*-Number1 =input ("Please enter the first value! : \ n") number2=input ("Please enter a second value! : \ n") Number3=input ("Please enter a third value! : \ n")ifNumber1>Number2:number2,number1=Number1,number2ifNumber1>Number3:number3,number1=Number1,number3ifNumber2>Number3:number3,number2=Number2,number3PrintNumber1,number2,number3number1,number2,number3=Number3,number2,number1PrintNumber1,number2,number3
Python Core Programming 2 Chapter two after-school exercises