[go: up one dir, main page]

0% found this document useful (0 votes)
3 views2 pages

Coding-TEST 4 Questions

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)
3 views2 pages

Coding-TEST 4 Questions

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/ 2

SET

1. How do you create an empty set in python?


Using set()
2. How to add elements to a set?
add() -> adds a single element to set
update() -> adds multiple elements in set form to set
3. How to remove elements from a set?
pop() -> removes random element from set
remove() -> removes specified element from set.
raises KeyError when nonexistent element specified
discard() -> same as remove(), but does not raise KeyError
clear() -> gives empty set ( set() )
4. What are the set operators in python?
Union (|) -> set of all elements in A and B
Intersection (&) -> set of all elements common to A and B
Symmetric difference (^) -> set of all elements uncommon to A and B
Difference (-) -> difference of A from B (A-B) is set of all elements in A but not in B
5. Write a note about set in python?
 Set is mutable
 Sequence of immutable elements enclosed in curly brackets separated by commas
 Consists of unique elements only (duplicates not allowed)
 Is unordered, therefore, no indexing or slicing possible
6. Difference between remove() and discard()?
Both remove() and discard() remove specified elements, but remove() raises KeyError when
nonexistent element specified, wereas discard() doesnt
7. How to check if an element present in a set or not?
Using ‘in’ operator
DICTIONARY

1. Write a note about dictionary in python?


 Dictionary is an ordered collection of key-value pair enclosed in curly brackets
separated by commas
 It is mutable
 Optimized for retrieving data, where key is used to retrieve the value
 Key must be immutable, value can be of any type
2. How to access dictionary elements?
Using key, we can access value in dictionary
3. How to get all the values from a dictionary? (using forloop)

4. how to access both keys and values from a dictionary?


5. How to check if a key exists in a dictionary?
Using ‘in’ operator
6. How to add an item to a dictionary?
Using new index key and assigning value to it
7. What is a nested dictionary in python?
A dictionary within another dictionary is called nested dictionary
This nested dictionary can only remain as value, because dict is mutable. Key is immutable
8. How to create a new dictionary?
Using dict() -> keys are not string literal
-> use ‘=’ rather than ‘:’ for assignments

9. What are the properties of a dictionary key?

 Immutable (num,str,tuple)
 Unique elements only allowed
9. Difference between setdefault() and get() method?
Setdefault() adds non existent keyvalue pair to dict wereas get() does not

FUNCTIONS :
str() -> returns printable string representation of dict
fromkeys() -> creates new dict with keys from seq and values set to value
get() -> returns value for given key, if key not available, returns ‘None’
keys() -> returns list of all keys in dict
update() -> adds d2’s keyvalue pairs to d1
setdefault() -> ws

You might also like