Wednesday, 11 February 2015

printing triangle pattern


       

word="ayman"
a="*"
c="h"
d="i"
n=0
m=0
while n<10:
    n=n+1
    print(c,end='')
    print(d,end='')
    print(a,end='')
    for b in word:
     print(b,end='')       
    print(a)
    m=0
    print(c,end='')
    while m<=n:
        print(d,end='')
        m=m+1
        
while n>=0:
      n=n-1
      print(a,end='')
      for b in word:
          print(b,end='')
      print(a)
      m=0
      print(c,end='')
      while m<n:
           m=m+1
           print(d,end='')
      print(d,end='')  
        
    

Monday, 2 February 2015

python 3.4.2 part 13 write and read a text file

First create file "readwritefile.py"

/****************************/


       

fw=open("sample.txt","w")
fw.write("i like to swim")
fw.close()
fr=open("sample.txt","r")
textcontent=fr.read()
print(textcontent)
fr.close()

/*********************/
1.fw is variable, it just indicate file write, you can have your own variable
2.fr is variable, it just indicate file read, you can have your own variable
3.open("sample.txt","w"). it create a text file named "sample.txt"
4.and the attribute "w" , w is predefined attribute indicating writing permission
5.and the attribute "r" , w is predefined attribute indicating reading permission
6.close() is used to avoid wastage of memory

python 3.4.2 part 12 downlaoding an image


First install beautifulscraper

pip install beautifulscraper

 

create a new file downimage.py


/******************************/
       

import urllib.request
import random
def download_image(url):
    file_name=random.randrange(1,1000)
    full_file_name=str(file_name)+".jpg"
    urllib.request.urlretrieve(url,full_file_name)
download_image("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg")

/***************************************/
run it
1.random is used select a random number
2.first we create download_image(url) function
3.to give a name for downloaded image, we use random function
4.str(file_name) is used to convert  "file_name( number )" to string
5.urllib.request.urlretrieve is a function used to download the image
6."https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg" this url will pass to def download_image(url):



python 3.4.2 part 11 executing a function from other fileStudent code


Create two files







1.teacher.py
2.student.py




student.py
/******************/
def john():
    print("my name is john")


teacher.py
/*****************/

import student

student.john()






Sunday, 1 February 2015

python 3.4.2 part 10 Add two numbers using def function

File ->new file

copy below code


/********************************/
       

def add_numb(*args):
total=0
for x in args:
total=+x
print(total)
add_numb(3)
add_numb(3,5,6)



/************************************/
then save it
Run->Run module(press f5)