-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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) |
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 |
There was a problem hiding this comment.
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".