Você está na página 1de 4

2 Handout

Conditional Statements
Conditionals are where a section of code is only run if certain conditions are met. However, Conditionals are only run once. The most common conditional in any program language, is the 'if' statement. Here is how it works: Code Example 1 - if statement and example if {conditions to be met}: {do this} {and this} {and this} {but this happens regardless} {because it isn't indented} #EXAMPLE 1 y = 1 if y == 1: print "y still equals 1, I was just checking" #EXAMPLE 2 print "We will show the even numbers up to 20" n = 1 while n <= 20: if n % 2 == 0: print n n = n + 1 print "there, done." Example 2 there looks tricky. But all we have done is run an 'if' statement every time the 'while' loop runs. Remember that the % just means the remainder from a division - just checking that there is nothing left over if the number is divided by two - showing it is even. If it is even, it prints what 'n' is.

'else' and 'elif' There are many ways you can use the 'if' statement, do deal with situations where your boolean expression ends up FALSE. They are 'else' and 'elif'. 'else' simply tells the computer what to do if the conditions of 'if' arent met. For example, read the following: Code Example 2 - the else statement a = 1 if a > 5: print "This shouldn't happen." else: print "This should happen." 'a' is not greater than five, therefore what is under 'else' is done. 'elif' is just a shortened way of saying 'else if'. When the 'if' statement fails to be true, 'elif' will do what is under it IF the conditions are met. For example:

Code Example 3 - The elif statement z = 4 if z > 70: print "Something is very wrong" elif z < 7: print "This is normal" The 'if' statement, along with 'else' and 'elif' follow this form: Code Example 4 - the complete if syntax if {conditions}: {run this code} elif {conditions}: {run this code} elif {conditions}: {run this code} else: {run this code} #You can have as many or as little elif statements as you need #anywhere from zero to the sky. #You can have at most one else statement #and only after all other ifs and elifs. One of the most important points to remember is that you MUST have a colon : at the end of every line with an 'if', 'elif', 'else' or 'while' in it. I forgot that, and as a result a stack of people got stumped at this lesson (sorry ;) ).

Indentation
One other point is that the code to be executed if the conditions are met, MUST BE INDENTED.

The length of a string


The len() method calculates the number of characters in a string. The white characters are also counted.
#!/usr/bin/python # length.py s1 = "Eagle" s2 = "Eagle\n" s3 = "Eagle " print len(s1) print len(s2)

print len(s3)

Stripping white characters


In string processing, we might often end up with a string that has white characters at the beginning or at the end of a string. The term white spaces, characters refers to invisible characters like new line, tab, space or other control characters. We have the strip(), lstrip() and rstrip()methods to remove these characters.
#!/usr/bin/python # strippig.py s = " Eagle "

s2 = s.rstrip() s3 = s.lstrip() s4 = s.strip() print print print print s, len(s) s2, len(s2) s3, len(s3) s4, len(s4)

We apply the stripping methods on a string word which has three white spaces. One space at the start and two spaces at the end. Note that these methods remove any number of white spaces, not just one.
s2 = s.rstrip()

The rstrip() method returns a string with the trailing white space characters removed.
s3 = s.lstrip()

The lstrip() method returns a string with the leading white space characters removed.
s4 = s.strip()

The strip() method returns a copy of the string with the leading and trailing characters removed.

$ ./stripping.py Eagle Eagle 6 Eagle Eagle 5 7 8

Output of the stripping .py example. Remember that when we print output to the terminal, one space character is added between the string and the number. This happens when we delimit output by a comma character for the print keyword.

Você também pode gostar