- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
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.
- Related Articles
- First Class functions in Python
- The :first-child Pseudo-class in CSS
- What is first-class lever?
- Program to find out the minimal cost so that the citizens have access to a market in Python
- Usage of :first-child pseudo-class in CSS
- Class Inheritance in Python
- self in Python class
- First Bad Version in Python
- First CGI Program in Python
- First Missing Positive in Python
- String Template Class in Python?
- Changing Class Members in Python?
- Class & Instance Attributes in Python
- Built-In Class Attributes in Python