Home arrow Python arrow Page 9 - Python 101 (part 2): If Wishes Were Pythons

Time For Lunch - Python

Begin your tour of Python with a look at its number and stringtypes, together with examples of how they can be used in simple Pythonprograms. Along the way, you'll also learn how to build conditionalexpressions, slice and dice strings, and accept user input from the commandline

  1. Python 101 (part 2): If Wishes Were Pythons
  2. Tax Evasion
  3. Q
  4. Sliced And Diced
  5. Comparing Apples And Oranges
  6. If Only...
  7. Tying Up The Loose Ends
  8. Cookie-Cutter Code
  9. Time For Lunch
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 3
May 31, 2001

print this article


Python also allows you to combine multiple conditional tests with the "and", "or" and "not" logical operators. If you consider the following code snippet,
if weekday == "Thursday":
if time == "12":
if place == "Italy":
lunch = "pasta"
you'll agree that is both complex and frightening. And so, in addition to the comparison operators we've used so liberally thus far, Python also provides the "and", "or" and "not" logical operators which allow you to group conditional expressions together. The following table should make this clearer.

Assume delta = 12, gamma = 12 and omega = 9
delta == gamma and delta > omega
delta == gamma and delta < omega
delta == gamma or delta < omega
delta > gamma or delta < omega
not delta
Given this knowledge, it's a simple matter to rewrite the example above in terms of logical operators:
if weekday == "Thursday" and time == "12" and place == "Italy":
lunch = "pasta"
Simple and elegant, wot?

And that's about it for the moment. You've learned a lot today - you now know how to manipulate strings and numbers, obtain user input from the command line, and use the "if" family of conditional statements to control the flow of your program.

In the next article, I'll be discussing some of Python's loops, together with a close look at another very useful and interesting Python data structure - the list. You make sure that you come on back for that one!

>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: