28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

print "Value : %s" % dict.items()

Let us compile and run the above program, this will produce the following result:

Value : [('Age', 7), ('Name', 'Zara')]

dict.keys()

Description

The method keys() returns a list of all the available keys in the dictionary.

Syntax

Following is the syntax for keys() method:

dict.keys()

Parameters

• NA

Return Value

This method returns a list of all the available keys in the dictionary.

Example

The following example shows the usage of keys() method.

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7}

print "Value : %s" % dict.keys()

Let us compile and run the above program, this will produce the following result:

Value : ['Age', 'Name']

dict.setdefault(key, default=None)

Description

The method setdefault() is similar to get(), but will set dict[key]=default if key is not already in dict.

Syntax

Following is the syntax for setdefault() method:

dict.setdefault(key, default=None)

Parameters

• key -- This is the key to be searched.

• default -- This is the Value to be returned in case key is not found.

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!