snippsat 661 Master Poster

Lets break it down.

IDLE 2.6.2      
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in a:
	print i
	
Mary
had
a
little
lamb

>>> for item in a:
	print item
	
Mary
had
a
little
lamb

>>> #So it can be anything you whant(i,item,element.....)
>>> #The for loop iterates over elemen in a

>>> i = 'Mary'
>>> i
'Mary'
>>> i = 'had'
>>> i
'had'
>>> i = 'a'
>>> i
'a'

>>> #So the for loop assigning each element in a to i
>>> for i in range(5):
	print i
	
0
1
2
3
4
>>> len(a)
5
>>> #So the last part that you have combine this 2
>>>
snippsat 661 Master Poster

Think maybe you are confused about calling a function and calling a method.

def my_func(name, greeting = 'Hello'):
    print '%s, %s' % (greeting, name)    

#Call a function
my_func('tom')  #output <Hello tom>

#-----------------------------------#
class myClass(object):    
    # Inside a class this is called a method
    def my_method(self,name,greeting = 'Hello'):
        print '%s, %s' % (greeting, name)        

a = myClass()  #Class instance

# Call a method
a.my_method('tom')  #output <Hello tom>

So do this.

>>> x = 'a string'
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> # Now we se method for string
>>> # And calling a method x.<name of method>
>>> x.upper()
'A STRING'
>>> x.split(' ')
['a', 'string']
>>>
snippsat 661 Master Poster
snippsat 661 Master Poster

It easier to help if you post some code and explain what the problem is.

snippsat 661 Master Poster
>>> class Stuff:
	def __init__(self):
		self.a = 0.02

		
>>> x = Stuff
>>> x
<class __main__.Stuff at 0x012007E0>
>>> # you are not instance the class
>>> x = Stuff()
>>> x
<__main__.Stuff instance at 0x011FF0F8>
>>> # Here you you instance the class
>>> x.a
0.02
>>> y = Stuff()
>>> z = Stuff()
>>> a = []
>>> a.append(x.a)
>>> a.append(y.a)
>>> a
[0.02, 0.02]
>>> # here you only get 0.02 what you maybe whant to do is this
>>> class Stuff:
	def __init__(self, a):
		self.a = a

		
>>> # Now we can pass argument to the instance
>>> x = Stuff(0.02)
>>> y = Stuff(0.5)
>>> z = Stuff(40)
>>> a = []
>>> a.append(x.a)
>>> a.append(y.a)
>>> a.append(z.a)
>>> a
[0.02, 0.5, 40]

>>> b = sum(a)
>>> b
40.520000000000003

>>> print 'the sum is %.2f' % (b)
the sum is 40.52
snippsat 661 Master Poster

You should try to put some code together.

Look at this,and you should figure how to set opp your variables.

IDLE 2.6.2 
>>> for i in range(1, 20, 3):
	print i   #print (i) for python 3	
1
4
7
10
13
16
19
######
#User input for python 2.x
>>> Start = int(raw_input('Enter first number: '))
Enter first number: 1
>>> Start
1
>>> type(Start)
<type 'int'>
>>> 
######
#User input for python 3.x
>>> Start = int(input('Enter first number: '))
Enter first number: 1
>>> Start
1
>>> type(Start)
<class 'int'>
>>>
snippsat 661 Master Poster

DaniWeb Community > Software Development > Python

So why are you posting java code here?

scru commented: Yeah, what the hell? +4
snippsat 661 Master Poster

input was defined earlier in the code

Dont ever do that.

>>> input = 'hi'
>>> input
'hi'
>>> dir(input)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>

Now input works as a string method.
And you can use input.find(),but this is very wrong.
Dont ever use build in python method as variables.

If you not sure use "my" before so my_input.

snippsat 661 Master Poster

indentations is 4 spaces,it work with 2 and 8 but never ever us anything else than 4.

A simple fix move line 8 so it get input from loop.
Remove set

import sys
result = []
f = open("sample_text.txt")
for line in f:
	for word in line.split():
		if word.startswith('h'):
			result.append(word)
                        result_length = len(result)
print "Total DISTINCT words starting with 'z': ", result_length

Here is the script with right indent,and som print to see what happends.

my sample_text.txt

hi this is an test.
hi again why not.
hi number 3.
result = []
f = open("sample_text.txt")
for line in f:
    for word in line.split():
        if word.startswith('h'):
            result.append(word)            
            print result  # use print as help easier to see what happends
            print len(result)  # use  print as help easier to see what happends      
print "Total DISTINCT words starting with '%s' is %d" % (result[0][0], len(result)) 

'''
my output-->
['hi']
1
['hi', 'hi']
2
['hi', 'hi', 'hi']
3
Total DISTINCT words starting with 'h' is 3
'''
vegaseat commented: very helpful +16
snippsat 661 Master Poster

If you want to open notepad.
You can use the os module.

import os
os.system('start notepad.exe')

#Open a webpage
os.system('start iexplore.exe http://www.google.com')