Skip to main content

Posts

You are NOT educated. This small article will prove it.

💭 You are not educated. And so am I. Let us start the discussion by defining certain terms to be clear about what I am talking about. Literacy: Learning to read, write, and speak a language acceptable by the government of the land Knowledge: The facts prevailing in the society Education: The learning of the ideals of life and non-life We have been using all the three terms interchangeably, the reason defining them was important. Now, that I have defined the three terms, we can see the difference between them. Learning of any language that will help us to communicate within the society we reside is called Literacy. Indian education system certainly makes us literate. Learning the facts that are prevailing in the society is knowledge. According to me, the Indian education system is concentrating excessively on this aspect. Learning the ideal way life and non-life objects on earth and beyond should behave is education. Is Indian education providing education? I doubt. Let us see an examp...

Flask 1 - Set Up a Basic App

🍶 Flask is a framework to build websites. Here, we will make a simple blogging website. Below are the steps to start. Install Python  Install Pip  Install Flask Run the following code in Command Line: import flask #TO CHECK IF FLASK IS INSTALLED exit() #TOEXIT FLASK cd inqzit #TO GO TO THE FOLDER Create a python file and name it app_001001_start.py. Add the following code in it. from flask import Flask app = Flask(__name__) @app.route("/") def hello():     return "Hello World!" Run the following code in Command Line: set FLASK_APP=app_001001_start.py #TO START RUNNING THE APPLICATION SERVER set FLASK_DEBUG = 1 #TO AUTO RUN SERVER WHEN FILES UPDATED flask run #TO RUN THE SERVER Go to http://127.0.0.1:5000/ or http://localhost:5000/ in browser. Instead of set FLASK_DEBUG = 1 line, we can add a few lines of code in the python file itself for auto run of server when a file is updated. Add the following code in the file. if __name__=='__main__': app.run(deb...

Python - Lambda Expression

🐍 Did you know you can create a quick anonymous one line function? Yes. It is called Lambda Expression. Why would you need lambda? Sometimes you want to use a function just once and never again for a simple task that ends in one line, such as for sorting, that's when lambda expression comes to use. Syntax of lambda expression <variable> = lambda <input_variable>: <expression> Example sqr = lambda num: num**2 print(sqr(10))

Python - .strip(), .capitalize(), .title(), .spit()

🐍 .strip() removes certain characters from both ends of the string. .capitalize() capitalizes the first letter of the string and sets the rest of the string to lower case. .title() capitalizes first letter of every word in the string. Example Run the following code in Jupyter notebook and you will see how each of the above functions work. a = "   john SNOW   " b = "0000CAEsy0james00" print(a.strip().capitalize()+" loves "+b.strip('0').title()) .strip() .strip() removes all spaces from the front and back of a string. Not the spaces in between. .strip() has default argument as space, i.e. ' '. But if you pass another character, it will look for that character in front and back of the string and remove it. For example, if you run print('0000James0Cameron000'.strip('0')) , it will remove all 0s from front and back and print ' James0Cameron '. Note that the 0 in between remains as it is. You can pass a string too. Exa...

Python - *args and **kwargs

🐍  *args Args in *args stand for arguments. It is not necessary to use args. You could name it anything. What is important is the asterisk. You could name the variable anything, but adding an asterisk in front of it, adds one awesome functionality to it. That is, it can take any number of arguments/variables from the user and save it in the form of a tuple in the variable args or whatever you might have named it. Example def test(*args):     print(args)     print(sum(args)) a = test(1,2,3,4,5) **kwargs Kwargs is similar to args, except it takes key-value pairs and not variables as arguments. Again, it is not necessary to name it kwargs. What is necessary is the two asterisks. It takes any number of key-value pairs and stores in a dictionary. Example def test(**kwargs):     print(kwargs) test(a=1, b=str(2), c='three')

Python - Use of Assert Keyword

🐍  Assert keyword simply checks if a condition returns true or false. It is a short for writing if statement to check if something is true or false. Syntax assert <condition>, <optional message> Optional message in the syntax is optional. It is displayed in the error when the condition is false. When the condition is false, the code will also not proceed any further. It ends the program there and returns and error with the message, if it is mentioned. Example assert 25%2==0, 'Add 1 to the number'

Python - Fastest Method To Calculate Value of Pi Up To Any Precision

🐍  Here is the fastest ever method to calculate the value of Pi up to any number of decimal places as you want. It was introduced in the year 1989 by David Volfovich Chudnovsky and Gregory Volfovich Chudnovsky.  Simply change 'prec' value in the code below. Solution from decimal import Decimal as Dec , getcontext as gc def PI ( maxK = 70 , prec = 1008 , disp = 1007 ): # parameter defaults chosen to gain 1000+ digits within a few seconds gc () . prec = prec K , M , L , X , S = 6 , 1 , 13591409 , 1 , 13591409 for k in range ( 1 , maxK + 1 ): M = ( K ** 3 - 16 * K ) * M // k ** 3 L += 545140134 X *= - 262537412640768000 S += Dec ( M * L ) / X K += 12 pi = 426880 * Dec ( 10005 ) . sqrt () / S pi = Dec ( str ( pi )[: disp ]) # drop few digits of precision for accuracy print ( "PI(maxK= %d iterations, gc().prec= %d , disp= %d digits) = \n %s " % ( ...