Computer Science, asked by rithika9473, 3 months ago

Create a list called “list2” and add numbers from 1 to 10 in the list
Using “pop” operation remove the last element from “list2
Using “remove” operation remove3 from the list “list2”
Using “for” loop and “range” function write a program to print the first 10 positive numbers(excluding 0 and including 10).

Answers

Answered by anindyaadhikari13
2

Answer:

1. To create a list called list2 and add numbers from 1 to 10, write the cσde given below,

\dag \: \boxed{\tt list2=list(range(1,11))}

2. To remove the last element from the list, write the cσde given below,

\dag \: \boxed{\tt list2.pop(-1)}

3. Using "for" loop and range function, write a program to print first ten positive numbers (excluding 0 and including 10). Here comes the cσde -

\dag\ \boxed{\begin{array}{c}\tt for \: i \: in \: range(1,11):\\\tt print(i,end=' \ ' ) \:\end{array}}

Explanation:

1. The list() constructor creates a list in python. list(range(1,11)) creates a list with numbers in the range 1(included) and 11(excluded) which is needed in the program.

So, to create list2, we wrote,

list2 = list(range(1,11))  # 11 is excluded

2. To remove the last element from the list, the pop() function is used. pop() function removes element at a specific index.

Python supports negative indexing. Let us write the negative index of the elements of the list.

\dag \ \boxed{\begin{array}{c|c|c|c|c|c|c|c|c|c|c} \tt \underline{Item}: & \sf 1 & \sf 2 & \sf 3 & \sf 4 & \sf 5 & \sf 6 & \sf 7 & \sf 8 & \sf 9 & \sf 10\\\tt \underline{Index}: & \sf -10 & \sf -9 & \sf -8 & \sf -7 & \sf -6 & \sf -5 & \sf -4 & \sf -3 & \sf -2 & \sf -1\end{array}}

From here, we can see that the index of last element is -1.

So, list2.pop(-1) removes the last element from the list.

>> list2 = [1,2,3,4,5,6,7,8,9]

3. To print numbers from 1 to 10, for loop is used.

range(1,11) iterates through numbers in the range 1 to 10 (last value is excluuded) As step value is not given, by-default, step value is 1. Inside the loop, print() statement is written so as to print the numbers from 1 to 10.

Answered by Anonymous
24

Explanation:

please refer the attachment

Attachments:
Similar questions