Category: Python

Aug 15 2010

Does python have a ruby installer like gem that lets you install modules from the command line even if they are not on your machine?

With Ruby you can do gem install from the command line to install a module...even if it is not on your machine.

Can you do that with python. Does someone know of a module?

Seth

Aug 15 2010

Python cheat sheet 1

# PYTHON TIPS AND TRICKS

# int(), float(), str() are type conversion functions – pass the variable to them to return a new type.
# type() function displays the type of the variable.

# print statement prints output to the screen.
# in the interactive interpreter just type a variable name to output to the screen

# raw_input() function get a string value from the user and can assign to a variable.

# put a comma at the end of the print statement to capture the input on the same line as the print statement.  Like this:

print "Enter your name: ",
somename = raw_input()

# will appear as

# Enter your name:  Seth

# (pound) is the comment marker. 
# For multiline comments you can "trick" python into ignoring text using a multiline string like this:
"""
Anything in here will be ignore!!
Pretty cool.
It is technically not a comment but it is unassigned string that gets ignored.
"""

# speaking of … that is how you can create multiline strings in python.  I could ASSIGN to a variable and then use it.  Like this …

my_string="""  Whatever I type i
here is treated exactly like it is typed…even code and reserved words.
"""

print my_string

# import <module>         #imports a module for use
import time

# you still have to type fully qualify names for variables and methods or functions like this…
# <module>.<function> like this.
time.sleep(2)

# to avoid do the import like this
# from <module> import <methodname>  … for example

from time import sleep

# then you can just do
sleep(2)

# to import all of the public members of the module use …
# from <module> import * … for example
from time import *
# use this with caution because it can cause namespace conflicts.

##############################   SAMPLE PROGRAM
width = int(raw_input(‘What is the width of the room:  ‘))
#depth = int(raw_input(‘What is the depth of the room:  ‘))   #this works…just wanted to try it the other way.
print ‘What is the depth of the room:  ‘,
depth = int(raw_input(”))
sq_feet = width * depth
sq_yard = sq_feet / 9
cost = float(raw_input(‘What is the cost per square yard for carpet?’))
carpet_cost = sq_yard * cost

print ‘You need ‘ + str(sq_feet) + ‘ square feet of carpet’
print ‘You need ‘ + str(sq_yard) + ‘ square yards of carpet’
print ”
print ‘The cost of the carpet is ‘ + str(carpet_cost) + ‘ dollars.’

mystring = """
Anything in here will be ignored!!
Pretty cool.
It is technically not a comment but it is unassigned string that gets ignored.
"""
print ”
print mystring
from time import strftime

print strftime(‘%X %x %Z’)

raw_input()  # wait before exiting
exit()
##################################### END SAMPLE