Python Code

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.
Python shell can be used in two ways, viz., interactive mode and script mode.
Interactive Mode: allows us to interact with OS.
When we type Python statement, the interpreter displays the result(s) immediately.

Script Mode: In script mode, we type Python program in a file and then use interpreter to execute the content of the file. Working in interactive mode is convenient for beginners and for testing small pieces of code, as one can test them immediately. But for coding of more than few lines, we should always save our code so that it can be modified and reused. Python, in interactive mode, is good enough to learn, experiment or explore, but its only drawback is that we cannot save the statements and have to retype all the statements once again to re-run them. 

Software Use-IDLE (Python 3.6.0)
Download link of latest version of Python 
https://www.python.org/downloads/



Python Program to print sum of two number.


a=9

b=8

sum=a+b

print(sum)



output-

17



Python Program to take two integer as input from user and print sum.


x=int(input("enter any no."))
y=int(input("enter another no."))
sum=x+y
print("sum=",sum)


Output-


Python Program to print number from 0 to 10.


count = 0
while (count <= 10):
   print('The count is:', count)
   count = count + 1

print("Good bye!")


Output-



Python program to check whether entered no. is even or odd.  


x=int(input("Please Enter a Number : "));  
if(x%2==0):  
    print("This Number is Even")  
else:  
    print("This Number is Odd")  

Output-

Python program to print first Prime no. within a given range.


