Python Basics (2)

Chapter 5 Conditional Judgments and Loops

Use a while loop to calculate the sum of odd numbers within 100.

sum = 0
x = 1
while x < 100:
    sum = sum + x
    x = x + 2
print sum

During the loop, you can use break to exit the current loop, and you can use continue to skip the subsequent loop code and continue to the next loop.

Example: For two-digit numbers up to 100, use a double loop to print out all the tens digit that is less than the one digit digit, for example, 23 (2 < 3)

method one:

for x in range (1,10):
    for y in range (x+1,10):
            print x*10+y

Method Two:

for x in range (1,10):
    for y in range (1,10):
        if y>x:
            print x*10+y

Method three

for x in range (1,10):
    for y in range (1,10):
        if y>x:
            print str(x)+str(y)
    

function

Example: The sum() function accepts a list as a parameter and returns the sum of all elements of the list. Please calculate 1x1 + 2x2 + 3x3 + ... + 100x100.
L = xrange(1, 101)
print sum([i*i for i in L])

Complement the difference between range and xrange, quoted from

range() returns a list of increasing or decreasing numbers. The element value of the list is determined by three parameters. range([start,] stop[, step]) start represents the starting value of the list, and the default is "0". stop indicates the value at the end of the list, this parameter is indispensable, the parameter step indicates the step size, and the default value is "1".
range() returns a list of increasing or decreasing numbers. xrange is a class that returns an xrange object. Use xrange() to traverse and return only one value per traversal. range() returns a list, calculates and returns all the values ​​at once. Therefore, the execution efficiency of xrange() is higher than that of range()
analysis. See the code as follows:

Tower of Hanoi problem

See also the Tower of Hanoi problem

Implement the code in python

-*- coding:utf-8 -*-
move(n, a, b, c)
def move(n, a, b, c):
    if n == 1:  
       print a, '-->', c
        return
    move(n-1, a, c, b)
    print a, '-->', c
    move(n-1, b, a, c)
move(4, 'A', 'B', 'C')

Below is a better explanation that I have seen, the recursive classic case of Tower of Hanoi python implementation


Python defines default parameters

Since the arguments of a function are matched in left-to-right order, default arguments can only be defined after required arguments:

Python defines variable parameters

def fn(*args):
    print args

The Python interpreter will assemble the incoming set of parameters into a tuple and pass it to the variable parameter. Therefore, inside the function, just treat the variable args as a tuple.

Write an average() function that accepts variadic arguments.

def average(*args):
    if len(args) != 0:
         return sum(args)*1.0/len(args)
    else:
        return 0.0

print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)

Note: sum needs to be multiplied by 1.0, because the average of 1 and 2 below is 1.5, if not multiplied by 1.0, the result is 1.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324789263&siteId=291194637
Recommended