Skip to content

Comments added #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions sampleclass.py
Original file line number Diff line number Diff line change
@@ -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'.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error , it should be "parameter".

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
26 changes: 17 additions & 9 deletions sampledicts.py
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be "python" and not "pythond".

#1st one is the key and secon one is the value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using short forms (like "secon" etc)

#The function of the dictionary is when calling the key in dic. it returns the value in the key
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frame the sentences properly without using short forms.


#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
16 changes: 8 additions & 8 deletions samplefile.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions samplefunc.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 7 additions & 7 deletions samplegame.py
Original file line number Diff line number Diff line change
@@ -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!"
15 changes: 8 additions & 7 deletions sampleloops.py
Original file line number Diff line number Diff line change
@@ -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