1. Key knowledge points
1. equality in Python means 1. two objects associated with different names have the same value. two different names are associated with the same object with the same ID = check whether the two referenced objects have the same value is check whether the two names reference the same object 2. to compare whether two floating point numbers are equal, use (x-y) <1.0000001 instead of X = Y. Otherwise, the error result 3 may be returned. for python, use a <= x <= B when comparing whether X is greater than or equal to a and less than B. This is different from other programming languages. the difference between values assigned by Python and other programming languages is that python supports multiple values. For example, aint, Bint, CINT = 15, 10, and 17 are equivalent: aint = 15bint = 10 CINT = 175. python can exchange values of two variables: >>> aint = 2 >>> bint = 3 >>> aint, Bint = bint, and aint. The intermediate variable has been defined. 6. in python, The while statement can be followed by the else clause while condition judgment: Statement block else: Statement block in the preceding statement, even if the while statement is not executed at a time, the program directly executes the else statement, this execution method is similar to the do while statement. The else statement at the end of the while loop can be regarded as the cleaning action at the normal end of the loop. 7. the for statement can also be aborted by the else statement block. Cocoa can use the for target in object: # statementsuite1 if boolenexpression1: break if boolenexpression2: Continue else: statementsuite2for loop to exit normally, the execution of the else block break statement provides the abnormal exit of the For Loop, skipping the else clause continue statement to terminate the current loop exception, and continuing the rest of the loop
2. Exercise after class: How many three digits can be divided by 17 in 2.1? Write a program to display these numbers
#all triple digits that can be divided exactly by 17count = 0for num in range(100,1000): if(num % 17 == 0): print num, count = count +1print print "Total number is:",count
2.2 sum of continuous integers:
(A) write a program, prompting you to enter integer x, and then calculate the sum of consecutive integer x starting from 1. That is to say, if X = 5, calculate 1 + 2 + 3 + 4 + 5 = 15
sum = 0count = 1num = raw_input("input an Integer:")num = int(num)while count <=num: print count, sum = sum + count if(count != num): print "+", count = count + 1 print "=",sum(B) rewrite the program and use nested loops to calculate the continuous integer sum. For example, if you input 5, the output requires the sum of 5 numbers:
1=11+2=31+2+3=61+2+3+4=101+2+3+4+5=15
sum = 0count = 1num = raw_input("Enter an integer:")num = int(num)while(count <= num): sum = sum + count count = count + 1 print sum,",",(C) modify the program again. If the obtained code is divisible by the number of operands (x), the displayed sum is used.
sum = 0count = 1num = raw_input("Enter an integer:")num = int(num)while(count <= num): sum = sum + count count = count + 1if(sum % num == 0): print sum. Write a program, prompt to enter an integer, and display this integer. If the input is not an integer, the program will always ask for an integer.
num = raw_input("Input an Integer:")while(num.isdigit() != True): print "Error: try again. ", num = raw_input("Input an Integer:")else: print "The Ingeger is",num3. How thick is origami in programming project 3.2 extracted from the newspaper, fold, fold again, continue to fold. Can I fold it 30 times? Suppose it can be folded 30 times. What is the thickness? Suppose the paper thickness is 1/200 cm.
height = 1.0/20000;times = raw_input("Input the times to fold:")times = int(times)height = height * 2**timesprint "After fold,the height is :",height