Python code part 2
#WAP to find whether a given username contains less than 10 characters or not
username = input("Enter username:")
if len(username) < 10:
print("\nusername is valid")
else:
print("\nusername is not valid")
# WAP to take input in list and find largest number in list
array_size=int(input("Enter array_size:"))
array=[]
for numbers in range(array_size):
values=input("Enter array elements:")
array.append(values)
print(array)
largest=array[0]
for next_number in array:
if largest<next_number:
largest=next_number
print("The largest in the array : ",largest)
#we check next_number2 in array then check condition if next_number2 is less than smallest then the next_number2 becomes the smallest which is done in the next step
smallest=array[0]
for next_number2 in array:
if smallest > next_number2:
smallest=next_number2
print("The smallest in the array:",smallest)
OUTPUT:
Enter array_size:4
Enter array elements:2
Enter array elements:9
Enter array elements:8
Enter array elements:3
['2', '9', '8', '3']
The largest in the array : 9
The smallest in the array: 2
##WAP for exchanging the start and the end character of a string
string=input("Enter string for exchanging the start and the end character of a string:")
if len(string)==1:
print(string)
else:
result=string[-1:]+string[1:-1]+string[0:1]
print(result)
OUTPUT:
Enter string for exchanging the start and the end character of a string:PYTHON
NYTHOP
##WAP List Overlap Common between two lists
list1=[]
list2=[]
n_list1=int(input("Enter number of elements in list1:"))
for n1 in range(n_list1):
elements1=int(input("Enter list1 elements:"))
list1.append(elements1)
n_list2=int(input("Enter number of elements in list2:"))
for n2 in range(n_list2):
elements2=int(input("Enter list2 elements:"))
list2.append(elements2)
print("List1 elements-->",list1)
print("List2 elements-->",list2)
print("Common element list-->",list(set(list1).intersection(list2)))
OUTPUT:
Enter number of elements in list1:3
Enter list1 elements:1
Enter list1 elements:2
Enter list1 elements:3
Enter number of elements in list2:5
Enter list2 elements:2
Enter list2 elements:3
Enter list2 elements:4
Enter list2 elements:5
Enter list2 elements:6
List1 elements--> [1, 2, 3]
List2 elements--> [2, 3, 4, 5, 6]
Common element list--> [2, 3]
##WAP that takes a list of numbers and makes a new list of only the first and last elements of the given list.
a_list=[]
no_of_elements=int(input("Enter number of elements in list2:"))
for n2 in range(no_of_elements):
elements=int(input("Enter list elements:"))
a_list.append(elements)
def list_ends(a_list):
return [a_list[0], a_list[-1]]
print(list_ends(a_list))
OUTPUT:
Enter number of elements in list2:5
Enter list elements:1
Enter list elements:2
Enter list elements:5
Enter list elements:6
Enter list elements:7
[1, 7]
##WAP to find sum of first n natural numbers
def sum(number):
if number<=0:
return number
else:
return sum(number-1)+number
number=int(input("Enter number until where you want to find some:"))
if number<0:
print("Enter +ve number:")
else:
print("Sum of first", number," numbers in natural number series is", sum(number))
OUTPUT:
Enter number until where you want to find some:6
Sum of first 6 numbers in natural number series is 21
Comments
Post a Comment