Python Reference Sheet

Looking to create a Python reference sheet for the DIC community

Page 1 of 1

2 Replies - 2333 Views - Last Post: 11 February 2008 - 02:37 AM Rate Topic: -----

#1 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 726
  • View blog
  • Posts: 8,638
  • Joined: 14-September 07

Python Reference Sheet

Posted 24 January 2008 - 11:02 AM

Hope this is right place to try to start this.

I would like to help contribute to a Python reference sheet since I am learning it. Looking for people to contribute and then we can hand off the info off to the DIC staff to hopefully publish it.

I will post back by starting it off with some useful string methods.

This post has been edited by nykc: 24 January 2008 - 11:22 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Python Reference Sheet

#2 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 726
  • View blog
  • Posts: 8,638
  • Joined: 14-September 07

Re: Python Reference Sheet

Posted 24 January 2008 - 11:18 AM

***Useful String Methods***

upper() - returns uppercase version of the string.
lower() - returns lowercase version of the string.
swapcase() - returns a new string where the case of each letter is switched. (Uppercase becomes lowercase and lowercase becomes uppercase.)
capitalize() - Returns new string where the first letter is capitalized and the rest is lowercase.
title() - Returns a new string where the first letter is capitalized and the rest are lowercase.
strip () - Returns a string where all the white space (tabs, spaces and newlines) at the beginning and end is removed.
replace(old, new [,max]) - Returns a new string where occurrences of the string old are replaced with the string new. The optional max limits the number of replacements.

*** Mathmatical Operators with Integers ***

[ * ] - Multiplication: 5 * 3 = 21

[ / ] - Division: 5 / 3 = 1

[ % ] - Modulus: 5 % 3 = 2

[ + ] - Addition: 5 + 3 = 8

[ - ] - Subtraction: 5 - 3 = 2


*** Mathmatical Operators with Floating-Point numbers ***

[ * ] - Multiplication: 5.0 * 3.0 = 15.0

[ / ] - Division: 5.0 / 3.0 = 1.6666666666666667

[ % ] - Modulus: 5.0 % 3.0 = 2.0

[ + ] - Addition: 5.0 + 3.0 = 8.0

[ - ] - Subtraction: 5.0 - 3.0 = 2.0


***Selected Type Conversion Functions***

float(x): Returns a floating point value by converting x
int(x): Returns integer value by converting x
str(x): Returns string value by converting x

This post has been edited by nykc: 24 January 2008 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 lollipop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-February 08

Re: Python Reference Sheet

Posted 11 February 2008 - 02:37 AM

Great idea nykc, I'd like to add a section on one of Python's most useful and powerful features - introspection.

This post is probably too long to go into the reference sheet directly, it's more like a short tutorial(!), but a line on how to use dir() would be a good addition I think since introspection is so very very useful.

----------

Introspection is the ability to dynamically inspect any python object and find out what it can do. Since 'everything in Python is an object' this feature is not limited to traditionally 'object-oriented' coding styles.

So, how does it work?


To 'introspect' a particular object you use a built-in method called dir(). This is probably named after the dir command in DOS which is used to list the contents of a directory.

So, if my_string is a string, we would introspect it as below. This example is done at the live prompt but you can use it the same way in code, just don't forget to print out whatever dir() returns or you won't actually see it!

>>> my_string = "A string"
>>> dir(my_string)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '_
_reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith'
, 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
rip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'transla
te', 'upper', 'zfill']



What we have here is a list of all the methods and attributes of the my_string object.

When might this be this useful?

Let's just say that ...




  • you can't recall whether the method for finding the first letter of a string is called 'startswith' or 'firstletter'. To quickly find out, simply make a string, call dir() on it, and you will be reminded.


  • you think there *may* be a method to do what you want, but are not sure. In these cases, dir() is a handy first port of call.


  • you are writing very dynamic code that adds methods and attributes 'on the fly'. During development, dir() is an invaluable accomplice.





There are many other ways to find out vital information at runtime when using python. For more in-depth coverage of all of these, see one of the following:


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1