• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Python

Python List

  • Categories Python, Python List, Python

Q. 1 What is list in python? Also give an example.
Ans.

    • A list is an ordered collection of items separated by commas and enclosed within square brackets ([]).
    • It can contain data of any type.
    • List is mutable, means we can change any data inside the list unlike string.
    • An empty list is denoted by empty square brackets[]
    • g. list = [ ‘abcd’, 26 , ‘j’, 3.14 ]

Q. 2 How to create a python list?
Ans.

    • To create a new Python list, you can either assign empty square brackets L=[ ] or you can use the list constructor L= list().
    • You can also assign any values directly inside square brackets separated by comma.
      L1= L = [ val1, val2,val3, val4….]
    • You can also create nested lists.
      L2 = [val1, val2, [val31, val32], val4]
    • You can create lists from other sequences such as string or tuple.
      L1 = list(“String”)
      L2 = list( (1,2,3,4))
    • Do note that creating a new list by inserting a string in square brackets, behaves differently than using a constructor which takes in a string.

 

Q. 3 What is the difference between creating two lists like this L1 = [‘Simply’] and L1 = list(‘Simply’)?
Ans. In the first option a list will be created and it will have only 1 element “Simply”. In second one a list will be created from string “Simply” and each individual character will be separate element of the list. So it will give output as [‘S’,’i’, ‘m’, ‘p’, ‘l’ , ‘y’ ]

Q. 4 Give 2 similarities and dis-similarities between lists and Strings.
Ans.

     Similarities

    • Indexing and Slicing work the same
    • In, not in + and * operation works the same

     Dis-Similarities

    • Strings are non-mutable, lists are mutable
    • Each character is stored directly in memory in String. In list each list element is stored as a reference (its address is stored)

Q.5  What is the output when following operators are applied on these list?
             a =  [ ‘Simply’, 2 ]
             b = [ ‘Coding’, 3.14]

    1. print (a+b)
    2. print (a*2)
    3. print (2 in a)
    4. print (‘S’ in a)
    5. ‘C’ not in b
    6. 3 not in b

Ans. 

    1. [ ‘Simply’, 2, ‘Coding’, 3.14]
    2. [ ‘Simply’, 2, ‘Simply’, 2]
    3. True
    4. False
    5. True
    6. True

Q. 6 Consider following 2 lists, what is the output of following List indexing and Slicing?
               a =  [ ‘Simply’, 2, 4, 10 ]
               b = [ ‘Coding’, 3.14, 55, 14]

 

    1. print (a[0], b[0])
    2. a[-1]
    3. a [10]
    4. a[ :-1]
    5. a[0: ]
    6. a[:2] + a[2:]
    7. a[0:10]

Ans. 

    1. Simply Coding
    2. 10
    3. Error
    4. [‘Simply’, 2, 4]
    5. [ ‘Simply’, 2, 4, 10 ]
    6. [ ‘Simply’, 2, 4, 10 ]
    7. [ ‘Simply’, 2, 4, 10 ]

Q. 7 What is the output of List editing commands on following lists?
              a =  [ ‘Simply’, 2, 4, 10 ]
              b = [ ‘Coding’, 3.14, 55, 14]

    1. a[0:2] = [0,1]
    2. a[0:2] = [40]
    3. a[1] = 20
    4. del a[0]
    5. a[0: ]
    6. a.pop()
    7. a.pop(1)
    8. a.remove(2)
    9. a.clear()
    10. a.insert(2,40)
    11. a.append(2)
    12. a.extend([3,4])

Ans. 

    1. [ 0, 1, 4, 10 ]
    2. [ 40, 4, 10 ]
    3. [ ‘Simply’, 20, 4, 10 ]
    4. [ 2, 4, 10 ]
    5. [ 4, 10 ]
    6. [ ‘Simply’, 2, 4 ]
    7. [ ‘Simply’, 4, 10 ]
    8. [ ‘Simply’, 4, 10 ]
    9. []
    10. [ ‘Simply’, 2, 40, 4, 10]
    11. [ ‘Simply’, 2, 4, 10, 2 ]
    12. [ ‘Simply’, 2, 4, 10, 3, 4 ]

Q. 8 List method ___ is used to add element at the end of the list.

    1. insert()
    2. append()
    3. extend()
    4. pop()

Ans. b

Q. 9 extend() Method use to add a ___ to the end of the list

    1. List
    2. Number
    3. String
    4. None of above.

Ans. a

Q. 10 Which List Method is used to add element at exact position? Also give Syntax for
Ans. Insert to add a element at specified position.
Syntax: list.insert(index, elmnt)
           elmnt can be anything like string, number, list, etc.

Q. 11 Pop Function is used to ____ element at specific position.

    1. Remove
    2. Add
    3. Copy
    4. None of above

Ans. a

Q. 12 _____ List function is used to display elements in ascending or descending order.
Ans. sort()

Q. 13 What is the difference between second and third statement below?
             L1 = [2,3]
             L2 = L1
             L3 = L1.copy()

Ans. The second statement does not creates a copy of L1. IT just assigns another name to L1. So if you make change in L1 it reflects in L2 too.
However third statement makes a true copy of L1. If L1 is changed, it does not impact L3.

Q. 14 Consider the following List and give the output after evaluating the code.

Lst = ['These',"are","a",["few","words"],"that","we","will","use"]
Lst[3:4][0]
[L[1]] + L[3]
remove('a')
print(Lst)
L2= ["simply",'Coding']
Lst.append(L2)
print(Lst)

Ans.

[‘few’, ‘words’]
[‘are’, ‘few’, ‘words’]
[‘These’, ‘are’, [‘few’, ‘words’], ‘that’, ‘we’, ‘will’, ‘use’]
[‘These’, ‘are’, ‘a’, [‘few’, ‘words’], ‘that’, ‘we’, ‘will’, ‘use’, [‘simply’, ‘Coding’]]

Q. 15 Give the output of following code.

a,b,c = [1,2],[1,2],[1,2]
print (a == b)
print (a is b)
d = a
print (a is d)

Ans. output.

True
False
True

Q. 16 Write a Python program to remove duplicates from a list.
Ans.

a = [10,20,30,20,10,50,60,40,80,50,40]
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)

Q.17  Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
Ans.

values = input("Input some comma separated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)

Q. 18 Write a Python program to create a histogram from a given list of integers.
Ans.

def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])

Q. 19 Write a Python program to filter the positive numbers from a list.
Ans.

nums = [34, 1, 0, -23]
print("Original numbers in the list: ",nums)
new_nums = list(filter(lambda x: x >0, nums))
print("Positive numbers in the list: ",new_nums)

Q. 20 Write a Python program to remove and print every third number from a list of numbers until the list becomes empty.
Ans.

def remove_nums(int_list):
#list starts with 0 index
position = 3 - 1
idx = 0
len_list = (len(int_list))
while len_list>0:
idx = (position+idx)%len_list
print(int_list.pop(idx))
len_list -= 1
nums = [10,20,30,40,50,60,70,80,90]
remove_nums(nums)

Some List methods : 

MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

  • Share:
author avatar
Simply Coding

Previous post

Python Strings
June 15, 2021

Next post

Python Tuples
June 15, 2021

You may also like

Python function
Python function
3 July, 2021
for & while
Learn Python for & while
19 June, 2021
Queue
Python Queue
17 June, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now