0% found this document useful (0 votes)
2 views

Python Polymorphism

Polymorphism in programming refers to methods or functions that can operate on different objects or classes with the same name. In Python, the len() function is an example of function polymorphism, as it can return the length of strings, tuples, and dictionaries. Each of these data types uses len() to provide the number of characters, items, or key/value pairs, respectively.

Uploaded by

vampireweekend
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)
2 views

Python Polymorphism

Polymorphism in programming refers to methods or functions that can operate on different objects or classes with the same name. In Python, the len() function is an example of function polymorphism, as it can return the length of strings, tuples, and dictionaries. Each of these data types uses len() to provide the number of characters, items, or key/value pairs, respectively.

Uploaded by

vampireweekend
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

Python Polymorphism

❮ PreviousNext ❯

The word "polymorphism" means "many forms", and in programming it


refers to methods/functions/operators with the same name that can be
executed on many objects or classes.

Function Polymorphism
An example of a Python function that can be used on different objects is
the len() function.

String
For strings len() returns the number of characters:

ExampleGet your own Python Server


x = "Hello World!"

print(len(x))

Try it Yourself »

Tuple
For tuples len() returns the number of items in the tuple:

Example
mytuple = ("apple", "banana", "cherry")

print(len(mytuple))

Try it Yourself »

Dictionary
For dictionaries len() returns the number of key/value pairs in the dictionary:

Example
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

print(len(thisdict))

Try it Yourself »

You might also like