Python tuple program to join tuples if the initial elements of the sub-tuple are the same

This Python Tuple program will check the initial value of all sub-tuples, if the initial value of two sub-tuple are the same, then it will merge both the tuple.

Input:
[(3,6,7),(7,8,4),(7,3),(3,0,5)]

Output:
[(3,6,7,0,5),(7,8,4,3)]

				
					# take input list value that contains multiple tuples
l1 = [(3, 6, 7), (7, 8, 4), (7, 3), (3, 0, 5)]

# initiate  a variable to store the required output
output = []

# initiate a loop with range of length of list l1.
for i in range(len(l1)):
    # initiate nested loop
    for j in range(i+1, len(l1)):
        # check any two same tuple initial values are same
        if l1[i][0] == l1[j][0]:
            # if two tuple initial value are same, then combine both tuple.
            # and store in output list.
            output.append(tuple(list(l1[i]) + list(l1[j][1:])))
        else:
            continue

print(output)
				
			
Output:
				
					# Output:
[(3, 6, 7, 0, 5), (7, 8, 4, 3)]
				
			

Python tuple program to add row-wise elements in Tuple Matrix

This Python tuple program will add a tuple of values as row-wise elements in the tuple matrix.

Input:
A = [[(‘sqa’, 4)], [(‘tools’, 8)]]
B = (3,6)

Output:
[[(‘sqa’, 4,3)], [(‘tools’, 8,6)]]

				
					var_a = [[('sqa', 4)], [('tools', 8)]]
var_b = (3, 6)
print("Input A : ", var_a)
print("Input B : ", var_b)

output = []

# initiate a loop till length of var_a
for i in range(len(var_a)):
    # get tuple value with the help of indexing of var_a and connvert into list
    l1 = list(var_a[i][0])
    # check if value of i is less than length of var_b
    if i < len(var_b):
        # append new value to the list
        l1.append(var_b[i])
    # now convert list into tuple and append to output list
    output.append([tuple(l1)])

print(output)
				
			

Output:

				
					Input A :  [[('sqa', 4)], [('tools', 8)]]
Input B :  (3, 6)

Output : 
[[('sqa', 4, 3)], [('tools', 8, 6)]]
				
			

PyCharm Configuration for Windows OS

For installing PyCharm in your System go through the following steps:

  • Open a web browser (e.g., Google Chrome, Firefox, or Edge).1 2
  • Click on first link as shown in web browser ,go to the official JetBrains PyCharm website,or click Here .2 2
  • Visit the official PyCharm website at JETBRAINS and this web page will appear3 2
  • Once the page loads, you’ll see options for different editions of PyCharm.
    • Professional Edition: Paid version with advanced features.
    • Community Edition: Free version, ideal for Python programming.4 2
  • Click the Download button under the Community Edition section.5 2
  • You’ll be redirected to the download page.The website should automatically detect your operating system (Windows) and provide the correct installer.
    • If not, ensure Windows is selected in the operating system dropdown or button.
    • Click the Download button to start downloading the PyCharm Community Edition .exe installer.6 2
  • The installer file will begin downloading. Its name will look something like pycharm-community-<version>.exe.Wait for the download to finish. The file size is usually around 300–400 MB.
    • Locate the downloaded .exe file (usually in your Downloads folder).
    • Double-click the file to start the installation process.7 2
  • A setup wizard will appear. Follow these steps:
    • Welcome Screen: Click Next.
    • Choose Installation Path: Select or confirm the default location where PyCharm will be installed (e.g., C:\Program Files\JetBrains\PyCharm Community Edition). Then, click Next.8 2
  • Installation Options:
    • Check Create Desktop Shortcut (optional).
    • Check Update PATH variable (optional but recommended for easy access to PyCharm from the command line).
    • Check Add Open Folder as Project (optional).
    • Click Next.9 2
  • Choose Start Menu Folder: Leave the default or choose a custom folder for shortcuts. Click Install.
    • 10 2The installation will begin. This might take a few minutes.
    • 11 2
  • Once the installation is complete, check Run PyCharm Community Edition if you want to open it immediately.12 2
  • Open desktop and click on PyCharm Logo.13 2
  • This Dialogue Box will appear, when you click on desktop Shortcut .14 2
  • After PyCharm opens, create a new project:
    • Click New Project.
    • Choose a location and name for the project.15 1
  • If everything works as expected, your PyCharm setup is complete.16 1
  • Right Click on your project name and select new.17
  • In New select for New file Python file.18
  • Name your Python file , here “Trial”.19
  • Write a Trial program of printing a “Hello World” ,and to run that script right click on screen & select Run and Debug.22
  • This Will be the output of the “Trial”. # printing “Hello World “23

Python installation in Windows OS:

  • Open Google Chrome or any other web browser and search for Python. 1 1 e1737919354722
  • Visit the official Python website at python.org.2 1 e1737960267614
  • Navigate to the Downloads section and select the latest stable release for Windows.Choose the appropriate installer based on your system architecture:
  • For 64-bit systems: “Windows installer (64-bit)”
  • For 32-bit systems: “Windows installer (32-bit)”3 1 e1737960433455
  • Locate the downloaded installer file (e.g., python-3.x.x-amd64.exe) and double-click to run it.4 1
  • Check the box labeled “Add Python to PATH” to ensure you can run Python from the command line.Click on “Install Now” to proceed with the default installation.5 1
  • In the “Optional Features” section, you can select additional components like:
    • Documentation
    • pip (Python package installer)
    • tcl/tk and IDLE (Python’s Integrated Development and Learning Environment)
    • Python test suite
    • py launcher6 1
  • Click “Next” and in the “Advanced Options” section, you can:
    • Choose the installation location
    • Add Python to environment variables
    • Install for all users7 1
  • After selecting the desired options, click “Install” to begin the installation.8 19 1 e1737960510964
  • Verify the Installation:
    • Open the Command Prompt:
    • Press Win + R, type cmd, and press Enter.
  • pip --version run in command prompt and python --version.14 1 e1737961172208

Python Installation for MacOS:

  • Check System Requirements: Ensure your macOS version is 10.9 or later.
  • Go To google chrome and search python