x=int(input("Enter upper limit: "))
print("Prime numbers are ")
for a in range(2,x+1):
    k=0
    for i in range(2,a//2):
        if(a%i==0):
            k=k+1
    if(k<=0):
        print(a)

Output-

Python program to find largest among the three numbers.


x=int(input("enter first no. "))
y=int(input("enter second no. "))
z=int(input("enter third no. "))

big=x
if(y>big):
      big=y
if(z>big):
      big=z

print("Largest number is",big)


Output-



Python program to find the simple interest based upon number of years. If number of years is more than 12 rate of interest is 10 otherwise 15.


p = int(input("Enter any principle amount "))
t = int(input("Enter any time "))
if (t>10):
 si=p*t*10/100
else:
 si = p*t*15/100
print("Simple Interest = ",si)


Output-



Python program to input any choice and to implement the following.
 1. Area of square
 2. Area of rectangle
 3. Area of triangle 


print("choice \n 1.area of square \n 2.area of rectangle \n 3.area of triangle  ")
c = int(input ("Enter any Choice " ))
if(c==1):
 s = int(input("enter any side of the square "))
 a = s*s
 print("Area = ",a)
elif(c==2):
 l = int(input("enter length "))
 b = int(input("enter breadth "))
 a = l*b
 print("Area = ",a)
elif(c==3):
 x = int(input("enter first side of triangle "))
 y = int(input("enter second side of triangle "))
 z = int(input("enter third side of triangle "))
 s = (x+y+z)/2
 A = ((s-x)*(s-y)*(s-z))**0.5
 print("Area=",A)
else:
 print("Wrong input")


Output-





Python program to input any string and count number of uppercase and lowercase letters.


s=input("Enter any String ")

u=0
l=0
i=0
while i<len(s):
   if (s[i].islower()==True):
    l+=1
   if (s[i].isupper()==True):
    u+=1
   i=i+1
print("Total upper case letters :", u)
print("Total Lower case letters :", l)


Output-


Python program to demonstrate the use of list.

list is a data structure in python.
The list is a most versatile datatype available which can be written as a list of comma-separated values (items) between square brackets. 
Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.


list1 = ['C', 'M', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
list3 = ["duke","lucy","will", 4, 5, 6, 7 ];
print("list1[0]: ", list1[0])
print("list2[1:5]: ", list2[1:5])
print("list3[1:5]: ", list3[1:5])
#updating in list
list1[2]='mill'
list1[1]='1997'
print("list1 after updating")

print("list1: ", list1)



Output-



Basic List Operations



list1 = ['C', 'M', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
list3 = ["duke","lucy","will", 4, 5, 6, 7 ];
print("list1[0]: ", list1[0])
print("list2[1:5]: ", list2[1:5])
print("list3[1:5]: ", list3[1:5])
#updating in list
list1[2]='mill'
list1[1]='1997'
print("list1 after updating")
print("list1: ", list1)
print("length of list1=", len(list1))
newlist=list1+list2
print("newlist=",newlist)
print("3*list1=",3*list3)
t=3 in list2
print("3 in list2 is",t)


Output-

Build-in List function
len(list)  Gives the total length of the list.
max(list)Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(seq)  Converts a tuple into list.
cmp(list1, list2)Compares elements of both lists.


Function

A function is a block of statements, reusable code that is used to perform any specific action.
Functions provide better modularity.
High degree of code reusing 


Python program to impediment simple function.

def simple():    print("simple function ")

simple()

 

Output-




Python program to input any choice and to implement the following using user define function for each case.
 1. Area of square
 2. Area of rectangle
 3. Area of triangle 





def square():

 s = int(input("enter any side of the square "))

 a = s*s

 print("Area = ",a)



def rectangle():
 l = int(input("enter length "))
 b = int(input("enter breadth "))
 a = l*b
 print("Area = ",a)

def triangle():
 x = int(input("enter first side of triangle "))
 y = int(input("enter second side of triangle "))
 z = int(input("enter third side of triangle "))
 s = (x+y+z)/2
 A = ((s-x)*(s-y)*(s-z))**0.5
 print("Area=",A)



def main():
 print("choice \n 1.area of square \n 2.area of rectangle \n 3.area of triangle  ")
 c = (input ("Enter any Choice " ))
 if(c=='1'):
    square()
 elif(c=='2'):
    rectangle()
 elif(c=='3'):
    triangle()
 else:
  print("Wrong input")
  main()

main()





Output-




Python program to find factorial of number using user-define function.



def fact(x):
    f=1
    for i in range(1,x+1):
        f=f*i
    return f

x=int(input("Enter any value "))
print("Factorial=",fact(x))



Output-


Python program to check whether entered number is armstrong number or not.



#armstrong number is
# 153=1*1*1+5*5*5+3*3*3


def checkamg(x):
    sum=0
    while(x!=0):
        t=x%10
        sum=sum+t*t*t
        x//=10
    return sum


n=int(input("Enter any number "))

if(n==checkamg(n)):
    print("number is Armstrong numbers")
else:
    print("number is not Armstrong numbers")
    
      
Output-




Module

It is a file consisting of Python code.
It can define functions, classes and variables. 
A module can also include runnable code.



Python program to implement module.



def simple(p):

    print("hellow:",p)

    return



simple module, simple.py

Now we can use this Python source file as a module by executing an import statement in some other Python source file.


import simple
#F:\python\modules
simple.simple("john")


when above code will execute it will produce following output-

hellow: john 


Python Program to print fibonacci series to nth term.


def fibo(x):
    a=0
    b=1
    sum=1
    i=1
    while(i<=x):
        print(" ",sum)
        sum=a+b
        a=b
        b=sum
        i=i+1
    return


x=int(input("enter any no. "))
if(x<0):
      print("please enter +ve no.")

elif(x==0):
      print("fibonacci series is 0")
      
else:
      print("fibonacci series is")
      fibo(x)


Output-





Python Program to define a simple class.


class student:
   'Common base class'
   stuCounte = 0

   def __init__(s, name, rno):
      s.name = name
      s.rno = rno
      student.stuCounte += 1

   def displaystudent(self):
      print("Name : ", self.name,  ", rno: ", self.rno)


stu1 = student("John", 32)
stu2 = student("duke", 16)
stu1.displaystudent()
stu2.displaystudent()
print("no. of students=",student.stuCounte)


Output-




Python Program to define class student and create a functions for getting marks marks from user and display details.



class student:
   'Common base class'
   stuCounte = 0
   m_phy=0
   m_che=0
   m_maths=0



   def __init__(s, name, rno):
      s.name = name
      s.rno = rno
      student.stuCounte += 1

   def displaystudent(self):
      print("Name : ", self.name,  ", rno: ", self.rno)
      print(" marks \n Physics-",self.m_phy,"\n chemistry-",self.m_che,"\n maths-",self.m_phy,"\n")
      

      
   def getmarks(self):
       print("\nEnter the marks of student ",self.name)
       self.m_phy=float(input("Enter the marks in physics "))
       self.m_che=float(input("Enter the marks in chemistry "))
       self.m_maths=float(input("Enter the marks in maths "))
       

stu1 = student("John", 32)
stu2 = student("duke", 16)
stu3=student("fl",12)

stu1.getmarks()
stu2.getmarks()
stu3.getmarks()

stu1.displaystudent()
stu2.displaystudent()
stu3.displaystudent()

print("no. of students=",student.stuCounte)



Output-



Python Program to define class student and create a functions for getting marks marks from user and display details. Also calculate percentage and display details of student having maximum percent.



class student:

   'Common base class'

   stuCounte = 0

   m_phy=0

   m_che=0

   m_maths=0

   def __init__(s, name, rno):

      s.name = name

      s.rno = rno

      student.stuCounte += 1


   def displaystudent(self):

      print("\nName : ", self.name,  ", rno: ", self.rno)

      print(" marks \n Physics-",self.m_phy,"\n chemistry-",self.m_che,"\n maths-",self.m_maths)

      print("Percentage=",self.cal_per())

            

   def getmarks(self):

       print("\nEnter the marks of student ",self.name)

       self.m_phy=float(input("Enter the marks in physics "))

       self.m_che=float(input("Enter the marks in chemistry "))

       self.m_maths=float(input("Enter the marks in maths "))


   def cal_per(self):

        per=(self.m_phy+self.m_che+self.m_maths)/3

        return per

    

stu1 = student("John", 32)

stu2 = student("duke", 16)

stu3=student("fl",12)

big=student(" ",0)


stu1.getmarks()

stu2.getmarks()

stu3.getmarks()

big=stu1

if(big.cal_per()<stu2.cal_per()):

    big=stu2

if(big.cal_per()<stu3.cal_per()):

    big=stu3


print("student with maximum percent ")

big.displaystudent()



Output-



Comments

  1. Excellent post. You have shared some wonderful tips. I completely agree with you that it is important for any blogger to help their visitors. Once your visitors find value in your content, they will come back for more How to Run Python Program In Script Mode



    ReplyDelete

Post a Comment

Popular posts from this blog

Perl

C++ code