Print with python f string format

We use make the python print more wonderful with f string (formatted string) x=1 y=2 z=x+y print(f’result {x}+{y}={z}’) Copy Code this will print We can make it more wonderful directly adding expression x=1 y=2 print(f'{x} times {y} is {x * y}’) Copy Code We can also set the decimal places in the print number = […]

Check if a key exist or not in a python dictionary

We can check it with “in” operator and “get()” method In operator here returns the value True or False For example dictionary={“one”:1, “two”:2} if “two” in dictionary: print(“two exist in the dictionary”) Copy Code The get() method returns the value if that key exists. If not exists it returns None dictionary_data={“one”:1, “two”:2} #Here the key […]

Find out median of a list with python

Python has wonderful built-in library “statistics” We can find out the median of a list with this library easily with less code import statistics List = [1,2,3,4,5,6,7,8,9,10] result= statistics.median(List) print(result) Copy Code We can also generate list randomly with random module List = [randint(1, 101) for _ in range(50)] this code create a list of […]

How to format JSON with python

We can format minified json with python. Python has built in JSON module. We will add indentation in the minified json to format it. First example format JSON add space import json minified_json_example = ‘{“text”:”Hi There”,”type”:”greeting”,”word”:”2″}’ #convert it to python dictonary python_dictionarty = json.loads(minified_json_example) #We will use 4 space indentation to format and convert it […]

How to run python file in terminal

Open the terminal from you script directory ( Go to the folder where your python script is or the script you want to run. [ after going to the folder, click right mouse button … you find open in terminal option]) and run python3 python_file_name.py Copy If you know your python file location you can […]

How to HTML open link in new in tab in browser

We can add this target=”_blank” in the <a> tag to open link in new tab Example code <!DOCTYPE html> <head> </head> <body> &lt!– Opens in the same tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page”>Visit Wikipedia (same tab)</a> <br> &lt!– Opens in a new tab –> <a href=”https://en.wikipedia.org/wiki/Main_Page” target=”_blank”>Visit Wikipedia (new tab)</a> </body> </html> Copy Code

Change button text with jQuery

Its essential to change t he button text in some applications . For example Like button. If the current is “Like”, after click on the button it the button text will be “Liked” jquery makes process efficient. <!DOCTYPE html> <html> <head> <script src=”https://code.jquery.com/jquery-3.7.1.min.js”></script> </head> <body> <button class=”like-button”>Like</button> <script> $(document).ready(function(){ $(‘.like-button’).click(function(){ var buttonText = $(this).text(); if […]

Zip and unzip files with python

Its easy to zip and unizip files with python built in zipfile module For this we have to get the file location list . which files we wan to zip. we can use python glob to get the path current example structure import glob files=glob.glob(“NewFolder/*”) print(files) Copy Code It will print the result like this […]

Flask session simplest example

Flask sessions are useful when we want to remember a specific user. For example to know the user already visited the website before. So we can generate dynamic content for that user. Or we can use login based system In this code we will use the simplest flask session example Run the code go the […]

Convert a String to uppercase or lowercase with python

If our string is “hello”we can make it upper with upper()s=”hello” s.upper() it will be HELLO And in the same way if we want to make it lowercase we will just use lower() s.lower() It will again hello The code x=”Hello” print(x.lower()) #hello print(x) #Hello x=x.lower() print(x) #hello Copy Code