Monday 23 September 2013

working with file

#creating range

for item in range(0,100,5):
    print item
   
#working with file in python

crip=open('flash.txt','w')
crip.write('sing the melody')
crip.close()
#print crip

#reading a file
x=open('flash.txt','r')
y=x.read()
x.close()
print (y)

#creating my own file
good_day=open('rise.txt','w')
good_day.write('I enjoy my sunday very well with my sister on her birthday')
good_day.close()
#print good_day

happy=open('rise.txt','r')
well=happy.read()
happy.close()
print (well)

#appending a file
good_day=open('rise.txt','a')
good_day.write('The both of us went for service at my church')
good_day.close()
print good_day

happy=open('rise.txt','r')
well=happy.read()
happy.close()
print(well)




Friday 20 September 2013

Learning Python-
Intermediate
Lesson 4 :Iteration and Tuples
Carter Draper
Sources: Zane Cochran, Allan Martell & Code AcademyLet’s get ready!
 List Review
 Using Functions With Lists
 Using the Entire List in a Function
 For Loop
 While Loop
 TuplesFor Loops
For loops are traditionally used
when you have a piece of code
which you want to repeat n number
of times.
Executes a sequence of statements
multiple times and abbreviates the
code that manages the loop
variable.
for letter in 'Python': # First Example
print 'Current Letter :', letterrange vs. xrange in for
loops
The range function creates a list
containing numbers defined by
the input
The xrange function creates a
number generator
The range function generates a
list of numbers all at once, where
as xrange generates them as
needed
for a in range(10):
print (a)
for x in xrange(5):
print xAssignment
Lesson 4
For Loops
ExercisesWhile Loops
A while loop statement in Python
programming language repeatedly
executes a target statement as long as
a given condition is true.
It tests the condition before executing
the loop body.
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1Assignment
Lesson 4
While Loops
ExercisesTuples
A tuple is a sequence of
immutable Python objects. Tuples
are sequences, just like lists.
The only difference is that tuples
can't be changed i.e., tuples are
immutable and tuples use
parentheses and lists use square
brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);Accessing Values in Tuples:
To access values in tuple, use the
square brackets for slicing along with
the index or indices to obtain value
available at that indexAssignment
Lesson 4
While Loops
ExercisesReflection
Learning Python-
Intermediate
Lesson 3 : Lists & Functions
Carter Draper
Sources: Zane Cochran, Allan Martell & Code AcademyLet’s get ready!
 List Review
 Function Review
 Using Functions With Lists
 Using the Entire List in a Function
 Lists of ListsList Review
 Indexing an element in a list
 x[n]
 Modifying an element in a list
 x[n] = v
 Appending an element to a list
 x.append(item)List Review
3 Ways to Remove Elements from
Lists
 Remove
 n.remove(item) will remove the actual item if it
