Conditionals and Looping

So far, we have just been looking at data types, which should show you how powerful Python’s data types are. However, you cannot write complex programs without conditional statements and loops.

Python has most of the standard conditional checks, such as > (greater than), <= (less than or equal to), and == (equal), but it also adds some new ones, such as in. For example, we can use in to check whether a string or a list contains a given character/element:

>>> mystring = "J Random Hacker">>> "r" in mystringTrue>>> "Hacker" in mystringTrue>>> "hacker" in mystringFalse

The last example demonstrates how it is case sensitive. We can use the operator for lists, too:

Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.