Featured Post

15 Python Tips : How to Write Code Effectively

Image
 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.     Python Tips for Effective Coding 1. Code Readability and PEP 8  Always aim for clean and readable code by following PEP 8 guidelines.  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly. 2. Use List Comprehensions List comprehensions are concise and often faster than regular for-loops. Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value. 3. Take Advantage of Python’s Built-in Libraries  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists. 4. Use enumerate Instead of Range     When you need both the index and the value in a loop, enumerate is a more Pyth

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

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Python placeholder '_' Perfect Way to Use it