Pages

Python: Printing Class Object

When we try to print a class object, 'print object' will show as the property. If we want to see what we want to print, just use __repr__.

class Foobar():
"""This will create Foobar type object."""
def __init__(self):
print "Foobar object is created."

def __repr__(self):
return "Type what do you want to see here."

a = Foobar()

print a

Python: Modul Import

Let's prepare a new file and save as "modul.py" to "C:\Python directory".

    name = 'Flow Chart'
    color = 'White'
    list = ['python', 'html5']

Open python shell.

if we want to import our modul:

>>>import modul     #without extension ".py"
>>>modul.variable

For example:

>>>modul.name
>>>'Flow Chart'

If we dont want to use modul name over and over again:

>>>from modul import name
>>>name
>>>'Flow Chart'

If we want to import all the variable at the same time:

>>>from modul import *

We sometimes want to import variable with another name, if we already use this variable name.

>>>from import name as new_name
>>>new_name
>>>'Flow Chart'

Python: Usign Dictionary For Menu

Using dictionary for menus are no so popular nowadays because of visual programing enviroment. But it is good to know.


def right():
print "\nYou turned right and see the beautiful lady.\n"

def left():
print "\nYou turned left and see the witch.\n"

def stand():
print "\nYou decided to do nothing. So, a thunder hit you!..\n"

print "What is your choice?"
print " 1 : Go right"
print " 2 : Go left"
print " 3 : Stand still"

choice = input('Select what to do:>')

menu = { 1 : right ,
2 : left ,
3 : stand
}
 
menu[choice]()

As you see, we are getting value with key (choice). This returns another word(right, left, stand). Brackets complete the function name. So we call our function as result.

right()
left()
stand()

Python: Counting Words in Statement

We can use dictionaries for word count too.

text = 'Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower maintenance costs.'

word = text.split()

dict = {}

for i in word:

    if i in dict:
dict[i] = dict[i] + 1
    else:
dict[i] = 1

for j in sorted(dict):

    print j , '----->' , dict[j]

This will print:

Python: Counting Letters in Statement

We will use dictionary to do that.

statement = 'This is a good day to live.'
dict = {}

for letter in statement:

if letter in dict:
dict[letter] = dict[letter] + 1
else:
dict[letter] = 1

print "Used letter numbers including ' ' and '.'"

for j in sorted(dict):

print j , '---->' , dict[j]

This will print:

Python: For in Dictionary

We can use 'for in' for dictionaries as list items.

dict = {'white' : 'clear soul' , 'flow' : 'contiunity' , 'name' : 'flow chart'}

for i in dict:
    print i

This will bring the keywords : white , flow, name

for i in dict:
    print dict[i]

This will bring the meanings: clear soul, contiunity, flow chart

If we want to get the value of a key in the dict we must check if it is in the dict or not. Otherwise we can get error. So we must check the availability first.

dict = {'white' : 'clear soul' , 'flow' : 'contiunity' , 'name' : 'flow chart'}
search = raw_input('What do you search?:>)

if search in dict:
    print dict[search]
else:
    print "This is not in dictionary."

Python: Dictionaries

There is a cool feature in Python which is called 'dictionary'. And as it sounds it is same as the dictionary. When we search for a word in dictionary we find the related explaination. The dictionary in Python works like this too.

dict = {'red' : 'color of blood' , 'car' : 'vehicle on the streets' , 'flow' : 'continuity'}
print dict

Let's print flow:

print dict['flow']

We can add more words to our dicitonary.

dict['python'] = 'Montly Python\' s Flying Circus'
print dict

We can use numbers too:

dict[1] = 'one'
dict[5] = 'five'
print dict

If we want to delete a member:

del dict['red']
del dict['1']
print dict