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

Comparing Apples And Oranges - 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

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The examples you've just seen are very rudimentary. To really add some punch, you need to know how to construct what the geeks call a conditional statement. And the very basis of a conditional statement is comparison - for example, "if this is equal to that, do thus and such".

Python comes with a bunch of useful operators designed specifically for use in conditional statements. Here's a list:


Assume delta = 12 and omega = 9
Operator What It Means           Expression Evaluates To
------------------------------------------------------------------------------
==      is equal to                   delta == omega          False
!=      is not equal to               delta != omega          True
>       is greater than               delta > omega            True
<       is less than                  delta < omega            False
>=      is greater than or equal to   delta >= omega           True
<=      is less than or equal to      delta <= omega           False
These comparison operators can be used for both strings and numbers. A positive result returns true (1), while a negative result returns false (0).

An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator (=) and the equality operator (==). The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression. So

a = 47;
assigns the value 47 to the variable a, while

a == 47
tests whether the value of a is equal to 47.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- 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: