1. Write a program to swap two numbers.
# a = 20
# b = 10
# 1.using temp variable swipe two numbers
# temp = a
# a = b
# b = temp
# print(a)
# print(b)
# 2. without using temp variable swipe two numbers
# a = a + b #a = 10 + 20, #a = 30
# b = a - b #b = 30 - 20, #b = 10
# a = a - b #a = 30 - 10, #a = 20
# print(a)
# print(b)
# a, b = b, a
# print(a)
# print(b)
# a="Nil"
# b="Priya"
# print("a:",a)
# print("b:",b)
a=a+b
b=a[0:len(a)-len(b)] this gives us range of sub string and not only position of
lttr
a=a[len(b):]
print("a:",a)
print("b:",b
2. How to check number is prime or not.
num=int(input('enter a number'))
if num>1:
for i in range(2,num):
if (num %i)==0:
print("number in not a prime no:")
break
else:
print("number is prime no:")
else:
print("")
--------------------------------------------------------------
or
num = int(input('number: '))
for i in range(2, num):
if (num % i) == 0:
is_prime = False # If a divisor is found, the number is not
prime
break # Exit the loop early
if is_prime:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
3. How to find factorial of a number
counter = 6
result = 1
while(counter > 1):
result *= counter ## result = result * counter
counter -= 1 ## counter = counter -1
print("Factorial of 6 : ", result) # Factorial of 5 : 120
4. How to find maximum and minimum elements in an array?
Combined both max and minimum
nums = [5, 7, 2, 4, 9, 6]
# initialize minimum and maximum element with the first element
max = min = nums[0]
# do for each element in the list
for i in range(1, len(nums)):
# if the current element is greater than the maximum found so far
if nums[i] > max:
max = nums[i]
# if the current element is smaller than the minimum found so far
elif nums[i] < min:
min = nums[i]
print('The minimum element in the list is', min)
print('The maximum element in the list is', max)
maximum
nums = [5, 7, 2, 4, 9, 6]
# initialize minimum and maximum element with the first element
max = nums[0]
# do for each element in the list
for i in range(1, len(nums)):
# if the current element is greater than the maximum found so far
if nums[i] > max:
max = nums[i]
print('The maximum element in the list is', max)
minimum
nums = [5, 7, 2, 4, 9, 6]
# initialize minimum and maximum element with the first element
min = nums[0]
# do for each element in the list
for i in range(1, len(nums)):
# if the current element is greater than the maximum found so far
if nums[i] < min:
min = nums[i]
print('The minimum element in the list is', min)
16. Find the sum of the elements in list
numlist = [2,4,2,5,7,9,23,4,5]
# Calculate sum of list
numsum=0
for i in numlist:
numsum+=i
print('Sum of List: ',numsum)
Diff between list and array : List cannot manage arithmetic operations. Array can manage arithmetic
operations.
5. Print Fibonacci series.
num = 10
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3, end=" ")
print()
5. How to find the sum of elements in an array?
arr = [1, 2, 3, 4, 5];
sum = 0;
#Loop through the array to calculate sum of elements
for i in range(0, len(arr)):
sum = sum + arr[i];
print("Sum of all the elements of an array: ", sum);
7. How to find the length of the list?
#method-1
thislist = ["apple", "banana", "cherry"] ###method 1
print(len(thislist))
#method-2
thislist = ['Python', 'Java', 'Ruby', 'JavaScript']
lenght = 0
for x in thislist:
lenght += 1
print(lenght)
8. How to swap first and last elements in the list
list=[0,12,2,45,5]
print("list before swapping:",list)
list[0],list[-1]=list[-1],list[0]
print("list after swapping:",list)
#method1
list=[0,12,2,45,5]
temp=list[0]
list[0]=list[len(list)-1]
list[len(list)-1]=temp
print("New list is:")
print(list)
##method2
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print("New list is:")
print(a)
9. How to swap any two elements in the list?
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1-1, pos2-1))
10. How to remove the Nth occurrence of a given word list?
#method 1
list=['greek','for','greek']
word='greek'
n=2
counter=0
for i in range (len(list)):
if (list[i]==word):
counter=counter+1
if (counter==n):
del(list[i])
print('NEW LIST :',list)
Using function
#method 2
def removeelement(list,word,n):
counter=0
for i in range (len(list)):
if (list[i]==word):
counter=counter+1
if (counter==n):
del(list[i])
return True
else:
return False
list=['greek','for','greek']
word='greek'
n=2
# counter=0
flag=removeelement(list,word,n) ##imp
if flag==True:
print((list))
Q.how to sort a list
11. How to search an element in the list?
12. How to clear the list?
13. How to reverse a list?
14. How to clone or copy a list?
15. How to count occurrences of an element in a list?
17. How to multiply all the numbers in the list?
18. How to find the smallest and largest numbers on the list?
19. How to find the second largest number in a list?
20. How to check string is palindrome or not?
21. How to reverse words in a string?
22. How to find a substring in a string?
23. How to find the length of a string?
24. How to check if the string contains any special character
25. Check for URL in a string
Q.write a python program you have string
str = "yogesh warghade" convert it into dict and
your output should be as follow dict = {y:1,o:1,g:2,.}
Or
Prrogram to count each element,to print element which occurs nth time
Q.
dict1 = {'a':1,'b':2,'c':3}
dict2 = {'a':1,'c':3}
you have two dictionaries write a python program to find key,value pair which are present in dict1 but
not in dict2
>>
Q.you have febonachi series where element of series is missing as shown below lst.find that missing
number
Q. write a program to find common letters between two string
Q.using two lists create dictionary
L1 =[‘name’,’age’]
L2=[‘yog’,29]
Dict = {‘name’:’yog’,’age’:29}
Q.str = 'yogesh warghade'
output = 'yxgxsx xaxgxaxe'
write a python program string and output is provided
Q . lst = [1,1,2,2,3,3,'a','a','b'] saparate numbers and string values in saparate list use python
Q .write a python program if element occurce In list nth time add I to new list
Without using count
Q .find max number between three
Q.find duplicate numbers from list
l = [5, 4, 8, 7, 8, 7, 41, 1, 2, 45, 10, 5]
l1 = []
duplicate = []
for i in l:
if i not in l1:
l1.append(i)
else:
duplicate.append(i)
print(duplicate)