Python Cheatsheet 1 Collections Python Lists A list is an ordered and mutable container; it is arguably the most common data structure in Python. Understanding how lists work becomes even more relevant as you work on data cleaning and create for-loops. list = list[from_inclusive : to_exclusive : ±step_size] list.append(el) # Or: list += [el] list.extend(collection) # Or: list += collection list.sort() list.reverse() list = sorted(collection) list = reversed(list) sum_of_elements = sum(collection) elementwise_sum = [sum(pair) for pair in zip(list_a, list_b)] sorted_by_second = sorted(collection, key=lambda el: el[1]) sorted_by_both = sorted(collection, key=lambda el: (el[1], el[0])) flatter_list = list(itertools. Python Development
NumPy Illustrated: The Visual Guide to NumPy NumPy is a fundamental library that most of the widely used Python data processing libraries are built upon pandas , inspired by PyTorch , or can efficiently share data with (TensorFlow , Keras , etc). Understanding how NumPy works gives a boost to your skills in those libraries as well. It is also possible to run NumPy code with no or minimal changes on GPU . Python Development
Python decorator to parallelize any function Wouldn’t it be cool if you can speed up your program by just adding a decorator to the function? Wouldn’t it be cool if you don’t have to worry about running the data in a list as a parallelly? Today we are going to write a python decorator which exactly does these automatically for you, so that you can concentrate more on the logics of your code than worrying about multi-threading issues. Python Development
Python Basics - Mutable vs Immutable Objects Introduction - Objects, Values, and Types All the data in a Python code is represented by objects or by relations between objects. Every object has an identity, a type, and a value. Identity An object’s identity never changes once it has been created. You may think of it as the object’s address in memory. The is and is not operator compares the identity of two objects. The id() function returns an integer representing its identity. Python Development
Python super() metod There are a lot of content out there explaining and showing examples of how to use Python’s super() method: Official documentation Raymond Hettinger’s post Supercharge your classes with Python super Still, after reading all of them, I wasn’t exactly sure which attribute to pass to the super() method in order to invoke a method from a certain class on the inheritance chain. Example Let’s imagine the following inheritance tree. python super() method example The code for classes A, B and C follows: Python Development
Tips and Tricks for Python Lists This article is for Python programmers who have gone beyond the basics of using lists, but would like to see some interesting concepts. It isn’t very advanced and should be easy for you to understand. Ensure that a List Contains Unique Elements. Sets contain unique elements but are unordered. On the other hand, lists can contain duplicate elements but retain their insertion order. This example shows one way to get the benefits of both sets and lists — an ordered sequence of unique elements. Python Development
The Definitive Guide to Python import Statements Summary / Key Points import statements search through the list of paths in sys.path sys.path always includes the path of the script invoked on the command line and is agnostic to the working directory on the command line. importing a package is conceptually the same as importing that package’s __init__.py file Basic Definitions module: any *.py file. Its name is the file name. built-in module: a 'module' (written in C) that is compiled into the Python interpreter, and therefore does not have a *. Python Development
CheatSheet - Python File Operations Quick reference on how to read and write files. This short tutorial isn’t intended to cover everything in file reading and writing in Python. Instead, I just wanted to share with you some cheat sheets that I created for reading and writing files in my daily Python programming. Python Development
Python - How To Refresh an JWT Access Token Using Decorators When you create a big python application, you can began to run into challenges with longer scripts that extended beyond the expiration of a single JWT. To elegantly solve this, You can use decorators to check the token’s expiration and request a new token if necessary. This article goes over the framework I set up so that you can apply a similar mechanism in your own scripts. Setting the Scene import time import requests class myAPI(): host = None key = None secret = None access_token = None access_token_expiration = None def __init__(self,host,key,secret): # the function that is executed when # an instance of the class is created pass def getAccessToken(self): # the function that is # used to request the JWT pass class Decorators(): @staticmethod def refreshToken(decorated): # the function that is used to check # the JWT and refresh if necessary pass Our class will work by requiring the fields necessary to request the JWT. Python Security Development
Python Slice Notation Guide Slice notation in Python is used for selecting a range of items from a sequence such as a list, tuple, or string. In this article, we’ll explore slice notation in detail and provide examples of how to use it in your Python code. By understanding slice notation, you’ll be able to work more effectively with sequences in your Python programs. Python Development
How To Refresh an Access Token Using Decorators When I was creating a one script, which uses JWT for authentication, I began to run into challenges with longer scripts that extended beyond the expiration of a single JWT. To elegantly solve this, I used decorators to check the token’s expiration and request a new token if necessary. This article goes over the framework I set up so that you can apply a similar mechanism in your own scripts. Python Development Networking
How create bar chart race with python Recently, I come across a medium post on announcement off Official Release of bar_chart_race by Ted Petrou. In his article, he provides an excellent tutorial on how to create Bar Chart Race using bar_chart_race package. Check out the official document here . In our example we use a World Population from 1955 to 2020 dataset from kaggle or you can directly download dataset here . Installation of Bar Chart Race package pip3 install bar_chart_race pandas or using anaconda: Python Development