
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python dictionary setdefault() Method
Description
Python dictionary 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.
Return Value
This method returns the key value available in the dictionary and if given key is not available then it will return provided default value.
Example
The following example shows the usage of setdefault() method.
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.setdefault('Age', None) print "Value : %s" % dict.setdefault('Sex', None)
When we run above program, it produces following result −
Value : 7 Value : None
python_dictionary.htm
Advertisements