Sunday, 21 April 2024

Python Program to find info about Chemical Formula

 

import pubchempy as pcp

# Take name as input

chemical_name = input("Enter chemical name: ")

try:

    # Search PubChem for the compound by its name

    compound = pcp.get_compounds(chemical_name, 'name')[0]

    # Display information about the compound

    print(f"IUPAC Name: {compound.iupac_name}")

    print(f"Common Name: {compound.synonyms[0]}")

    print(f"Molecular Weight: {compound.molecular_weight}")

    print(f"Formula: {compound.molecular_formula}")

    # You can access more properties as needed

except IndexError:

    print(f"No information found for {chemical_name}.")

    #clcoding.com

Explanation: 

This code snippet performs a similar task to the previous one but takes the name of a chemical compound as input instead of its chemical formula. Here's an explanation of each part:

import pubchempy as pcp: This line imports the PubChemPy library and aliases it as pcp, allowing us to refer to it more conveniently in the code.

chemical_name = input("Enter chemical name: "): This line prompts the user to input the name of the chemical compound they want to retrieve information about. The input is stored in the variable chemical_name.

try:: This starts a try-except block, indicating that we are going to try a piece of code that might raise an exception, and if it does, we'll handle it gracefully.

compound = pcp.get_compounds(chemical_name, 'name')[0]: This line uses the get_compounds function from the PubChemPy library to search for compounds matching the provided chemical name. The function takes two arguments: the chemical name (chemical_name) and a string indicating that we are searching by name ('name'). Since get_compounds returns a list of compounds, we select the first compound using [0] and assign it to the variable compound.

Printing compound information:

print(f"IUPAC Name: {compound.iupac_name}"): This line prints the IUPAC (International Union of Pure and Applied Chemistry) name of the compound.

print(f"Common Name: {compound.synonyms[0]}"): This line prints the common name of the compound, using the first synonym available.

print(f"Molecular Weight: {compound.molecular_weight}"): This line prints the molecular weight of the compound.

print(f"Formula: {compound.molecular_formula}"): This line prints the molecular formula of the compound.

except IndexError:: This block catches the IndexError exception, which occurs if no compound is found for the provided chemical name.

print(f"No information found for {chemical_name}."): If an IndexError occurs, this line prints a message indicating that no information was found for the provided chemical name.

In summary, this code allows the user to input the name of a chemical compound, searches for the corresponding compound in the PubChem database using PubChemPy, and displays information about the compound if found. If no information is found for the provided name, it prints a corresponding message.


import pubchempy as pcp

# Define the chemical formula 

chemical_formula = input("Enter chemical Formula : ")  

try:

    # Search PubChem for the compound by its chemical formula

    compound = pcp.get_compounds(chemical_formula, 'formula')[0]

    # Display information about the compound

    print(f"IUPAC Name: {compound.iupac_name}")

    print(f"Common Name: {compound.synonyms[0]}")

    print(f"Molecular Weight: {compound.molecular_weight}")

    print(f"Formula: {compound.molecular_formula}")

    # You can access more properties as needed

except IndexError:

    print(f"No information found for {chemical_formula}.")

    #clcoding.com

Explantion:

This code snippet aims to retrieve information about a chemical compound using its chemical formula. Here's a breakdown of each part:

import pubchempy as pcp: This line imports the PubChemPy library and aliases it as pcp, allowing us to refer to it more conveniently in the code. PubChemPy is a Python library for accessing chemical information from the PubChem database.

chemical_formula = input("Enter chemical Formula : "): This line prompts the user to input the chemical formula of the compound they want to retrieve information about. The input is stored in the variable chemical_formula.

try:: This starts a try-except block, indicating that we are going to try a piece of code that might raise an exception, and if it does, we'll handle it gracefully.

compound = pcp.get_compounds(chemical_formula, 'formula')[0]: This line uses the get_compounds function from the PubChemPy library to search for compounds matching the provided chemical formula. The function takes two arguments: the chemical formula (chemical_formula) and a string indicating that we are searching by formula ('formula'). Since get_compounds returns a list of compounds, we select the first compound using [0] and assign it to the variable compound.

Printing compound information:

print(f"IUPAC Name: {compound.iupac_name}"): This line prints the IUPAC (International Union of Pure and Applied Chemistry) name of the compound.

print(f"Common Name: {compound.synonyms[0]}"): This line prints the common name of the compound, using the first synonym available.

print(f"Molecular Weight: {compound.molecular_weight}"): This line prints the molecular weight of the compound.

print(f"Formula: {compound.molecular_formula}"): This line prints the molecular formula of the compound.

except IndexError:: This block catches the IndexError exception, which occurs if no compound is found for the provided chemical formula.

print(f"No information found for {chemical_formula}."): If an IndexError occurs, this line prints a message indicating that no information was found for the provided chemical formula.

In summary, this code allows the user to input a chemical formula, searches for the corresponding compound in the PubChem database using PubChemPy, and displays information about the compound if found. If no information is found for the provided formula, it prints a corresponding message.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (13) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (161) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (53) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (917) Python Coding Challenge (304) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses