[go: up one dir, main page]

0% found this document useful (0 votes)
29 views6 pages

Mod 2 Solutions

The document provides an overview of various list methods in Python, including append(), index(), sort(), remove(), and insert(), explaining their functionalities and usage. It also discusses negative indexing, the use of 'in' and 'not in' operators, and introduces dictionary methods such as get() and setdefault(), highlighting the differences between lists and dictionaries. Additionally, it compares the characteristics and use cases of lists and dictionaries in Python.

Uploaded by

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

Mod 2 Solutions

The document provides an overview of various list methods in Python, including append(), index(), sort(), remove(), and insert(), explaining their functionalities and usage. It also discusses negative indexing, the use of 'in' and 'not in' operators, and introduces dictionary methods such as get() and setdefault(), highlighting the differences between lists and dictionaries. Additionally, it compares the characteristics and use cases of lists and dictionaries in Python.

Uploaded by

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

Introduction to Python Programming (BPLCK105B)

MODULE -2

VTU Questions Paper Solution

1. Explain append(),index(),sort(),remove(),insert() methods with respect to lists in


python
i. append()
 To add new values to a list, use the append() method
 The append() method call adds the argument to the end of the list.
 The append() methods are list methods and can be called only on list values,
not on other values such as strings or integers
 Example:

ii. index()
 List values have an index() method that can be passed a value, and if that
value exists in the list, the index of the value is returned. If the value isn’t in
the list, then Python produces a ValueError error.
 Example:

iii. sort()
 Lists of number values or lists of strings can be sorted with the sort()
method
 First, the sort() method sorts the list in place; don’t try to return value by
writing code like spam = spam.sort().
 Second, we cannot sort lists that have both number values and string
values in them

Meghashree, CSE (ICSB) PA College of Engineering Page 1


Introduction to Python Programming (BPLCK105B)

 Third, sort() uses “ASCIIbetical order(upper case)” rather than actual


alphabetical order(lower case) for sorting strings
Ex:

iv. remove()
 The remove () method is passed the value to be removed from the list it is
called on.
 If the value appears multiple times in the list, only the first instance of the
value will be removed.
 The remove() method is good when you know the value you want to
remove from the list.
Ex:

v. insert()
 The insert() method can insert a value at any index in the list. The first
argument to insert() is the index for the new value, and the second
argument is the new value to be inserted
Ex:

Meghashree, CSE (ICSB) PA College of Engineering Page 2


Introduction to Python Programming (BPLCK105B)

2. Explain negative indexing and the use of in and not in operator in the list with
examples
1. Negative Indexing
 We can also use negative integers for the index. The integer value -1 refers to the
last index in a list, the value -2 refers to the second-to-last index in a list, and so on
 Accessing from the End: Negative indexing allows you to access elements of a
list, string, or tuple from the end.
 Reverse Order: While positive indices count from the start (0, 1, 2...), negative
indices count backward (-1, -2, -3...).
 For example, -1 refers to the last element, -2 to the second last, and so on.

2. Use of in and not in operator in the list


 We can determine whether a value is or isn’t in a list with the in and not in
operators.
 in and not in are used in expressions and connect two values: a value to look for in
a list and the list where it may be found and these expressions will evaluate to a
Boolean value.

Meghashree, CSE (ICSB) PA College of Engineering Page 3


Introduction to Python Programming (BPLCK105B)

3. Explain with a programming example to each i) get() ii) set default()


i)get
 Dictionaries have a get() method that takes two arguments:
 The key of the value t o retrieve and
 A fallback value to return if that key does not exist
 get() gives the value for a key but won't show an error if the key is not there.
It shows None instead.

ii)set default()

 To set a value in a dictionary for a certain key only if that key does not
already have a value
 The setdefault() method offers a way to do this in one line of code.
 Setdeafault() takes 2 arguments:
 The first argument is the key to check for, and
 The second argument is the value to set at that key if the key does not
exist. If the key does exist, the setdefault() method returns the key’s
value

Example:

Meghashree, CSE (ICSB) PA College of Engineering Page 4


Introduction to Python Programming (BPLCK105B)

4. What is a dictionaries in python? Give the difference between list and


dictionaries
 A dictionary is a collection of many values.
 Indexes for dictionaries can use many different data types, not just integers.
Indexes for dictionaries are called keys, and a key with its associated value is
called a key-value pair. A dictionary is typed with braces, {}.
 This assigns a dictionary to the myCat variable. This dictionary’s keys are
'size', 'color', and 'disposition'.
 The values for these keys are 'fat', 'gray', and 'loud', respectively. You can
access these values through their keys:
 Dictionaries can still use integer values as keys, but they do not have to start
at 0 and can be any number.
 Examples:
1.

2.

3.

Meghashree, CSE (ICSB) PA College of Engineering Page 5


Introduction to Python Programming (BPLCK105B)

Comparing lists and dictionaries in Python

Feature List Dictionary


Items are ordered, and position Dictionary are unordered, and there is no "first"
Order
matters. or "last".
Access Access items using numeric
Access values using keys like dict["key"].
Method indexes like list[0].
Error for Gives IndexError if an invalid
Gives KeyError if a missing key is accessed.
Missing Item index is accessed.
Stores items as a sequence of
Structure Stores data as key-value pairs.
values.
Best for ordered collections like Best for organizing data with meaningful keys
Use Case
numbers or strings. (e.g., names and birthdays).

birthdays = {"Alice": "Jan 1", "Bob": "Feb 2"}


spam = [10, 20, 30] print(birthdays["Alice"])
Example:
print(spam[0]) # Output: Jan 1
# Output: 10

Meghashree, CSE (ICSB) PA College of Engineering Page 6

You might also like