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)







Friday, 30 January 2015

Python 3.4.2 part 2.1 printing hello tutorial

  1. cmd

       

cd /python34
python helloworld.py



  1. Notepad

       
       

print("helloworld")
  1. cmd

       
       
       

python helloworld.py








Sunday, 25 January 2015

Python 3.4.2 Part 5 Using input tutorial

>>> x=input("Enter a string")
Enter a string ayman
>>> x
' ayman'
>>> y=input("Enter your name: ")
Enter your name: Ayman
>>> x
' ayman'
>>> y
'Ayman'
>>> x=input("Enter a string")
Enter a string5
>>> x
'5'
>>> y=int(input("Enter a number"))
Enter a number8
>>> y
8
>>> y+int(x)
13



Now why we got an error in y+x??
please comment the reason

Python 3.4.2 Part 4 Using Variables tutorial

Storing a number to x


       

x=10
        
    


       

x+20
        
    


Storing string

       

x="mams"
        
    


       

x+" "+"Python"
        
    


Now adding a number with string variable must type cast to int
eg
x=100
y="10"
to add x and y we should convert y to a number

type casting (Converting string to number)

       

x+int(y)
        
    



python 3.4.2 Part 3 maths tutorial

To Add two number simply type

       

5+8
        
    

To Subtract
       

5-9
        
    
To Divide
       

8/3
        
    


To Mutiplication

       
8*9
        
    


To get power of X^n
eg: 2^50
       

2**50
        
    

or
       

pow(2,50)
        
    


Now check these examples

1.8/3

2.8//3 (check the decimals)

3.3+4+5*9+8/4 (order is PEMDAS, if you dont know this check it google)

Python 3.4.2 part2 printing hello tutorial

Goto start menu then click python command line
or 
Goto start menu then click python GUI


now type
       

print ("hello")
        
    

press enter


out put

Python 3.4.2 part1 installation tutorial

1.Goto this https://www.python.org/downloads/ site and download python 3.4.2 or for windows click this

https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi





2.Install it