Home arrow Python arrow Page 5 - Python Operators

Assignment Operators - Python

In the previous two articles on Python, I promised to tell you about Operators, but I lied. Instead I covered such things as conditionals, sets, lists, dictionaries, and so forth. Now, at last, I am going to cover the various operators that Python has to offer, and force you, I mean teach you, to manipulate data with them.

TABLE OF CONTENTS:
  1. Python Operators
  2. The Prestigious Mathematical Operators
  3. The Judgmental Comparison Operators
  4. Boolean is Not a Type of Broth
  5. Assignment Operators
By: James Payne
Rating: starstarstarstarstar / 8
January 14, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

These little guys are what make it all worthwhile. You've already worked with the '=' assignment operator, which assigns a value. Now we will learn to work with the rest of these handy hooligans:


#!/usr/local/bin/python

beers = 20

print beers

beers +=5

print beers

beers -=5

print beers

beers *=10

print beers

beers /=10

print beers

beers **=5

print beers

beers %=9

print beers

The result of this is:

  20

  25

  20

  200

  20

  3200000

  5

Note that in the above example we used all of what are called Augmented Assignment operators. If we said beers +=1 for example, what we are really saying is beers = beers + 1. In the above example that would mean beers value is now 21. The only augmenter we left out was the //= which would just have given a similar result to the /= (remember, for now the division operator truncates, but that will change in the future).

And Lastly, Operator Precedence

Operator Precedence determines in which order operations are evaluated. You can control this with parentheses(). Consider this:

2+10*10 = 120 in normal math.

2+ (10*10) = 102 because the parentheses gives precedence to the equation 10*10.

That isn't all there is to say about operator precedence; there is a whole table's worth of operator precedence I could show you, but at the moment we are out of time and really, that table is quite boring.

So come back often as we continue our death-defying discussion of...what were we talking about again? Oh yeah, Python.

Till then...



 
 
>>> More Python Articles          >>> More By James Payne
 

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: