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:
>>> mylist = ["soldier", "sailor", "tinker", "spy"]>>> "tailor" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access