Python base -> for loop, string and tuple

python flow control> for loop, string and tuple

Thinking about the sequence of learning. Sequence: a sequence group things. All the elements of the sequence are made, the element position in the sequence is numbered starting from 0, the last position of the element is its length minus one.

for loop

dedicated for loop processing sequence, the sequences are able to direct cycle for more flexible than conventional loop

  1. It is repeatedly executed while loop according to a condition code, as long as the condition is true; loop will be repeated for the code, but not according to a condition, but according to a sequence. for loop sequence order in which each element of the implementation of a loop, when it reaches the end of the sequence, the cycle is over.

  2. The for loop will be a sequence of an element traversed.

  3. Create a for loop: first, for then is used to store the individual elements to a variable, then in, then the sequence to be traversed. No one must be specified in the body of the loop for the loop variables to use them, sometimes simply repeated a specified number of executions it.

  4. By counting the for loop. Using the range () function for each case is counted. range () function is a generator, called once each, to generate a number. To the range () function provides three parameters, start point, end point, the counting unit. The starting point is always the first sequence of values ​​that people would have liked, but the end point is not included.

for i in range(10, 0, -1):
    print(i)

for letter in word:
    print(letter)

String

String is also a sequence that each element is a character.

  1. Using sequence string operators and functions. Python provides many useful functions and operators for operating the various sequences, including a string, such as in, len (). These operators and functions can tell you some simple but important information about the sequence, such as how long it is and whether it contains a specific element.

  2. len function, len (message), it will return length of the sequence, i.e. the number of elements in the sequence.

  3. in operator. Determining whether or not an element contained in the sequence. Is a condition calculated to True or False. Such as "e" in message to True.

  4. The string index. Can be traversed in order to string character by character through the for loop, which is also called sequential access. This means that you must see a sequence of an element. Random access can be obtained directly from the elements in a sequence anywhere, indexing is a random access, via a position number you can get the elements that position.

  5. String of positive and negative position numbers. When a position number n, the sequence of the reference point at the beginning of the time reference; position while a negative number is counted from the beginning of the forward end of the sequence, the end of the sequence as a reference point. Sometimes we do need to end of the sequence as a reference point, then a negative position numbers can come in handy.

  6. random.randrange (0, 10) to generate a random number from 0 to 10 range, comprising a 0 to a 10, the index count for standard sequence.

0 1 2 3 4
i n d e x
-5 -4 -3 -2 -1
import random

word = "helloworld"
high = len(word)
low = -len(word)

for i in range(10):
    position = random.randrange(low, high)
    print("word[]", position, "]\t", word[position])

Tuple

Tuple in the form of an ordered set of information organization and management

Guess you like

Origin www.cnblogs.com/mindshare/p/11409627.html