- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
First-Class Citizens in Python
First-class citizens are entities that enable support for all the operations facilitating other fellow entities.
These entities are often used : while passing an argument , returning a value from function , conditional modifications & value assignment.
In this article, we will look at the implementation & usage of first-class citizens in Python 3.x or earlier. Also, we will be learning what all entities get the tag of being First Class citizens.
These citizens include Variables along with functions.
Let’s first get familiar with the data types that are part of First-class citizens
- Floating type
- Complex Numbers
All four types mentioned above are given the tag of being first-class citizens in Python 3.x. Or earlier.
#Declaration of an integer print("hello world") int_inp=int(input()) print("This is a First class Citizen of "+str(type(int_inp))) #Declaration of floating type float_inp=float(input()) print("This is a First class Citizen of "+str(type(float_inp))) #Declaration of complex numbers complex_inp=complex(input()) print("This is a First class Citizen of "+str(type(complex_inp))) #Declaration of Strings str_inp=input() print("This is a First class Citizen of "+str(type(str_inp)))
2 23.4 4+7j tutorialspoint
This is a First class Citizen of <class 'int'> This is a First class Citizen of <class 'float'> This is a First class Citizen of <class 'complex'> This is a First class Citizen of <class 'str'>
Now Let’s take a look at some functions which are referred to as first class citizens
First class objects are uniformly handled in Python language. Being objectoriented every entity refers to a default object which can be referenced and dereferenced at any point of time. Storage may be done using data structures or control structures.
Now we will look whether python supports first-class functions or not. So any language is said to support first-class functions when it treats functions as firstclass objects.
# Python program # functions being be treated as objects def comp_name(text): return text.isupper() print(comp_name("TUTORIALSPOINT")) new_name = comp_name #referencing a function with the object print(new_name("TutorialsPoint"))
# Python program # functions being passed as arguments to other functions def new_inp(text): return text.upper() def old_inp(text): return text.lower() def display(func): # storing the function in a normal variable code = func("Love Coding, Learn everything on Tutorials Point") print (code) display(new_inp) #directly referenced by passing functions as arguments. display(old_inp)
LOVE CODING, LEARN EVERYTHING ON TUTORIALS POINT love coding, learn everything on tutorials point
Here it is clearly seen that Python functions can be referenced using an object and can also be passed as an argument to another function which clearly shows that in Python functions are First Class Citizens and can be referenced and dereferenced using an object entity.
In this article, we learnt the implementation of max and min function included in the Standard Python Library.
- First Class functions in Python
- The :first-child Pseudo-class in CSS
- Usage of :first-child pseudo-class in CSS
- Program to find out the minimal cost so that the citizens have access to a market in Python
- First Bad Version in Python
- First CGI Program in Python
- First Missing Positive in Python
- Class Inheritance in Python
- self in Python class
- Changing Class Members in Python?
- String Template Class in Python?
- Python class browser support
- Built-In Class Attributes in Python
- First occurrence of True number in Python