diff --git a/sampleclass.py b/sampleclass.py index 0870430..0292fd4 100644 --- a/sampleclass.py +++ b/sampleclass.py @@ -2,22 +2,22 @@ -class TheThing(object): - def __init__(self): - self.number = 0 - def some_function(self): - print "I got called." - def add_me_up(self, more): - self.number += more - return self.number +class TheThing(object): # Create the class Structure named 'TheThing' + def __init__(self): #This step Creates the Initial data (__init__(self) method calls it self) + self.number = 0 #This step creates a new attribute named 'number' and it's value is '0' + def some_function(self): #some_function is a method named 'Some Function' + print "I got called." #when calling to this method it prints 'I got called' + def add_me_up(self, more): #another method with a prameter 'more'. + self.number += more #calculate the addition of variables 'more' and 'number (in the initial)' + return self.number #returns the number calculated # two different things -a = TheThing() +a = TheThing() # 'a' and 'b' is a object of the class b = TheThing() -a.some_function() +a.some_function() # calling the methods inside the class individually b.some_function() -print a.add_me_up(20) +print a.add_me_up(20) # the number '20' is assigned to the variable 'more' in the 'add_me_up' method in the class print a.add_me_up(20) print b.add_me_up(30) print b.add_me_up(30) -print a.number +print a.number #prints the calculated Number print b.number diff --git a/sampledicts.py b/sampledicts.py index 9d9dcac..3ccef9e 100644 --- a/sampledicts.py +++ b/sampledicts.py @@ -1,23 +1,31 @@ # SAMPLE CODE TO UNDERSTAND THE CONCEPT OF DICTIONARIES IN PYTHON -cities = {'CA': 'San Francisco', 'MI': 'Detroit','FL': 'Jacksonville'} +#The pythond Dictionaries have two things, +#1st one is the key and secon one is the value +#The function of the dictionary is when calling the key in dic. it returns the value in the key + +#This is a Dictionary +cities = {'CA': 'San Francisco', 'MI': 'Detroit','FL': 'Jacksonville'} #Each key and value is separated by ':' +#Calling the key inside the dictionary and it returns the value belongs to the key cities['NY'] = 'New York' cities['OR'] = 'Portland' + +#This is a function 'themap' is the dictionary and 'state' is the key def find_city(themap, state): - if state in themap: + if state in themap: #if the 'state' is in the dictionary return the key's value return themap[state] else: - return "Not found." + return "Not found." #if the requested key is not in the dictionary it prints 'Not Found' # ok pay attention! -cities['_find'] = find_city +cities['_find'] = find_city #Assign the function in to a variable 'cities['_find']' -while True: - print "State? (ENTER to quit)", - state = raw_input("> ") +while True: #This is a loop wich run until user press enter + print "State? (ENTER to quit)", + state = raw_input("> ") # save user inputs into the 'state' variable if not state: break # this line is the most important ever! study! - city_found = cities['_find'](cities, state) - print city_found + city_found = cities['_find'](cities, state) #call the funtion written before + print city_found #print the returned value diff --git a/samplefile.py b/samplefile.py index 326d59a..2386c45 100644 --- a/samplefile.py +++ b/samplefile.py @@ -1,9 +1,9 @@ -from sys import argv -script , filename =argv -txt=open(filename) -print "here is your file %r" %filename -print txt.read() +from sys import argv #import some system libraries +script , filename =argv +txt=open(filename) #open the file +print "here is your file %r" %filename #print file name +print txt.read() #read the text inside the file print "i'll also ask you to type it again" -fi=raw_input("> ") -tx=open(fi) -print tx.read() +fi=raw_input("> ") #take a input (file name) +tx=open(fi) #open the file named as user input +print tx.read() #read the file data and print it diff --git a/samplefunc.py b/samplefunc.py index 998966e..46fe21a 100644 --- a/samplefunc.py +++ b/samplefunc.py @@ -1,8 +1,8 @@ -def multiply(a,b): - c=a*b - print "product is %r" %c -def add(a,b): - e=a+b - print "addition is %r" %e -multiply(3,4) +def multiply(a,b): #funtion named multiply with 'a' and 'b' parameters + c=a*b #calculation and assign the value in to variable c + print "product is %r" %c #print the calculated c value +def add(a,b): # functon named add with 'a' and 'b' parameters + e=a+b #calculation and assign th value into the variable 'c' + print "addition is %r" %e #print the calulated c value +multiply(3,4) #calling the funtions with a and b values add(3,4) diff --git a/samplegame.py b/samplegame.py index be0539b..66751ee 100644 --- a/samplegame.py +++ b/samplegame.py @@ -2,19 +2,19 @@ # Users are advised to go through this piece of code and dry run it to get an understanding of function calls. from sys import exit -def gold_room(): +def gold_room(): #Simple function with no parameters print "This room is full of gold.How much do you take?" next = raw_input("> ") - if "0" in next or "1" in next: - how_much = int(next) + if "0" in next or "1" in next: #check if '0' or '1' in the user input + how_much = int(next) #conver the user input number in to the integer format else: dead("Man, learn to type a number.") if how_much < 50: print "Nice, you're not greedy, you win!" - exit(0) + exit(0) #imported function else: dead("You greedy bastard!") -def bear_room(): +def bear_room(): #anothe Simple functon that can easyly undestand print "There is a bear here." print "The bear has a bunch of honey." print "The fat bear is in front of another door." @@ -40,11 +40,11 @@ def cthulu_room(): print "Do you flee for your life or eat your head?" next = raw_input("> ") if "flee" in next: - start() + start() #this line call for the start funtion in the below elif "head" in next: dead("Well that was tasty!") else: - cthulu_room() + cthulu_room() #this one call this function again (recursively) def dead(why): print why, "Good job!" diff --git a/sampleloops.py b/sampleloops.py index e55070d..7ed5589 100644 --- a/sampleloops.py +++ b/sampleloops.py @@ -1,13 +1,14 @@ -arr=[1,2,3] + +arr=[1,2,3] #this is a simplae list wich can consider as an array arrstr=["ashu" , "himu" , "piyu"] -for num in arr: +for num in arr: #take the each element in the arr list to the num variable print "numbers are %d" %num -for name in arrstr: +for name in arrstr: #take the each element in the arrstr list to the num variable print "names are %s" %name -arrinput=[] -for i in range(0,4): +arrinput=[] #take an empty list +for i in range(0,4): #loop in a selected range (i=0,1,2,3) print "enter element number %d" %i number = raw_input("> ") - arrinput.append(number) -for inp in arrinput: + arrinput.append(number) #save the each input value in to the empty list named arrinput +for inp in arrinput: #take the each element in the arrinput list to the num variable print "elements are %r" %inp