Cse2k16.files.wordpress.com



INSTALLATION:Here are the steps to install Python on Windows machine.Open a Web browser and go to the link for the Windows installer of Python 2.7 seriers or python 3.1 series Save the installer file to your local machine and then run it to find out if your machine supports MSI.Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished.Exercise 1 - BasicsAim: To run python instructions in interactive and Script modeINTERACTIVE MODE:When commands are read from a terminal (ex:-keyboard), the interpreter is said to be in?interactive mode. In this mode it prompts for the next command with the?primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the?secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:Consider a command that print welcome message. Type the command at the interactive mode and press EnterThis produces the following result:Input and output are distinguished by the presence or absence of prompts (i.e.,>>>?). Lines that begin with prompt indicate input mode where you must type everything after the prompt. Lines that do not begin with a prompt are output from the interpreter.SCRIPT MODE:Programs cannot be edited or saved for future purpose in Interactive mode. For this, you’ll need an editor window. Running programs through editor window is known as Script Mode.To execute a script follow the below steps:From the File menu of the output window select New File. A second window will pop open. It will not contain a cursor. This is a simple text editor. It will not interpret lines as you type them. The edit window is where you will normally write your programs In Editor Window type the program to be executed .For example consider a program that displays a welcome messageGo to File Menu option and then Click on Save.Save the program with .py extension Ex:-First.py and then click OKTo Run the script Click on Run and then Click on Run Module or press F5After Program is executed ouput is displayed as b) Aim: To calculate Biggest of three numbers using nested if-else as an example to raise Indentation Error and Correct it. Program :With Indentation Error:a=input("Enter value of a: ")b=input("Enter value of b: ")c=input("Enter value of c: ")if(a>b): if(a>c): print a," is Biggest number" else: print c," is Biggest number"else: if(b>c): print b," is Biggest number" else: print c,"is Biggest number"OUTPUT:WITHOUT INDENTATION ERROR:a=input("Enter value of a: ")b=input("Enter value of b: ")c=input("Enter value of c: ")if(a>b): if(a>c): print a," is Biggest number" else: print c," is Biggest number"else: if(b>c): print b," is Biggest number" else: print c,"is Biggest number"OUTPUT:Exercise 2 - OperationsAIM: program to compute distance between two points taking input from the userPROGRAM:Aim: To write a program add.py that takes 2 numbers as command line arguments and prints its sum.Program:import sysa=int(sys.argv[1])b=int(sys.argv[2])c=a+bprint “Sum of “,a,”and”,b,”is =”,cOutput:Exercise - 3 Control FlowAim: Program for checking whether the given number is a even number or notPROGRAM:a=input("Enter a value:")if a%2==0: print a,"is even number"else: print b,"is an odd number"OUTPUT:b) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . ,1/10SEQUENCE:In?Python,?sequence?is the generic term for an ordered set. There are several types of?sequences in Python, the following three are the most important. Lists are the most versatile?sequence?type. The elements of a list can be any object, and lists are mutable - they can be changed.AIM: program using a for loop that loops over a sequence and calculates squares of numbers in list. PROGRAM:n= input("Enter length of list:")digits=[]for i in range(n): k=input("Enter a number:") digits.append(k)print "Squares of numbers in list are:"for i in digits: print(i*i)else: print("No items left.")OUTPUT:d) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.Exercise 4 - Control Flow Aim: Program to calculate the sum of all the primes below two million.Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...Program:n=input("Enter length of Series:")sum=0a=1b=2for i in range(2,n+1): c=a+b count=0 if(c<=2000000): for j in range(1,c+1): if(c%j==0): count=count+1 continue if(count==2): print c sum=sum+c a=b b=c else: print"Value exceeding 2 million"print "Sum of primes in Fibnocci Series is:",sumOutput:Aim:Program to calculate sum of even values terms in considering the terms in the Fibonacci sequence whose values do not exceed four millionProgram:Exercise - 5 - DSAIM: program to count the numbers of characters in the string and store them in a dictionary data structurePROGRAM:str1=raw_input("Enter a String:")L=len(str1)print"The length of the given string is:",Ldict1={str1:L}print"The new dictionary is:",dict1OUTPUT:AIM: program to use split and join methods in the string and trace a birthday with a dictionary data structure.Program: Date=raw_input("Enter a Birthday in dd-mm-yyyy format::")L1=Date.split('-')print "list1 is =",L1D1="Date"M1="Month"Y1="year"S=[D1,M1,Y1]print "List2=",SD=dict(zip(S,L1))print "Generated Dictionary is :",Dprint (D)OUTPUT:Exercise - 6 DS AIM: program combine_lists that combines lists into a dictionary.PROGRAM:L1=[]L2=[]N=int(input("Enter the range for 1st list:"))print("Enter the elements of 1st list:")for i in range(N): k=input("Enter a number:") L1.append(k)print("The first list elements are:",L1)print("Enter the elements of 2nd list:")for j in range(N): p=input("Enter a name:") L2.append(p)print("The second list elements are:",L2)d=dict(zip(L1,L2))print("The newly generated dictionary is:",d)OUTPUT:AIM: program to count frequency of characters in a given file.PROGRAM:Fn=raw_input("Enter File Name:")File=open(Fn,'r')List=File.readlines()File.close()Dict={}for line in List: for char in line: if char not in Dict: Dict[char]=1 else: Dict[char]+=1print("the frequency of characters in file is:",Dict) OUTPUT:Exercise - 7 FilesAIM: program to print each line of a file in reverse orderExercise 11 - Multi-D ListsAIM: Program that defines a matrix and prints itPROGRAM:A=[]m=input("Enter row size : ")n=input("Enter column size : ")for i in range(m): A.append([0]*n)#And again append empty lists to original list for j in range(n): A[i][j]=input("Enter the value")print "Matrix A are :"for i in range(len(A)): print A[i]OUTPUT:AIM: Program to perform addition of matrixPROGRAM:L=[]M=input("Enter the row size of the matrix:")N=input("Enter the column size of the matrix:")print("enter the elements of 1st matrix:")for i in range(M): L.append([0]*N) for j in range(N): L[i][j]=input("Enter a value:")print"The generated matrix is:"for i in range(len(L)): print L[i] L1=[]print "Enter the elements of 2nd matrix:"for i in range(M): L1.append([0]*N) for j in range(N): L1[i][j]=input("Enter value:")print "The generated matrix is:"for i in range(len(L1)): print L1[i] print "The resultant addition matrix is:"R=[]for i in range(M): R.append([0]*N) for j in range(N): R[i][j]=L[i][j]+L1[i][j]for i in range(len(R)): print R[i]OUTPUT:AIM: Program to perform multiplication of MatrixPROGRAM:A=[]M=input("Enter the row size of the matrix A:")N=input("Enter the column size of the matrix A:")P=input("Enter the row size of the matrix B:")Q=input("Enter the column size of the matrix B:")if(N==P): print"enter the elements of 1st matrix:" for i in range(M): A.append([0]*N) for j in range(N): A[i][j]=input("Enter a value: ") print"The matrix A is:" for i in range(len(A)): print A[i] B=[] print"Enter the elements of 2nd matrix:" for i in range(P): B.append([0]*Q) for j in range(Q): B[i][j]=input("Enter a value:") print "The matrix B is:" for i in range(len(B)): print B[i] C=[[0 for i in range(M)] for j in range(Q)] for i in range(M): for j in range(Q): for k in range(N): C[i][j]+=A[i][k]*B[k][j] print"the resultant matrix after multiplication is:" for i in range(len(C)): print C[i]else: print "Matrix Multiplication Not Possible"OUTPUT: ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download