Python Hello Script

Python Logo

As promised in the last post(Hello Script for PHP), this is my Hello Script for Python. Hello Script‘ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

A word of caution here – the blocks in Python is created using whitespace – so the indentation is important. So when you see an indentation in the below code, think of it as one tab(instead of four spaces or something).

I want to insert a disclaimer here – I am not that good with python. We never really clicked. I have done very limited work in Python. So if you notice any problems with the below script, let me know and I’ll correct it.


#!/usr/bin/python

print "Hello World\n"

name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + str(year) + "\n"

# If,else conditions
if (year > 2008):
	print "Welcome to the future - yes, we have flying cars!\n"
elif(year < 2008):
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!\n"
else:
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo.\n"

# For loop
for i in range(0,3):
	print str(i) + ") Hi there!"

print ""

#Numerical Array, While
rules = ['Do no harm','Obey','Continue Living']
i = 0
while(i<len(rules)):
	print "Rule " + str(i+1) + " : " + rules[i]
	i = i + 1
print ""

# Associated array, foreach
associated = {
	'hello'	:	'world',
	'foo'	:	'bar',
	'lorem'	:	'ipsum'
}
for key in associated:
	print key + " : " + associated[key]
print ""

import string
csv_values = string.split("hello,world,how,are,you\n", ",")
print string.join(csv_values, ":")


# Function, argument, return, call
def hello(name):
	return "Hello " + name + "\n"

hello_string = hello("Binny")
print hello_string

# One for the OOP fanboys - Class, members, object and stuff.
class Movie:
	name = ''
	rating = 0
	
	def __init__(self, name):
		self.name = name
		self.rateMovie()

	def rateMovie(self):
		self.rating = (len(self.name) % 10) + 1 #IMDBs rating algorithm. True story!
	
	def printMovieDetails(self):
		print "Movie : ",  self.name
		print "Rating : ", '*' * self.rating , "(", self.rating ,")\n"
	

#Create the object
ncfom = Movie("New Country for Old Men") #It's a sequel!
ncfom.printMovieDetails()

# File IO
# File reading, easy method...
file_in  = open('Hello.py', 'r')
contents = file_in.read()
print "Current file has " + str(len(contents)) + " chars\n"
file_in.close()
# Writing to a file
file_out = open('/tmp/hello.txt', 'w')
file_out.write("Hello World")
file_out.close()

# Command Executing
import commands
import os
print "Result of 'ls' command is " + commands.getoutput('ls') #Execute the command 'ls' and print its output
print

# Regular Expressions
import re
hell_check = re.compile("^Hell")
string = "Hello World"
if hell_check.match(string): print "Yup - its evil (Compiled)"
if re.match('^Hell', string): print "Yup - its evil (Not Compiled)"
print re.sub(r'l([^l])', r'\1', string)

As I said last time, save this to a file and keep it around for future reference.

Next hello script – Perl, Ruby or Tcl/Tk? Which one do you want – leave it in the comments.

4 Comments

  1. A very interesting concept. However, I think we should add following elements in the script

    – list comprehension
    – tuples and tuple unpacking
    – duck typing
    – using zip() with dict() and such

Comments are closed.