0% found this document useful (0 votes)
45 views

# "" Means, It Should Be An Empty String Not Int or Diiferent Type # String To Int Type Casting

The document contains 4 Python programs: 1) A program to separate numbers into lists based on being positive, negative, or zero. It takes user input and appends it to the appropriate list. 2) A program to sort numbers in a list in ascending and descending order using various Python functions. 3) A program similar to #2 that uses the sorted() function to sort a list. 4) A program to remove outlier numbers from a list by sorting it and removing the highest and lowest values.

Uploaded by

vinod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

# "" Means, It Should Be An Empty String Not Int or Diiferent Type # String To Int Type Casting

The document contains 4 Python programs: 1) A program to separate numbers into lists based on being positive, negative, or zero. It takes user input and appends it to the appropriate list. 2) A program to sort numbers in a list in ascending and descending order using various Python functions. 3) A program similar to #2 that uses the sorted() function to sort a list. 4) A program to remove outlier numbers from a list by sorting it and removing the highest and lowest values.

Uploaded by

vinod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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:
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)

data.sort() # To sort the numbers in ascending order


print(data)

print(data[::-1]) # To sort the numbers in descending order using slicing

data.reverse() # 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(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:

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:
data.append(num)
num=int(input('enter the number to be added to list:'))

print(data)

#data.sort() # To sort the numbers in ascending order


print(sorted(data))
#print(data)

print(data[::-1]) # To sort the numbers in descending order using slicing

data.reverse() # 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):
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]

You might also like