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
Answer:
1. To create a list called list2 and add numbers from 1 to 10, write the cσde given below,
2. To remove the last element from the list, write the cσde given below,
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 -
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.
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.
Explanation:
please refer the attachment