1) #PROGRAM TO SEGREGATE NUMBERS BELOW ZERO AND NUMBERS ABOVE ZERO
neg=[]
pos=[]
zero=[]
line=input('enter the value: ')
while line!= "": # "" means, it should be an empty string not int or diiferent type
num=int(line) # string to int type casting
if num>0:
[Link](num)
elif num<0:
[Link](num)
elif num==0:
[Link](num)
line=input('enter the value: (blank to exit)')
print(neg,pos,zero)
NOTE:
# !="" expression is wrong ==> gives invalid literal error
# != "" expression is wrong ==> works perfect
Output:
enter the value: 10
enter the value: (blank to exit)20
enter the value: (blank to exit)30
enter the value: (blank to exit)-10
enter the value: (blank to exit)-20
enter the value: (blank to exit)-30
enter the value: (blank to exit)0
enter the value: (blank to exit)0
enter the value: (blank to exit)0
enter the value: (blank to exit)
[-10, -20, -30] [0, 0, 0] [10, 20, 30]
2) #PROGRAM TO SORT THE ELEMENTS OF A LIST
data=[]
num=int(input('enter the number to be added to list:'))
while num!=0:
[Link](num)
num=int(input('enter the number to be added to list:'))
print(data)
[Link]() # To sort the numbers in ascending order
print(data)
print(data[::-1]) # To sort the numbers in descending order using slicing
[Link]() # To sort the numbers in descending order using inbuilt function(reverse)
print(data)
NOTE:
reversing the numbers using reverse function it seems like returning something.. so
print([Link]()) & print([Link]()) will give None answer.
We can sorting of a list is as another copy of the list, when there is a situation where both original list
and sorted lists are needed.
Output:
enter the number to be added to list:25
enter the number to be added to list:30
enter the number to be added to list:15
enter the number to be added to list:26
enter the number to be added to list:42
enter the number to be added to list:29
enter the number to be added to list:10
enter the number to be added to list:30
enter the number to be added to list:22
enter the number to be added to list:0
[25, 30, 15, 26, 42, 29, 10, 30, 22]
[10, 15, 22, 25, 26, 29, 30, 30, 42]
[42, 30, 30, 29, 26, 25, 22, 15, 10]
[42, 30, 30, 29, 26, 25, 22, 15, 10]
#PROGRAM TO SORT THE ELEMENTS OF A LIST (using sorted())
data=[]
num=int(input('enter the number to be added to list:'))
while num!=0:
[Link](num)
num=int(input('enter the number to be added to list:'))
print(data)
#[Link]() # To sort the numbers in ascending order
print(sorted(data))
#print(data)
print(data[::-1]) # To sort the numbers in descending order using slicing
[Link]() # To sort the numbers in descending order using inbuilt function(reverse)
print(data)
enter the number to be added to list:2
enter the number to be added to list:3
enter the number to be added to list:1
enter the number to be added to list:5
enter the number to be added to list:4
enter the number to be added to list:6
enter the number to be added to list:7
enter the number to be added to list:4
enter the number to be added to list:0
[2, 3, 1, 5, 4, 6, 7, 4]
[1, 2, 3, 4, 4, 5, 6, 7]
[4, 7, 6, 4, 5, 1, 3, 2]
[4, 7, 6, 4, 5, 1, 3, 2]
#PROGRAM TO REMOVE OUTLIERS IN THE LIST
def rmvoutlier(data,num_outlier):
retval=sorted(data)
print('elements arranged in ascending order:\t',retval)
for i in range(num_outlier):
[Link]()
for i in range(num_outlier):
[Link](0)
return retval
def main():
values=[]
s=input('enter a value(blank to quite): ')
while s!= " ":
num=int(s)
[Link](num)
s=input('enter a value(blank to quite): ')
if len(values)<4:
print('yout dont enter enough values.')
else:
print('with the outliers removed:',rmvoutlier(values,2))
print('the original data:', values)
main()
output:
enter a value(blank to quite): 10
enter a value(blank to quite): 5
enter a value(blank to quite): 7
enter a value(blank to quite): 9
enter a value(blank to quite): 31
enter a value(blank to quite): 22
enter a value(blank to quite): 20
enter a value(blank to quite): 15
enter a value(blank to quite): 48
enter a value(blank to quite): 8
enter a value(blank to quite):
elements arranged in ascending order: [5, 7, 8, 9, 10, 15, 20, 22, 31, 48]
with the outliers removed: [8, 9, 10, 15, 20, 22]
the original data: [10, 5, 7, 9, 31, 22, 20, 15, 48, 8]