arr = [1, 2, 3, 4, 5]
for element in arr:
print(element, end=" ")
Store Element in the List and display it
print("How many element to store in the list ? ", end="")
n = input()
arr = []
print("\nEnter", n, "Elements: ", end="")
n = int(n)
for i in range(n):
element = input()
arr.append(element)
print("\nThe list is:")
for i in range(n):
print(arr[i], end=" ")
Sum of Element in The list
nums = []
print("Enter 5 elements for the list: ")
for i in range(5):
val = int(input())
nums.append(val)
sum = 0
for i in range(5):
sum = sum + nums[i]
print("\nSum of all elements =", sum)
User should input the elements in the array and then
Search an Element in a List (Linear Search)
Linear
Binary
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
val = int(input())
mylist.append(val)
print("Enter an element to be search: ")
elem = int(input())
for i in range(5):
if elem == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)
Binary Search using List
nums = []
print("Enter 10 Numbers (in ascending order):")
for i in range(10):
nums.insert(i, int(input()))
print("Enter a Number to Search:")
search = int(input())
first = 0
last = 9
middle = (first+last)/2
middle = int(middle)
while first <= last:
if nums[middle]<search:
first = middle+1
elif nums[middle]==search:
print("The Number Found at Position:")
print(middle+1)
break
else:
last = middle-1
middle = (first+last)/2
middle = int(middle)
if first>last:
print("The Number is not Found in the List")
Bubble Sort using List
nums = []
print("Enter the Size of List: ")
tot = int(input())
print("Enter " + str(tot) + " Numbers: ")
for i in range(tot):
nums.insert(i, int(input()))
for i in range(tot-1):
for j in range(tot-i-1):
if nums[j]>nums[j+1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
print("The Sorted List is:")
for i in range(tot):
print(nums[i])
Text File
Read a File in Python
print("Enter the Name of a File to Read: ", end="")
fileName = input()
fileHandle = open(fileName, "r")
content = fileHandle.read()
print("\n----File Contains----")
print(content)
Python Write Text to a File
print("Enter the Name of a File: ", end="")
fileName = input()
print("Enter the Text to Write in File: ", end="")
text = input()
fileHandle = open(fileName, "w")
fileHandle.write(text)
fileHandle.close()
print("\nThe given content is written on the file successfully!")
Append Text to a File in Python
print("Enter the Name of File: ")
fileName = str(input())
fileHandle = open(fileName, "a")
print("Enter the Text to Append in Given File: ")
while True:
text = str(input())
if len(text)>0:
fileHandle.write("\n")
fileHandle.write(text)
else:
break
fileHandle.close()
HW: Calculator Program using while Loop and if-else
while True:
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
print("Enter Your Choice (1-5): ", end="")
ch = int(input())
if ch>=1 and ch<=4:
print("\nEnter Two Numbers: ", end="")
numOne = float(input())
numTwo = float(input())
if ch==1:
res = numOne + numTwo
print("\nResult =", res)
elif ch==2:
res = numOne - numTwo
print("\nResult =", res)
elif ch==3:
res = numOne * numTwo
print("\nResult =", res)
elif ch==4:
res = numOne / numTwo
print("\nResult =", res)
elif ch==5:
break
else:
print("\nInvalid Input!..Try Again!")
print("------------------------")