Ps:pdf Online Address: http://bcmi.sjtu.edu.cn/~zhaohai/ptm2012/data/Python-kernel.programming.v2.pdf
2-1 variables, print, and string formatting operators. Start the interactive interpreter, assign values to variables (strings, values, and so on) and display their values by entering variable names. Then do the same thing with the print statement. What is the difference between the two? Also try to use the string format operator%, do a few more times, and slowly become familiar.
# -*-coding:utf-8-*- >>> A = 123>>>b = ' >>> >>>
Visible, in the interactive interpreter, the display value by the input variable name is the direct display of each variable contains the variable type of the value, that is, the string with a string of flags "' quotation marks; Through the print statement, the string quotation mark ' is removed.
2-2 Program Output
(a) This script is used to calculate the value of 1 + 2 * 4
(b) This script is saved and then run and will not output any items
(c) After the Save As script runs, there is no output, as expected. The reason for this is that the interactive interpreter and the script run differently
(d) Execution of this code alone will not have any output; enter 1 + 2 * 4 in the interactive interpreter and output "9". The following:
# !/usr/bin/env python1 + 2 * 4
(e) Use the print statement to
2-3 slightly
2-4 (a)
1 # -*-coding:utf-8-*- 2 string = raw_input ("Pleaseenter something:")3Print string
Output:
is is number one!
(b)
# -*-coding:utf-8-*-string = Raw_input ("pleaseenter a number:") Print"Your Enter is%d" % int (string)
Output:
Please enter a number:123 is 123
2-5 (a)
i = 0 while I <=: print i + = 1
(b)
for in range: Print i
2-6 (1)
# -*-coding:utf-8-*-num = tenif num > 0: print u" it's positive!" "elif num < 0: print u" it's negative! "else: print u" it is 0"
(2)
#-*-coding:utf-8-*-num = Int (raw_input ("Enter a number:". Decode ("Utf-8"). Encode ("GBK")))ifnum >0:PrintU"It's a positive number! "elifNum <0:PrintU"It's negative! "Else: PrintU"It's 0 ."
2-7 (1) while
strings = Raw_input ("pleaseenter something:"= 0 while I < Len (strings): print strings[i],'(%d)' % i + = 1
(2) for
strings = Raw_input ("pleaseenter something:") Enumerate (strings): Print string,"(%d)" % i
2-8
(1)
List1 = [123,45,678,91,124== 0 while I < Len (list1): = sum + List1[i] + = 1print= 0 for in list1 := Sum +print
(2)
num_one = Int (Raw_input ("Please enter the first number :")) Num_two= Int (Raw_input ("Please enter the second NUMEBR:")) Num_three= Int (Raw_input ("Please enter the third number:")) Num_four= Int (Raw_input ("Please enter the fourth number:")) Num_five= Int (Raw_input ("Please enter the fifth number:")) List1=[Num_one,num_two,num_three,num_four,num_five]i=0sum=0 whileI <Len (list1): Sum= Sum +List1[i] I+ = 1Printsum sum=0 forNuminchList1:sum= Sum +NumPrintSum
2-9
Number = [12,345,67,890,123= 0 for in number : = sum += float ( SUM)/5print"%f is the average of these five numbers. " % average
2-10
whileTrue:num= Int (Raw_input ("Please enter a number bewteen 1 and:")) ifNum >= 1 andNum <= 100: Print "Well done.you do it." Break Else: Print "Man,you is wrong! Learn to read the prompt!!!" Continue
2-11
#-*-coding:utf-8-*-defcalc_sum ():Globalsum sum=0 I=0 whileI < 5: Num= Int (Raw_input ("Please enter five numbers in turn:". Decode ("Utf-8"). Encode ("GBK")) sum= Sum +num I+ = 1returnsum whileTrue:choice= Raw_input ("1 Summing, 2 averaging, x exit, please enter your choice:". Decode ("Utf-8"). Encode ("GBK")) ifChoice = ='X': PrintU"program will exit" Break elifChoice.isdigit () andint (choice) = = 1:
#isdigit () is used to determine whether an object is a number, which is a number that returns true, otherwise false. such as 123.isdigit () returns the result truePrintU"calculates the five-digit and"calc_sum ()PrintU"The and of these five numbers are:", SumelifChoice.isdigit () andint (choice) = = 2: PrintU"calculates an average of five numbers"calc_sum ()PrintU"The average of these five numbers is:", float (sum)/5Else: PrintU"Please re-enter"
2-12
(a) dir () displays all global variables
(b) dir returns <built-in function Dir> without parentheses; The basic introduction to Dir
(c) Type (dir) returns <type ' Builtin_function_or_method ', meaning the built-in function or method
2-13
2-14
Original statement:
Print -2 * 4 + 3 * * 2
Rewrite:
Print -2 * 4 + (3 * * 2)
2-15/2-16 doubts, to be mended
Python core programming-exercises-chapter II