Featured Post

Python Set Operations Explained: From Theory to Real-Time Applications

Image
A  set  in Python is an unordered collection of unique elements. It is useful when storing distinct values and performing operations like union, intersection, or difference. Real-Time Example: Removing Duplicate Customer Emails in a Marketing Campaign Imagine you are working on an email marketing campaign for your company. You have a list of customer emails, but some are duplicated. Using a set , you can remove duplicates efficiently before sending emails. Code Example: # List of customer emails (some duplicates) customer_emails = [ "alice@example.com" , "bob@example.com" , "charlie@example.com" , "alice@example.com" , "david@example.com" , "bob@example.com" ] # Convert list to a set to remove duplicates unique_emails = set (customer_emails) # Convert back to a list (if needed) unique_email_list = list (unique_emails) # Print the unique emails print ( "Unique customer emails:" , unique_email_list) Ou...

How to Find Factorial in Python for Any Number

I have explained how to find factorial for a given number in Python using my own script fact.fy.
A module is created as a script file, which contains function definitions that can be called in two ways:

  • From the interpreter
  • From another script file or from another function

python factorial logic

How to import a Script from Linux to Python Console

I have written a script fact.fy

# This program illustrates the designing/creation of a module

def factorial(n):
        "This module computes factorial"
        f=1;
        for i in range (1, n+1):
                  f=f*i;
        print(f)
        return 

In interpreter...
>>> import fact       ==> Import from Linux
>>>fact.factorial(5)
120

What is script reloading?

The Python interpreter imports a module only once in a session. 

If some modifications are performed in the script, then it must be reloaded (imported) again in the interpreter for future use.

A script is a reusable component and you can add n number of functions inside of it.

Directory function in Python.

In order to see the list of function names defined in a module, Python is provided with a built-in function called dir().

It displays the list of all the function definition names as follows:

>>>dir()

['__builtins__', '__cached__', '__doc__', '__file__', 
'__loader__','__name__', '__package__','__spec__',
'fib']

Another way if we give 'module' name in dir(), you will get a list of all functions inside of it.

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)

Python placeholder '_' Perfect Way to Use it