Friday, January 27, 2017

Vowel or Consonant Python

Following code will identify that entered character is a vowel or consonant.

Code::

def vowel(str):
    if(str=='a' or str=='e' or str=='i' or str=='o' or str=='u' or str=='A' or str=='E' or str=='I' or str=='O' or str=='U'):
        print("Vowel")
    else:
        print("Consonant")
while True:                                                #for only one character 
    
    a=input("Enter a Character: ")
    if(len(a)==1):
        break
    else:
        print("Try Again!!")
        continue
vowel(a)
    

Output:



Length of a String in Python

Following code is used to calculate length of a string using user defined length function :

Code::

def length(l):
    count=0
    for j in l:
        count=count+1
    return count
    
a=input("Enter String:")
c=length(a)
print("Length is :",c)

Output:


Greater of three Number in Python


Functions are defined in python using def keyword .Following will illustrate the concept.

Code::

def max(a,b,c):
    if(a>b and a>c):
        print ("Greater is :",a)
    elif(b>c and b>a):
        print("Greater is : ",b)
    else:
        print("Greater is :",c)
        
     
a=input("Enter First No:")
b=input("Enter Second no.:")
c=input("Enter Third no:")

max(a,b,c)


Output:



List and Tuple Datatype in Python

List and Tuple Both are datatypes in Python .These are similar to each other and used to store data of different types.But the only difference in list and tuple is that we can edit data in list but we can not edit data in tuple


For Example:

List =['76','23','25','85']

Tuple=('75','6','7')


Question:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')



Code::

var=input("Enter Values Using Comma ',':")
n=var.split(",") # for constructing list from sequence
d=tuple(n)        # for constructing tuple

print ("List is :",n)
print ("Tuple is :",d)



Output:




Thursday, January 26, 2017

Dictionary datatype Python

Question:
With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.

Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}


Code::


dict={}              #dict is of type dictionary
i=input("Enter any Number:")
for j in (range(1,i+1)):
 dict[j]=j*j
print(dict)


Output:


Factorial of a Number Python

Following Code will Calculate the factorial of a number :

Code::

fact=1
i=input("Enter Any Number:")
for j in (range(1,i+1)):                #range function for iteration from 1 to i+1
 fact=fact*j

print(fact)



Note:   Take care of the space in the code which leads to formation of blocks

Output:

National Flag ("Tiranga") Using Java

Following Code will create Our National Flag("Tiranga") using Java  Applet 

Code::

import java.applet.*;
 import java.awt.*;


public class Republic extends Applet 
{

   public void paint(Graphics fl)
   {    Color c1=new Color(255,140,0);   //saffron color
        Color c2=new Color(139,0,0);     // dark red color
       //for pole
       fl.setColor(c2);
       fl.fillRect(250,100,5,400);
       fl.setColor(Color.black);
       fl.drawRect(250,100,5,400);
       //for saffron
       fl.setColor(c1);
       fl.fillRect(255,102,180,40);
       fl.setColor(Color.black);
       fl.drawRect(255,102,180,40);
       //for white
       fl.setColor(Color.WHITE);
       fl.fillRect(255,142,180,40);
       fl.setColor(Color.black);
       fl.drawRect(255,142,180,40);
       //for green
       fl.setColor(Color.GREEN);
       fl.fillRect(255,182,180,40);
       fl.setColor(Color.black);
       fl.drawRect(255,182,180,40);
       // for background
       Color c4= new Color(173,216,230);
       setBackground(c4);
       // for stairs
      int j[]={250,245,245,225,225,280,280,260,260,255};
      int k[]={500,500,505,505,515,515,505,505,500,500};
      fl.setColor(c2);
      fl.fillPolygon(j,k,10);
      fl.setColor(Color.BLACK);
      fl.drawPolygon(j,k,10);
      // for ashok chakra
      fl.setColor(Color.blue);
      fl.drawOval(325,142,39,39);
      // for lines in ashok chakra
       int n1=345;               
       int d1=162;
       double n2,d2;

        double angle= 0.0;    //for angle determination

        double line=0.0;

            

             int r=20;

             for(int i=1;i<=24;i++)
                {
                         angle=(double)line*(3.14/180);

                          n2=n1+(double)r*Math.cos(angle);

                          d2=d1+(double)r*Math.sin(angle);

                         fl.drawLine(n1,d1,(int)n2,(int)d2);   //drawing line between (n1,d1) and (n2,d2)

                        line=line+(360/24);
               }
             // for text
             Font f=new Font("Arial",Font.BOLD,36);
             fl.setFont(f);
             Color c3=new Color(0,100,0);
             fl.setColor(c3);
             fl.drawString("Happy Republic Day",240,595);
             
   }
    

}

Output:

Happy Republic Day To All !!!