First, the content
Second, exercise: 1, use while loop output 1, 2, 3, 4, 5, 6, 8, 9, 10
Method One:
Description: When Count equals 7 o'clock, Count plus 1, the value of Count is 8, and then continue the following code is not executed, skip the loop back to the beginning to continue the loop.
Count = 1while count<11: if Count = = 7: count + = 1 continue print (count) count + = 1
Icon:
Method Two:
Description: Prints the value of count when count is not equal to 7 o'clock
Count = 1while count<11: if Count! = 7: print (count) count + = 1
2. For all numbers of 1-100
Method One:
Description: With a For loop, first define an initial value to receive the result of the calculation, add the result of each loop and assign the value to the initial value
sum = 0 # Sets the initial value of a total number for the calculation of the total number of results for I in range (1,101): # Sets the range of loop 1-100 sum = sum + i # to add and assign the results of each loop to Sumprint (sum)
Method Two:
Description: With a while loop, the first definition defines an initial value that is used to receive the result of the calculation, adding and assigning the result of each loop to the initial value
Count = 1 # Sets an initial value of sum = 0 # Sets the initial value of a total number to store the calculated result of the total number while count < 101: # Sets the range of loop 1-100 sum = sum + count # let each loop The result is added and assigned to sum count + = 1 # count self-Increment 1 operation print (sum)
3. All the even numbers in the output 1-100
For I in Range (1,101): If I% 2 = = 0: print (i)
4. All the odd numbers in the output 1-100
For I in Range (1,101): If I% 2! = 0: print (i)
5, to seek the 1-2+3-4+5-6...99 of all the number and
Method One:
Count = 1sum = 0while Count <: if count% 2 = = 0: sum = sum-count # all negative even and else: sum = sum + cou NT # all odd and count + = 1print (sum)
Method Two:
sum = 0for i in range (1,100): if I% 2 = = 0: sum = sum-i # all negative even and else: sum = sum + I # all odd and print ( Sum
Icon:
2. Login again
Http://www.cnblogs.com/fyknight/p/7967436.html
Python first day: Programming concepts, Python Introduction and installation, Python variables, python operators