Sample codes in Python

GCD & LCMGreatest common divisor (GCD) & Lowest Common Multiple (LCM).
factorialFactorial of an input number by looping and by recursive functions
factorsFactors of an input number
Prime NumbersPrime Numbers
FibonacciFibonacci numbers by looping and by recursive functions
Sum of DigitsSum of digits of an input number
Multiplication tableMultiplication table using nested for loops
strong numberCheck input number is strong number or not
Armstrong numberCheck input number is Armstrong number or not
Digit CubesNumber equal to sum of cubes of its digits
recursionGetting and setting recursion limits
DateExercise on Date & time
SalaryEmployee Salary calculation

Sum of three user input numbers

int() to get Integer from String
a=int(input("Enter first Number"))
b=int(input("Enter second Number"))
c=int(input("Enter third Number"))
sum=a+b+c
print(sum)

Enter mark to get status of your division

 m=int(input("Enter your marks"))
if(m>=90):
    print("You have Passed with First Distinction")

else:
    if(m>=60):
        print("You have passed with Second Distinction")
    else:
        if(m>=35):
            print("You have just passed work hard next time")
        else:
            print("Failed")
In place of using multiple if else , it is better to use elif.
Read more how to use elif to get the grade from input mark.

Converting Decimal to Binary number

def my_binary(n):
    i=''
    while n>1:
        a = n%2
        n = n//2
        i=str(a)+i
    i=  str(n%2) +i   
    print (i)
    
a=my_binary(156) 

round a number without using round() function

x=float(input("input number "))
y=int(x+0.5)
print(y)


PageSpeed Insights (PSI)

PageSpeed Insights analyzes the content of a web page and generates suggestions to make it faster. This service evaluates web performance using both lab and field data to provide recommendations for improvement.

By leveraging both real-user experiences and simulated environments, PageSpeed Insights offers a comprehensive view of a page's performance. This helps developers optimize their sites, ensuring faster load times and a better user experience. For more information, visit Google's PageSpeed Insights.
import urllib.request, json
key='YOUR_API_KEY' # Use your API key here 

x = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url="
x= x + 'https://www.plus2net.com/python/pdf-grid.php'
x= x + '&strategy=desktop' # other value is mobile 
x= x + '&locale=en'
x= x + '&key='+key

response = urllib.request.urlopen(x)
data = json.loads(response.read()) # get the return Json data 
score = data["lighthouseResult"]["categories"]["performance"]["score"]
print(score) 
  • FCP (First Contentful Paint): The time it takes for the first piece of content to appear on the screen.
  • LCP (Largest Contentful Paint): Measures the loading performance of the largest content element visible in the viewport.
  • CLS (Cumulative Layout Shift): Evaluates the visual stability by measuring unexpected layout shifts during the page load.
  • INP (Interaction to Next Paint): Measures the responsiveness by evaluating the time from user interaction to the next frame painted.
fcp = data["loadingExperience"]["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]["percentile"] 
fid = data["loadingExperience"]["metrics"]["FIRST_INPUT_DELAY_MS"]["percentile"]
lcp = data["loadingExperience"]["metrics"]["LARGEST_CONTENTFUL_PAINT_MS"]["percentile"]
cls = data["loadingExperience"]["metrics"]["CUMULATIVE_LAYOUT_SHIFT_SCORE"]["percentile"]/100

print('fcp:',fcp,'fid: ',fid,'lcp:',lcp,'cls:',cls) 
Learn Python basics through ONLINE classes


Podcast on Python Basics


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer