Found 10476 Articles for Python

Absolute and Relative Imports in Python

Pradeep Elance
Updated on 30-Jul-2019 22:30:26

658 Views

Many times when we create python code we find that we need to access code from another python file or package. This is when you need to import that other python file or package into your current code. So the straight forward way to achieve this is just written the below statement at the top of your current python program.import package_name or module_name or from pacakge_name import module_name/object_nameWhen the above statement is parsed the interpreter does the following.The interpreter will look for names in the cache of all modules that have already been imported previously. The name of this cache ... Read More

a.sort, sorted(a), np_argsort(a) and np.lexsort(b, a) in Python

Pradeep Elance
Updated on 30-Jun-2020 08:34:19

694 Views

Ordering of data elements in a specific order is a frequently needed operation. To sort elements in an array, python uses the functions named sorted() and array.sort().sorted(array)This function returns a sorted array without modifying the original array.a = [9, 5, 3, 1, 12, 6] b = sorted([9, 5, 3, 1, 12, 6]) print "Sorted Array :", print (b) print "Original Array :", print (a)Running the above code gives us the following result −Sorted Array : [1, 3, 5, 6, 9, 12] Original Array : [9, 5, 3, 1, 12, 6]list.sort()The sort function returns a sorted array by doing in-place modification ... Read More

__name__ (A Special variable) in Python

Sarika Singh
Updated on 13-Jul-2025 00:07:41

3K+ Views

Python does not require a main function to start execution like many other programming languages. Instead, it uses a special built-in variable called __name__ to determine how a Python script is being executed (directly or, is it imported as a module into another script). In this article, we will learn about the __name__ variable in Python. Understanding the __name__ Variable The __name__ variable is a built-in variable that holds the name of the current module. When you run a script directly, Python sets __name__ to __main__. If the same script is imported into another file as a module, __name__ ... Read More

Using Iterations in Python Effectively

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

229 Views

In this article, we will learn about how to implement iterators and their effective implementation in Python 3.x. Or earlier. Let’ s take a look at various methods available in python which implements iterators.Type 1 − Implementation Of While Loop With Known LengthExample Code Live Demogenre = ("Python", "C", "C++", "Java") print("The topic available on Tutorial's Point are:") i = 0 while (i < len(genre)): print (genre[i]) i += 1ExplanationDue to its less compact structure, this method is not favored. Error Handling is also difficult in this case . Large-scale programs or designs doesn’t use ... Read More

Using List as Stack and Queues in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

6K+ Views

In this article, we will learn about Stack & Queue structures in Python 3.x. Or earlier. Here we will discuss the working and modification within these data structures −This includes −Insertion operation (Push, Enqueue)Deletion operation (Pop, Dequeue)Display / Traversing OperationPrerequisites: List & List OperationsRelated Data Structure: List ManipulationRelated ImagesStackIn stacks, objects are stored one over another, and these objects get removed in the reverse order of the arrival i.e. LIFO concept is followed. LIFO means Last in First Out type arrangement is followed in the Stack data structure.Operations on a Stack −Addition / Appending of Element: This increases the stack ... Read More

Using Set() in Python Pangram Checking

karthikeya Boyini
Updated on 30-Jun-2020 08:14:53

375 Views

In this article, we will learn how to determine whether a string is “pangram” or not in Python 3.x. Or earlier. A pangram string contains every letter in the list of English language alphabets . Let us look at the illustration below −Provided Input: str = 'This is the python blog on Tutorial point' Desired Output: No Provided Input : str='I want to contribute to a 'dxyzwuvghlkfmq' open source project' Desired Output: YesBy definition, a perfect pangram includes every letter of the ‘26 English alphabets’ exactly once. This tutorial doesn’t include the concept of ‘perfect pangram’.Now Let’s look at the ... Read More

vars() function in Python

Syed Javed
Updated on 30-Jun-2020 08:13:57

707 Views

vars() function belongs to the collection of inbuilt functions provided by the Python standard library. It returns the __dic__ attribute of an associated object to the console.Syntaxvars(object)Return TypeParametersvars() function accepts only one parameter. It takes an object as its parameter which can be any module, class or any object having __dict__ attribute associated with it.This parameter is optional in nature. In case the function is used without parameters A dictionary containing local symbol table is displayed.Exceptions InvolvedIf the argument passed doesn’t match the attribute, it raises the TypeError exception.ScopeVars() acts like locals() method when no argument is passed.The locals() method ... Read More

Python zip() Function

Akshitha Mote
Updated on 22-Jan-2025 14:28:28

613 Views

The zip() function in Python is used to map elements from multiple iterables at corresponding index positions. It returns an iterator of tuples, where each tuple contains elements from the input iterables at the same index. Suppose the lengths of the input iterables are not the same. In that case, the zip() function will stop pairing once the shortest iterable is exhausted, ignoring any extra elements from the longer iterables. Following is the syntax of the zip() function in Python − zip(iterable1, iterable2) It accepts iterables as a parameter and returns a single iterable. Using zip() with list list ... Read More

Custom len() Function In Python

Hafeezul Kareem
Updated on 30-Jul-2019 22:30:26

814 Views

Let's see how can we implement custom len() function in Python. Try it by yourself first using the following steps.StepsGet the iterator from the user string/list/tuple.Define a function with a custom name as you like and invoke it by passing the iterator.Initialize the count to 0.Run a loop until it reaches the end.Increment the count by 1Return the count.Example Live Demo## function to calculate lenght of the iterator def length(iterator):    ## initializing the count to 0    count = 0    ## iterating through the iterator    for item in iterator:       ## incrementing count       ... Read More

Why importing star is a bad idea in python

Hafeezul Kareem
Updated on 29-Jun-2020 14:05:12

510 Views

Importing all methods from a module in Python is a bad idea because of the following reasons.It is difficult to find a parent module of the method which we used in the programs.We are not allowed to create our functions with the names of methods.Let's see an example. Below we write a function called add in the sample.py.## sample.py file def add(a, b): return a + bExampleSave the above file in the same directory as below Python file.## let's assume we have module called sample from sample import * def add(*nums):    return sum(nums) print(add(1, 2, 3, 4, ... Read More

Advertisements