Menu

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Different ways of writting loops in Python

Lets see we have a list of inetgers name x. 

x = [3,3,3,4,4,4,3,3,5,5,6,6,7,8,9,9,9]

Now iterate over this list using differnet loops.


  1. For loop

          #To read all  the elements from list

for elem in x:
    print(elem)


       #To read element from specific index using range(startindex, stopindex)

        for i in range(2, len(x)):

            print(x[i])

 

  1. While loop
    i = 4
while i < len(x):
    print(x[i])
    i+=1

    Pyhton Function sample code

    Different ways to manage fucntion parameter in Python.

    def paragraph(font,background,fontSize,color):
        print('Font:',font,', Background',background,', Font Size',fontSize,', Font Color',color)
        
    paragraph('Ariel','red','18px','white')
    
    
    # Fucntion parameters with default value
    def paragraph(font='Monoview',background='grey',fontSize='18px',color='blue'):
        print('Font:',font,', Background:',background,', Font Size:',fontSize,', Font Color:',color)
        
    paragraph('Ariel','red','white')
    
    
    # Fucntion parameters with following the sequence of paramters
    def paragraph(font='Monoview',background='grey',fontSize='18px',color='blue'):
        print('Font:',font,', Background:',background,', Font Size:',fontSize,', Font Color:',color)
        
    paragraph(color='red',background='orange')
    

    python logo

    How to create virtual environment in python

    A Python virtual environment, often referred to as "virtualenv," is a tool that allows Python developers to create isolated and self-contained environments for their Python projects. Each virtual environment acts as a sandbox, providing a separate space with its own Python interpreter and package dependencies, isolated from the system-wide Python installation.

    The primary purpose of using virtual environments is to manage project dependencies efficiently. With Python virtual environments, we can install Python packages in a separate and isolated location, distinct from your system-wide installations. Different projects may require specific versions of Python packages, and conflicts can arise when installing packages globally on the system. Virtual environments help avoid these conflicts by creating separate environments for each project, ensuring that the project's dependencies do not interfere with one another.

    Key features and benefits of Python virtual environments include:

    1. Isolation: Each virtual environment contains its own Python interpreter and library dependencies, isolating it from the system's Python installation and other virtual environments.

    2. Dependency Management: Virtual environments allow developers to install and manage project-specific dependencies without affecting the system-wide Python installation.

    3. Version Compatibility: Different projects may require specific versions of Python packages. With virtual environments, you can easily set up the required versions for each project.

    4. Reproducibility: By using virtual environments, you can ensure that other developers working on the project can replicate the exact environment to maintain consistency and avoid compatibility issues.

    Steps to create virtual environment

    Creating a virtual environment is straightforward. In Python 3 and above, you can use the built-in module `venv` to create a new virtual environment. Here's a simple example of creating and activating a virtual environment:

    1. Open a terminal or command prompt.

    2. Navigate to your project directory.

    3. Create the virtual environment:

       python -m venv myenv


    4. Activate the virtual environment:

       - On Windows:

         myenv\Scripts\activate

       - On macOS and Linux:

         source myenv/bin/activate

    Once activated, any Python packages installed using `pip` will be isolated within the virtual environment. When you are done working on your project, you can deactivate the virtual environment using the command `deactivate`.

    Using Python virtual environments is a best practice in Python development, as it promotes a clean and organized approach to managing project dependencies and ensures a smooth and hassle-free development experience.  

    A quick video tutorial of creating python virtual environment.

                            


    References

    PythonLand virtual environments


    Why we need Python to run Node.js?

    Node.js is written on c++, and is build on GYP. GYP is a Meta-Build system: a build system that generates other build systems and help us to build the large project that needs multiple platform to build. GYP is developed and written using Python, so to build and run few modules of Node.js we need Python.

    Therefore wherever we need node-gyp to build any node module, Python will be required.

    References: