Skip to content

Instantly share code, notes, and snippets.

@sreeragh-ar
Created June 23, 2021 14:07
Show Gist options
  • Save sreeragh-ar/7facbf6aace844692eb0dd8f32ee5d4c to your computer and use it in GitHub Desktop.
Save sreeragh-ar/7facbf6aace844692eb0dd8f32ee5d4c to your computer and use it in GitHub Desktop.
Python_quick_tips.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Python_quick_tips.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMADIXxM9egOiBQJazQOcNR",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/sreeragh-ar/7facbf6aace844692eb0dd8f32ee5d4c/python_quick_tips.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "N5ENiwdpElh6"
},
"source": [
"## Python Quick Tips\n",
"**Note**:\n",
"Goal of this doc is only to make reader aware of few built in methods/modules in Python.\n",
"\n",
"The examples used are just for demonstration purpose and should not be reagrded as complete. Reader is encouraged to check the official documentation of modules to get a complete picture of each module. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zH3hXWYcHWlm"
},
"source": [
"## Get quotient and remainder with a single operation"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xp5RnHnh28vy",
"outputId": "d5f8b396-0303-46f1-c2e8-83487186b4be"
},
"source": [
"## divmod()\n",
"\n",
"dividend = 53\n",
"divisor = 10\n",
"\n",
"quotient, remainder = divmod(dividend, divisor)\n",
"print('Quotient:', quotient) \n",
"print('Remainder:', remainder)\n",
"\n"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Quotient: 5\n",
"Remainder: 3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vnx9w4iZHnYu"
},
"source": [
"## Insert to a sorted list"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Gh3O3IaC4Nyh",
"outputId": "5ceaf362-7a42-45b3-8f94-51ba8967a258"
},
"source": [
"## bisect module\n",
"\n",
"import bisect\n",
"\n",
"sorted_list = [1,3,10]\n",
"new_elem = 5\n",
"bisect.insort(sorted_list, new_elem)\n",
"\n",
"sorted_list"
],
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 3, 5, 10]"
]
},
"metadata": {
"tags": []
},
"execution_count": 2
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Coap2rlIHu4U"
},
"source": [
"## Check if a given year is leap year or not"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ibqdOc7W6PX0",
"outputId": "62586a66-9e8f-4637-d8d2-3fee6fc68f27"
},
"source": [
"## calendar module\n",
"\n",
"import calendar\n",
"\n",
"calendar.isleap(2003)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 3
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T9IBQtjYH7mz"
},
"source": [
"## Get unique elements (remove duplicate elements) of a list"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OHFooAMv7EKC",
"outputId": "94059859-5932-401b-ab5e-861a8c395ce5"
},
"source": [
"big_list = [1,2,1,1,2,5]\n",
"unique_list = list(set(big_list))\n",
"\n",
"unique_list"
],
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 2, 5]"
]
},
"metadata": {
"tags": []
},
"execution_count": 4
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0VqMct6VIKYl"
},
"source": [
"## Reverse a list"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FmVl-YyY729e",
"outputId": "50970bea-ab56-430c-aba9-62f062328b6f"
},
"source": [
"a_list = [1,2,3]\n",
"rev_list = a_list[::-1]\n",
"\n",
"rev_list"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[3, 2, 1]"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V7tfAOd4IP2u"
},
"source": [
"## Get elements from odd (or even) indices"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6lFvQxPp8JK9",
"outputId": "65167fe2-607f-490c-db38-3213852d124a"
},
"source": [
"pairs = ['John', 'Maria', 'Tom', 'Mary', 'Charles', 'Rose']\n",
"wifes = pairs[1::2] # odd indices - start from 1 and take step size of 2\n",
"husbands = pairs[0::2] # even indices - start from 0 and step size of 2\n",
"\n",
"print('wifes:', wifes)\n",
"print('husbands:', husbands)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"wifes: ['Maria', 'Mary', 'Rose']\n",
"husbands: ['John', 'Tom', 'Charles']\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l3Nc_zXXIZsP"
},
"source": [
"## Convert decimal to binary, octal or hexadecimal and back."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pFFoa0DQ-YbB",
"outputId": "58d9702e-d500-4586-a93d-b4435720c188"
},
"source": [
"## int(), bin(), oct(), hex()\n",
"\n",
"decimal = 13\n",
"\n",
"binary = bin(decimal) # 0b will be present infront of the actual value \n",
"octal = oct(decimal) # 0o will be present infront of the actual value \n",
"hexa_decimal = hex(decimal) # 0x will be present infront of the actual value\n",
"\n",
"\n",
"print('Decimal:', decimal)\n",
"print('Binary:', binary)\n",
"print('Octal:', octal)\n",
"print('Hexa decimal', hexa_decimal)\n",
"\n",
"# convert back to decimal - use int() - and pass the base appropriately\n",
"int(binary, base=2) == int(octal, base=8) == int(hexa_decimal, base=16) == decimal"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"Decimal: 13\n",
"Binary: 0b1101\n",
"Octal: 0o15\n",
"Hexa decimal 0xd\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vpy7ntjaI4rT"
},
"source": [
"## Get all permutations of a list"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QSohjNisCuw4",
"outputId": "ee5fd4c1-87ae-4f89-cc5d-f6ebfb2b8570"
},
"source": [
"## itertools.permutations()\n",
"\n",
"import itertools\n",
"a_list = [1,4,2]\n",
"\n",
"for perm in itertools.permutations(a_list):\n",
" print(perm)"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"(1, 4, 2)\n",
"(1, 2, 4)\n",
"(4, 1, 2)\n",
"(4, 2, 1)\n",
"(2, 1, 4)\n",
"(2, 4, 1)\n"
],
"name": "stdout"
}
]
}
]
}
@Syltaris
Copy link

👍

@joelreji
Copy link

Looks great! Thanks!

@Chief-Aathil
Copy link

👍Thanks. Hoping for more

@j4jessay
Copy link

Great. Thanks

@RamuPittala
Copy link

Great. Hope to see many in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment