Total Pageviews

Monday 1 June 2015

BASICS OF PYTHON SCRITPTING

 PYTHON SCRIPTING:



 PYTHON IS VERY EASY BUT WE NEED TO UNDERSTAND THE LOGIC AND JUST IMPLEMENT IT.

WRITE A SAMPLE PROGRAM TO PRINT HELLO WORLD USING PRINT AND ECHO STATEMENT.
(COMMENT:-PRINT AND ECHO STATEMENTS ARE JUST LIKE PRINTF IN C,COUT<< IN C++.)

print "HELLO WORLD";
echo "WELCOME TO PYTHON SCRIPTING";


OUTPUT:-python   hello.py


HELLO WORLD
WELCOME TO PYTHON SCRIPTING



PRINT AND ECHO ARE USED FOR THE SAME PURPOSE(I.E., PRINT SOME THING ON SCREEN LIKE SOME SOME MEASSAGE,VARIABLES.)




Line indendations are special feature of python,

suppose 
if  condition {
some statements;
}
These is the syntax in another programming languages.

But in case mof python there will be no  flower braces ....

so the  scope is determined by line indendations


if a>b
<-tab space->print("a is greater");

Thus scope in python is determined by line which represent bygiving tab space.



*WRITE A  PYTHON SCRIPT TO PRINT BIGGEST OF THREE NUMBERS.

COMMENT:
IN THIS SCRIPT  "raw_input" IS GIVEN TO ASSIGN "enter x:" TO THE VARIABLE X

THIS IS SIMILAR TO Y AND Z ALSO IN PYTHON ELSEIF IS USED BUT IN AN DIFFERENT FORMAT LIKE elif



x = float(raw_input("Enter first number: "));
y = float(raw_input("Enter second number: "));
z = float(raw_input("Enter third number: "));

if (x > y) and (x > z):
   print"x is largest";
elif (y > x) and (y > z):
    print "y is largest";
else:
    print "z is largest";



OUTPUT:python bigger.py
           
       enter x:9
       enter y:8
       enter z:7
       x is biggest

       enter x:7
       enter y:9
       enter z:8
       y is biggest.

       enter x:7
       enter y:8
       enter z:9
       z is biggest



BUt in python 3.1  version and higher versions
raw_input gives an error that name raw _ input is not defined.

Then follow these syntax:

raw_input = vars(__builtins__).get('raw_input',input);




EXAMPLE PROGRAM TO PERFORM GREATEST AMONG TWO NUMBER WITH VARIABLE RAW_INPUT DEFINED.


raw_input = vars(__builtins__).get('raw_input',input);
a=input(raw_input("enter a"));
b=input(raw_input("enter b"));
c=input(raw_input("enter c"));
if a>b and a>c:
 print("a is greater");

elif b>c:
 print("b is greater");

else:
 print("c is greater");

OUTPUT:


*WRITE A PYTHON SCRIPT TO PRINT MULTIPLICATION OF TWO NUMBERS.


x = int(raw_input("Enter first number: "));

y = int(raw_input("Enter second number: "));
c=x*y;
print("multiplication of two numbers is:"c);


OUTPUT: python mul.py
Enter first number:3
Enter second number:5
multiplication of two numbers is:15



*program regarding multipliaction of two numbers  in python 3.1.

raw_input = vars(__builtins__).get('raw_input',input);
a=int(raw_input("enter a"));
b=int(raw_input("enter b"));

c=a*b;
print("mul of two numbers:",c);


save it as: d.py

open->cmd->browse to python then from there to your folder.

c:\python\sample>python d.py

output:


To take input of type int use the  following syntax:

raw_input=vars(__builtins__).get('raw_input',input);

variable name=int(raw_input("enter some value:"));


similarly for float:

variable name=float(raw_input("enter some value:"));


*WRITE A PYTHON SCRIPT TO PRINT DIVISION OF TWO NUMBERS WITH ITS QUOTIENT AND REMAINDER.


x = int(raw_input("Enter first number: "));

y = int(raw_input("Enter second number: "));
c=x/y;
d=x%y;
print("division of two numbers quotient is:",c);
print("division of two numbers remainder is:",d);

OUTPUT: python div.py
Enter first number:15
Enter second number:5
division of two numbers quotient is:3
division of two numbers remainder is: 0





program to perform division of two numbers in python 3.1




raw_input=vars(__builtins__).get('raw_input',input)
a=int(raw_input("enter a"))
b=int(raw_input("enter b"))

c=a%b
print("mul of two numbers:",c)

output:



In these pyhton programming the (;)semi colon(i.e., delimiter is not so important)

The main thing in python is line indendation...So careful with indendations
if    :
  print
elif    :
 print
else:
 print