finds it.
 Del
 del(n[1] is like .pop in that it will remove the item
at the given index, but it won't return it.
 Pop
 n.pop(index) will remove the item at index from
the list and return it to you.Assignment
Lesson 3
List Review
Exercise 0-3Function Review
 Function Syntax
def test(z):
return z + 1
print test(5)
⇒ Declare Function
⇒ Return Value
⇒ Call Function
 Unknown Number of Arguments
def test2(*args):
print args[0]
⇒ gets all arguments
⇒ prints first argumentAssignment
Lesson 3
Function Review
Exercise 0-2Using Functions With Lists
 You can pass lists to
functions just like any
other argument.
 You can modify elements
in a list while in a function.
 You can also add and
remove elements onto a
list inside a function
 Functions can return single
elements from the list or
the entire list.
my_list= [ 8, 2, 1, 9 ]
def test1(a_list):
return a_list [1]
test1(my_list)
def test2(a_list):
a_list[1] += 1
return a_list
test2(my_list)Assignment
Lesson 3
List Functions
Exercise 0-2Using the Entire List in a
Function
 We’ve changed or added
or removed ONE element
in a list.
 How could we change
multiple elements or all
the elements?
 We can use a for loop and
the range function.Using the Entire List in a
Function
 range( )
 A shortcut for
generating a list
 Can take 3 different
arguments
depending on what
you want it to do
1 Argument
Starts at 0
Increases by 1 until 1 less than 1st argument
range(2) ⇒ [0, 1]
2 Arguments
Starts at 1st argument
Increases by 1 until 1 less than 2nd argument
range(1,3) ⇒ [1, 2]
3 Arguments
Starts at 1st argument
Increases by 3rd argument until less than
2nd argument
range(2,8,3) ⇒ [2,5]Using the Entire List in a
Function
 range(3)
 range(0)
 range(0, 5)
 range(2, 4)
 range(2, 5, 2)
 range(1, 12, 3)
[0, 1, 2]
[]
[0, 1, 2, 3, 4]
[2, 3]
[2, 4]
[1, 4, 7, 10]Using the Entire List in a
Function
 Let’s pretend we have a
list [3, 5, 7]
 How could we find out the
result of all the items in
the list added together?
 What if we didn’t know
how many items were in
the list?
# The Hard Way
n = [3, 5, 7]
count = n[0] + n[1] + n[2]
print count
# The Easy Way
def total(a_list):
sum = 0
for item in a_list:
sum += item
return sumAssignment
Lesson 3
Entire List
Exercise 0-4Lists of Lists
 We know functions can take two arguments
 What types of things could we do if we gave it
two lists?
 farmA = [“sheep” , “chicken”, “cow”]
 farmB = [“goat”, “horse”, “duck”]
 We could write a function to combine these lists!
 You can combine lists like this: farmA + farmBLists of Lists
 We’ve looked at lists of numbers and lists of
strings
 What about lists of lists?
List of Lists
List 1 , List 2 , List 3
-----------------------------------------------
warehouse = [ [1, 2, 3], [4, 5, 6, 7], [8, 9]
]Assignment
Lesson 3
List Lists
Exercise 0-2Quick Look & Reflection
 Are you becoming more comfortable with using lists
and functions?
 What has been your favorite thing so far?
 When would it make sense to have a list of lists?

working with loop and while loop

#for loop
for letter in 'python':
    print 'current letter:',letter

#creat a list of five colors
colors=['red','blue ','green','yellow','orange']
print colors

for green in 'favorite colors':
   print 'favorite colors:',colors

#range
for a in range(20):
    print(a)

#using xrange
for x in xrange(20):
    print x

#using odd number in xrange
for x in xrange(30):
    if x%2==0:
        continue
print x

#using even number in xrange
for w in xrange(20):
    if w%2!=0:
        continue
    print w

#counting from 500,600
for x in xrange(500,600):
    print x

#working with while loop
count=0  
while(count<9):
    print 'the count is:', count
    count=count+1

#using while loop
add=11
while(add<35):
    print 'this is great then :',add
    add=add+1
#

  
  
  

Wednesday 18 September 2013

Intermediate list function

INTERMEDIATE LIST FUNCTION

#creating list
continent=["asia","europe","africa"]
print continent


#indexing a list
earth=continent[2]
print earth

#modifying a list
continent[2]='australia'
print continent

#appending a list
continent.append('america')
print continent

#deleting using pop
continent.pop(1)
#deleting using remove
continent.remove('america')
print continent

#deleting using del
del(continent[0])
print continent


#function review
girls=3
def caculation(x):
    return x*4
print caculation(girls)

#multiple argument
liberia=['buchanan','greenville','robertsport','kakata','zwedra','monrovia']
print liberia

#placing list into function
def visit(k):
    return k
print visit(liberia)

#printing one item from list in funx
def home(new_list):
    return new_list[2]
print home(liberia)

#deleting in function
def mon(galaxy):
    galaxy.pop(4)
    return galaxy
print mon(liberia)

#adding in funx
def sun(light):
    light.append('gbanga')
    return light
print sun(liberia)

#counting in funx
count=[1,2,3,4,5,6,7,8,9,10]
def join_list(word,number):
    return word+number
print join_list(liberia,count)

#loop list using for in funx
for i in count:
    print i

for k in liberia:
    print k

for i in count:
    print i+1

#adding a new list in funx
streets=[[1,2,3,],[6,8,7,9]]
def shine(m):
    result=sum(m,[])
print shine(streets)


   




Monday 16 September 2013

Assignment

#create a list called continent
continent=['Africa','Antarctica','Asia','Australia','Southamerican','Nouthamerican','Europe']
print continent

#print only the five continent
print continent [5]

#update the continent on the list by adding two planet: 'earth' and 'mars'
continent.append("earth")
print continent

continent.append("Mars")
print continent

#4
continent[3]="pluto"
print continent

#5
print len("continent")

#6
galaxy=continent[0:5]
print galaxy

space=continent[5:9]
print space

#7
continent.insert(4,"mercury")
print continent

#8
for item in continent:
    print continent

#9
continent_africa=['liberia,''guinea','ghana','congo','sierrialeon','zimbobwe','libya','togo','egypt','tunisia','somalia','algerie','benina','mila','niger','kenya','gabon','chad','nigeria']
print continent_africa

for counties in continent_africa:
  print continent_africa
 
#10
space.pop(2)

#part two
liberia={'grandcapemount':'via','bong':'kpelle','grandgedeh':'Kremlin','grandbassa':'bass a','grandkru':'kru','maryland':'gebo','grandgee':'gerbo','sinoe':'sarpo','nimba':'mano'}
print liberia

for counties in liberia:
    print liberia

#2
print Liberia["nimbi"]

#3
Liberia["fellah"]='Norman'
print Liberia

#4
street={}
print street

#5
street['Benson']=1
street['crown hall']=4
street['front']=7
street['waterside']=5
street['colemanhall']=3
print street

#6
del(liberia['maryland'])
print liberia

#7
street['draperline']=[8,3,9,11,2]
print street

#8
street['draperline'].sort()
print street

#9
house_thing={'table':'it is where we put food','pan':'is where we eat','pot':'is where we cook our food','chair':'is where we sit','bed':'is where we sleep'}
print house_thing

#10
print house_thing
  
print liberia

Friday 6 September 2013

working with dictionary

# dictionary
school={'techers':7,'students':20,'janitor':1,'cook':3,'principal':1,'cashier':2}
print school

#updating a dictionary
school["sponsor"]=6
print school

#printing key and values in a dict
print school.keys()

#print value of a key
print school['students']

#deleting an item from diet
del(school['janitor'])
print school
print len(school)

#empty diet
menu={}
menu['wine']=21.50
menu['rice']=12.5
menu['fufu']=13
menu['peza']=24.6
menu['cake']=3.00
menu['frie chicken']=5.00
menu['softdrink']=35.00
menu['cornbread']=45.00
menu['dosa']=16.00
print menu

for item in menu:
    print menu[item]
for key,value in menu.items():
    print key,value

citizens={'liberia':'liberians','Nigeria':'nigerians','Americans':['chinese','europeans','africans','asians']}
print citizens

#using for loops
for key,value in citizens.items():
    print key,value
citizens['Americans'].sort()
print citizens

citizens['brazil']=['brazilians','south american']
print citizens