Python Exercises



Python code for Arrays and Files

Input values in an 1D Array

	
			
		#creating an empty list
		lst = []
		# number of elements as input	
		n = int(input("Enter number of elements : "))
  
		# iterating till the range
		for i in range(0, n):
			print("Enter the value at number[",i,"]",end='')
			ele = int(input()) 
		   lst.append(ele) # adding the element
	
		#loop for printing    
		for i in range(0, n):
		    print("The value at number[",i,"] is: ", lst[i])
		#to print using single command
		#print(lst)
			
	

Linear Search in Array

			
		
		lst = [76,12,342,3,4,6,1,9,10]
		n = len(lst)

		flag = False
		answer = "y"
		
		while answer == "y":    
		    print("Enter the value to search the list:",end='')
		    search = int(input())
		    flag = False
		    for i in range(0, n):
		        if lst[i] == search:
		            flag = True
		            print("The value found at number[",i,"]")    
		    if flag == False:
		        print("The value not found.")
		    answer = input("Do you want to search another number?(y/n)")    
	
	

Bubble Sort in Array

			
		
		myList = [321,34,1,5,7,23,0,133,87]
		UB = 8
		LB = 0
			#index : INTEGER
		index = 0
			#swap : BOOLEAN
		swap = True
			#DECLARE temp : INTEGER
		temp = 0
			#DECLARE top : INTEGER
		top = UB
			#top ← upperBound
		print(myList)
		while (swap) or (top > 0):
		    for index in range (LB,top):
	        swap = False
	        if ( myList[index] > myList[index + 1]):
    	        print ("in if")
    	        temp = myList[index]
    	        myList[index] = myList[index + 1]
    	        myList[index + 1] = temp
    	    swap = True
    	next 
    	top = top -1
		print(myList)    
	
	

2D Array: Making a Student Result sheet

			
		
		#classAS = [["Student Name","Test1", "Test2", "Test3", "Test4"]]
		strength = int(input("Enter the number of Students: "))
		subjects= int(input("Enter the number of Tests: "))
		subjects= subjects + 1
		r,c=0,0

		classAS = [[0] * subjects for i in range(strength)]
	
		#print(classAS)
		for r in range(strength):
		    for c in range(subjects):
		        print(classAS[r][c],end=" ")
		    print("\n")
		
		classAS[0][0]="Name"
		r=0
		c=0
		sub = ""
		# Getting names of subjects
		for i in range (1,subjects):
		    print("Enter Subject ",i," Name: ")
		    sub = input()
		    classAS[0][i] = sub
		
		student = ""
		marks = 0
		for row in range (1,strength):
		    print("\n")
		    print("Enter name of Student ",row,": ")
		    student = input()
		    classAS[row][0]=student
	    
    		for column in range (1,subjects):
    		    print("Enter marks of test ", column ,": ",end="")
    		    marks = int (input())
    		    classAS[row][column]=marks
	
		r,c=0,0
		print("\n")
		#Display the whole array
		for r in range(strength):
		    for c in range(subjects):
		        print(classAS[r][c],end=" ")
		    print("\n")
        
	
	

File Handling

			
		
		# Open a file named "myText.txt" which should be in the same folder as this code file
		textLn = "a"
		
		print("The file contains these lines of text:")
		f=open("myText.txt","r")
		print(f.read())
		
		f.close()
		f=open("myText.txt","w")
		while textLn != "":
		    print ("Please enter a line of text: ")
		    textLn = input()
		    if (textLn==""):
		        f.close()
		    else:
		        textLn = textLn + "\n"
		        f.write(textLn)
		print("The file contains these lines of text:")
		f=open("myText.txt","r")
		print(f.read())
		f.close()