# "" Means, It Should Be An Empty String Not Int or Diiferent Type # String To Int Type Casting
# "" Means, It Should Be An Empty String Not Int or Diiferent Type # String To Int Type Casting
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:
pos.append(num)
elif num<0:
neg.append(num)
elif num==0:
zero.append(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:
data.append(num)
num=int(input('enter the number to be added to list:'))
print(data)
NOTE:
reversing the numbers using reverse function it seems like returning something.. so
print(data.reverse()) & print(data.sort()) 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:
data=[]
num=int(input('enter the number to be added to list:'))
while num!=0:
data.append(num)
num=int(input('enter the number to be added to list:'))
print(data)
def rmvoutlier(data,num_outlier):
retval=sorted(data)
print('elements arranged in ascending order:\t',retval)
for i in range(num_outlier):
retval.pop()
for i in range(num_outlier):
retval.pop(0)
return retval
def main():
values=[]
s=input('enter a value(blank to quite): ')
while s!= " ":
num=int(s)
values.append(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]