Python
Pages in this section
- Bitwise operations in pythonLast edited: 2026-01-28
# Bitwise operations
Bitwise operations can apply to the binary data type but also integers .
Representation of integers in Python IndexIn Python integers are signed and stored using Two’s complement .
- Classes in Python
Last edited: 2023-11-11Classes in python are defined by the
classkeyword for example.class NewThing: def __init__(self, variable): self.variable = variableWhen using classes you use the
selfkeyword to refer back to the object. Therefore a class method requires to have the first argument asself.Special functions
# __call__
When a class has this property it makes it callable. For example:
- Fast API
Last edited: 2023-11-11This is a python library to implement an API . Whilst there are other API implementations in python such as Flask the unique selling point of Fast API is it’s speed.
Similarly to Flask this is implemented mainly by using decorators to wrap functions to turn them into end points.
- Functions in Python
Last edited: 2023-11-11Functions in Python Index are a class and have type ‘function’. i.e.
>>> type(my_function) <class ‘function’>
They have their own namespace that gets destroyed once the function call has ended. In Python Index functions are First-class objects , meaning they can be assigned to variables.
You can see this in the below example.
- Lambda functions
Last edited: 2023-11-11These are Anonymous Functions that take multiple inputs and evaluate the output in a single statement. It has the following syntax:
lambda arguments : expressionSyntactic SugarA
lambdaexpression creates a function object just like thedefstatement.- Logging in python
Last edited: 2023-11-11# logging python
The logging module in python is used to do exactly what you would expect, it logs messages.
Some cool features it has is:
- different levels of messages for filtering of messages,
- different logger names for different parts of the application,
- different handlers to post messages such as streamed, files, http and rotating, and
- custom formatters for different handlers.
This library was converted from Java which is why it does not conform to the python naming conventions.
- Mutability in Python
Last edited: 2025-12-05In Python Index all variables are references to objects. Every object has a mutability property. Objects that are mutable in Python behave as you would expect: when a variable pointing to a mutable object is altered, the object itself is altered.
x = [1,2] y = x x.append(3) print(y) # [1,2,3]Here
xandypoint to the mutable list object. Whenxis altered to add3to the list,yalso reflects this change because both variables point to the same object.- Python Built-in Functions
Last edited: 2023-11-11# Python Builtin Functions
Python Index has some built in functions that make using the language slightly easier. You can find a full list of these in the python documentation .
# Functions
# callable(object)
This returns
TrueorFalsedepending on if the object is callable. For example- Pythonic
Last edited: 2023-11-11Pythonic means code that doesn’t just get the syntax right, but that follows the conventions of the Python community and uses the language in the way it is intended to be used. 1
For example:
for index in range(len(some_list)): some_list[index].do_something()would not be considered pythonic however
for item in some_list: item.do_something()would be as it uses the built in iteration of the python language.
- Reference counting in Python
Last edited: 2023-11-11In Python Index when objects are created, it also stores the number of references there are to that object. This is what we mean when we say the reference count of an object.
You can access this reference count by using the
syslibrary with the functionsys.getrefcount.import sys x = [1,2] print(sys.getrefcount(x)) # 2 y = x print(sys.getrefcount(x)) # 3 print(sys.getrefcount(y)) # 3 del y print(sys.getrefcount(x)) # 2(Note the numbers are 1 more than you would expect, this is due to the object
[0,1]being passed into the functionsys.getrefcountwhich then has a reference to that object also.)- Special functions
Last edited: 2023-11-11This are also known as dunder (double underscore) or magic functions. They are signified by two underscores before and after its name i.e. __len__.
These functions are not normally called by the user, the are for the python interpreter to use. For example if you call
item in some_iterable: passto get the list of items this calls
some_iterable.__iter__(). The same forlen(object)this callsobject.__len__().- Variables in python
Last edited: 2023-11-11To understand how variables work in python it is good to keep in mind the separation between computer memory and references to places in computer memory . In Python Index , variables are more like tags or labels that are attached to objects, rather than containers that store data, as they are in many other programming languages.
When you assign a value to a variable, what Python Index actually does is create an object in computer memory representing that value, and then creates a name in the appropriate namespace that points to that object. This happens regardless of whether the value is immutable (like an integer or a string) or mutable (like a list or a dictionary).
- Classes in Python