Learning Python notes --- action list

1, for loop:

When writing for circulation term storage for temporary variables for each value in the list, you can specify any names.

Part of the for loop, like how many lines of code are included, each line of code is indented cycle, and both will be executed once for each value in the list.

In a for loop, the code is not indented only once, and not repeated.

The end of the colon for the statement to tell Python, the next line is the first line of the loop.

Such as:

magicians=['alice','david','carolina']
for magician in magicians:
    print(magician.title())
    print(magician.title()+',that was a great trick!')
    print("I can't wait to see your next trick,"+magician.title()+".\n")
print("Thank you,everyone.That was a great magic show!")

2, the list of numbers

In Python, using range () can easily generate a series of numbers.

Range () function lets you specify Python after reaching the second value from the first stop start value specified number, and therefore does not include a second output value.

Such as:

for value in range(1,5):
    print(value)

Output:

1 

2 

3 

4 

This is the difference between a behavior results in a programming language often seen.

To create a list of numbers, you can use the function list () will result range () is converted directly to the list. As the range () as a list () parameters, the output will be a list of numbers.

Using the range () function, the step size may also be specified.

Such as: print even 1 to 10.

even_numbers=list(range(2,11,2))
print(even_numbers)

Use the range () function can create almost any set of numbers needed.

In Python, two asterisks (**) indicates exponentiation.

Such as: The first 10 was added to the square root of an integer of a list.

squares=[]
for value in range(1,11):
    square=value**2
    squares.append(square)
print(squares)

Statistical figures:

min () The minimum value of a list of numbers

max () calculates the maximum number of list

sum the sum () of the list of numbers calculated

List Analysis: List resolve to merge and create the code for a new cycle of elements in a row, and automatically append the new element.

Such as:

cubes=[cube**3 for cube in range(1,4)]
print(cubes)

To use this grammar, the first to specify a list of descriptive names, such as: cubes; then specify a left square bracket, and define an expression (cube ** 3), for generating a list of values ​​to be stored in your . Next, write a for loop, the expression is used to provide a value (for cube in range (1,4)), plus the right bracket.

3, sliced

Part of the element processing list --- the Python called slices.

To create a slice, you can specify the index and the index of the last element of the first element to be used by one. Like the function range (), Python stops after it reached the front of the second index element. To output the first three elements of the list, specify the index 0-3.

Such as:

players=['charles','martina','michael','florence','eli']
print(players[0:3])

Printing a slice of the list, which includes only the first three values. 

If you do not specify the first index, Python will automatically start from the beginning of the list.

Such as:

print(players[:3])

Let the slice terminated at the end of the list, also can use a similar syntax.

Such as:

print(players[3:])

No matter how long the list, this syntax can let you export all elements from a particular location to the end of the list. 

A negative index returns the corresponding element from the end of the list of distance, so you can output any slice at the end of the list. For example: You want to output the value of the last three on the list, you can use the players [-3:].

We can also use slices in a for loop.

Such as:

players=['charles','martina','michael','florence','eli']
print("Here are the first three players on my tem:")
for player in players [0:3]:
    print(player.title())

Copy the list:

To copy the list, create a slice containing the entire list, it is while omitting starting index and ending index ([:]).

Such as:

my_foods=['pizza','falafel','carrot','cake']
friend_foods=my_foods[:]
print("My favorite foods are:")
print(my_foods,'\n')

print("My friend's favorite foods are:")
print(friend_foods,'\n')

Note: This must not be a simple assignment, if only a simple assignment, you can not get two lists.

4, tuples

Python will not modify the value is called invariable without variable list is called tuples.

Tuples looks like a list, but the use of parentheses instead of square brackets to identify. After the definition of a tuple, you can use the index visitors asked about elements, like access to the same list elements.

Such as:

dimensions=(200,50)
print(dimensions[0])
print(dimensions[1])

Like lists, you can also use a for loop to loop through all the values ​​in the tuple.

Such as:

dimensions=(300,100)
print("Modified dimedsions:")
for dimension in dimensions:
    print(dimension)

Although you can not modify the elements of the tuple, but to store tuple variable assignment.

Compared to the list of tuples is a simpler data structure. If a set of values ​​to be stored throughout the life cycle of the program are the same, you can use a tuple.

 

Guess you like

Origin www.cnblogs.com/Slayers-Z/p/11813538.html