Está en la página 1de 3

Python tutorial

IF statement
a = int(input("a: ")) b = int(input("b: ")) if a < b: print('a is less than b') elif a > b: print('a is greater than b') else: print('a is equal to b')

There can be zero or more elif parts, and the else part is optional. The keyword elif is short for else if, and is useful to avoid excessive indentation

Loops
While loop
count = 0 while count < 10: print('count = ',count ) count = count + 1

The while loop executes as long as the condition (here: count < 10) remains true. Any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).
a, b = 0, 1 while b < 10: print(b) a, b = b, a+b

Fibonacci series: the sum of two elements defines the next The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the righthand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right The keyword end can be used to avoid the newline after the output, or end the output with a different string:
a, b = 0, 1 while b < 1000:

http://www.learn-with-video-tutorials.com

print(b, end=',') a, b = b, a+b

FOR loop The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Pythons for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence:
for counter in range(5): print('counter = ', counter)

If you do need to iterate over a sequence of numbers, you can use range() function. It generates arithmetic progressions. The given end point is never part of the generated sequence; range(5) generates 5 values, the legal indices for items of a sequence of length 5. It is possible to let the range start at another number, or to specify a different increment
for counter in range(10, 20, 3): print('counter = ', counter) words = ['car', 'home', 'bicycle'] for w in words: print(w, len(w))

Lists all items


for i in range(len(words)): print(i, words[i])

Another way of listing items To iterate over the indices of a sequence, you can combine range() and len() as follows
for w in words[:]: if len(w) > 5: words.insert(0, w) words

If you need to modify the sequence you are iterating over while inside the loop, it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient Loop over a slice copy of the entire list The break statement, breaks out of the smallest enclosing for or while loop. Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the http://www.learn-with-video-tutorials.com

loop is terminated by a break statement.


for n in range(2, 10): for x in range(2, n): if n % x == 0: print(n, 'equals', x, '*', n//x) break else: print(n, 'is a prime number')

The else clause belongs to the for loop, not the if statement When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a try statements else clause runs when no exception occurs, and a loops else clause runs when no break occurs
for x in range(10): if x % 2 == 0: continue print(x)

The continue statement, continues with the next iteration of the loop.

Watch Python video tutorial, visit http://www.learn-with-videotutorials.com/python-tutorial-video

http://www.learn-with-video-tutorials.com

También podría gustarte