seach on google

  • Visit Python’s Official Website: Open https://www.python.org and navigate to the “Downloads” section. The website will auto-detect the appropriate version for macOS.irst page for python
  • Wait until the installation take place & navigate in download bar. 4 e1737897166784
  • Open the .pkg file.5 e1737900198771
  • Click to continue.6 e1737900590266
  • Again click on continue.7 e1737900689287
  • Now click on Agree.8
  • Now complete the processing and click on Install Button.10 e1737901071202And move the python installer package to bin12 e1737902387634
  • Now close all the tabs and open IDLE python.11 e1737902149333
  • Open IDLE shell and try a hello world Program.14 e1737902564954
  • Simple “Hello World” program.16You can also check version in Terminal by giving the following command to check the version of python.
    python3 –versionScreenshot 2025 01 26 at 11.38.27 PM

    Handle Iframes In Selenium

    To handle iframes in Selenium using Python, you need to switch the WebDriver’s context to the iframe first using the switch_to.frame() method. Here is a basic guide on working with iframes:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    import time

    # launch Chrome browser
    driver = webdriver.Chrome()

    #
    navigate to a page with an iframe
    driver.get("https://www.globalsqa.com/demo-site/frames-and-windows/#iFrame")

    #
    get iframe web element
    iframe_element = driver.find_element(By.XPATH, "//iframe[@name='globalSqa']")

    #
    switch to iframe with web element
    driver.switch_to.frame(iframe_element)

    #
    get web element of header email inside iframe
    header_email = driver.find_element(By.XPATH, "//div[@class='header_mail']")

    #
    get email from web page with text
    print(header_email.text)

    # switch default main page
    driver.switch_to.default_content()

    #
    get Tester web element from default content
    tester_hub_elem = driver.find_element(By.XPATH, "//div[@id='menu']//a[contains(text(),'Tester')]")

    #
    click on menu option
    tester_hub_elem.click()

    time.sleep(5)
    driver.close()

    Explanation:

    • driver.switch_to.frame(“frame_name_or_id”): Switches to an iframe using its name or id attribute.
    • driver.switch_to.frame(index): Switches to an iframe using its index (0-based).
    • driver.switch_to.frame(iframe_element): Switches using a WebElement representing the iframe.
    • driver.switch_to.default_content(): Switches back to the main document from an iframe.

    This approach allows you to interact with elements inside the iframe and switch between the main content and other frames when necessary.


    Handle Browser Windows/Tabs

    To handle browser tabs in Selenium using Python, you can use methods to switch between windows or tabs using their handles. Here’s a guide on how to open a new tab, switch between tabs, and close

    Example Code

    from selenium import webdriver
    import time

    #
    Initialize the WebDriver (e.g., using Chrome)
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)

    #
    Open the first URL
    driver.get("https://www.facebook.com")
    print("Current tab URL:", driver.current_url)

    #
    Save the original window handle
    original_tab = driver.current_window_handle

    #
    Open a new tab using JavaScript (useful for browsers like Chrome)
    driver.execute_script("window.open('https://www.google.com', '_blank');")

    #
    Get the list of all window handles (tabs)
    window_handles = driver.window_handles
    print("All window handles:", window_handles)

    #
    Switch to the new tab
    driver.switch_to.window(window_handles[1])
    print("Switched to new tab URL:", driver.current_url)

    #
    Pause for a few seconds (optional)
    time.sleep(2)
    # close google page
    driver.close()

    #
    Switch back to the original tab
    driver.switch_to.window(original_tab)
    print("Switched back to original tab URL:", driver.current_url)

    time.sleep(2)

    #
    Close the browser
    driver.quit()

    Explanation:

    • driver.window_handles: Returns a list of window handles (IDs) for all the open tabs/windows.
    • driver.switch_to.window(handle): Switches the WebDriver’s focus to the specified window handle.
    • driver.execute_script(“window.open(‘URL’, ‘_blank’);”): Opens a new tab using JavaScript.
    • To close a specific tab, you can use driver.close() while the focus is on that tab.

    This approach allows you to manage multiple tabs efficiently, such as switching between them or closing specific ones as needed.


    Take Screenshot Selenium

    To take screenshot using Selenium in Python, you can use the save_screenshot() method or get_screenshot_as_file() method. Here’s how you can do it:

    Example Code

    from selenium import webdriver
    from selenium.webdriver.common.by import By

    #
    Initialize the WebDriver (e.g., using Chrome)
    driver = webdriver.Chrome()

    #
    Navigate to a website
    driver.get("https://www.google.com")

    #
    Take a screenshot and save it as a file
    screenshot_path = "google_page.png"
    driver.save_screenshot(screenshot_path)
    #
    Or driver.get_screenshot_as_file(screenshot_path)

    print(f"Screenshot saved at: {screenshot_path}")

    search_field = driver.find_element(By.NAME, "q")

    #
    Take a screenshot of the specific element
    search_field.screenshot("search_field.png")

    #
    Close the browser
    driver.quit()

    Explanation:

    • driver.save_screenshot(“filename.png”): Saves a screenshot of the current browser window with the specified filename.
    • element.screenshot(“filename.png”): Saves a screenshot of the specific element with the specified filename.
    • You can use any valid file path or name as the argument to save the screenshot where you want.

    Forward, Back and Refresh Methods

    In Selenium with Python, you can use the back(), forward(), and refresh() methods to navigate through browser history and reload pages. Here’s a quick overview of how to use them

    Example Code

    from selenium import webdriver
    import time

    #
    Initialize the WebDriver (e.g., using Chrome)
    driver = webdriver.Chrome()

    #
    Navigate to a URL
    driver.get("https://www.google.com")
    print("Initial URL:", driver.current_url)

    #
    Navigate to another URL
    driver.get("https://www.facebook.com")
    print("Navigated to:", driver.current_url)

    #
    Go back to the previous page
    driver.back()
    print("Back to:", driver.current_url)
    #
    Back URL should be www.google.com

    #
    Wait for 2 seconds
    time.sleep(2)

    #
    Move forward in the browser history
    driver.forward()
    print("Forward to:", driver.current_url)
    # Move forward url to www.facebook.com

    #
    Wait for 2 seconds
    time.sleep(2)

    #
    Refresh the current page ( facebook.com )
    driver.refresh()


    #
    Refresh facebook URL
    print("Page refreshed at:", driver.current_url)

    #
    Close the browser
    driver.quit()

    Results:

    Initial URL: https://www.google.com/
    Navigated to: https://www.facebook.com/
    Back to: https://www.google.com/
    Forward to: https://www.facebook.com/
    Page refreshed at: https://www.facebook.com/

    Explanation:

    • driver.back(): Navigates back to the previous page in the browser history.
    • driver.forward(): Moves forward in the browser history.
    • driver.refresh(): Refreshes/reloads the current page.

    Get Current URL

    To get the current URL in Python using Selenium, you can use the current_url property of the WebDriver instance. Here’s how you can do it:

    from selenium import webdriver

    # Example using Chrome WebDriver
    driver = webdriver.Chrome()

    # Navigate to a website
    driver.get("https://sqatools.in/")

    # Get the current URL
    current_url = driver.current_url
    print("Current URL:", current_url)

    # Close the browser
    driver.quit()

    This will print the current URL of the page that the WebDriver is on.