Found 33676 Articles for Programming

What is the use of "from...import *" Statement in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:48:52

2K+ Views

The "from module import *" statement is used to import all function from a Python module. For example, if you want to import all functions from math module and do not want to prefix "math." while calling them, you can do it as follows:>>> from math import * >>> sin(0) 0.0 >>> cos(0) 1.0Note that for any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy ... Read More

What is the use of "from...import" Statement in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:49:27

2K+ Views

The "from module import function" statement is used to import a specific function from a Python module. For example, if you want to import the sin function from the math library without importing any other function, you can do it as follows:>>> from math import sin >>> sin(0) 0.0Note that you don't have to prefix sin with "math." as only sin has been imported and not math. Also you can alias imported functions. For example,>>> from math import cos as cosine >>> cosine(0) 1.0

How to import a single function from a Python module?

Yaswanth Varma
Updated on 28-Aug-2025 13:06:30

10K+ Views

In Python, the module is a file containing Python definitions and statements. For keeping the code organized, we often write functions in separate modules and import them into main program. While importing the entire module, sometimes we only need a single function from it. Python provides a direct way to do this using the from module_name import function_name syntax. Importing sqrt() from math module In this approach, we are going to use from math module importing the sqrt to import only the sqrt function. The Python sqrt() function is used to calculate the square root of ... Read More

How to do multiple imports in Python?

Rajendra Dharmkar
Updated on 10-Aug-2023 20:49:45

10K+ Views

Importing modules is an essential part of programming in Python, and it's something you'll do a lot as you develop your skills. In Python, you can import multiple modules using a few different methods. Here are some examples of how to do it: Import each module on a separate line The simplest way to import multiple modules in Python is to import each one on a separate line. For example, suppose you want to import the math, random, and time modules. You could do it like this: Example This code tells Python to import the math, random, and time modules ... Read More

How to use multiple modules with Python import Statement?

Rajendra Dharmkar
Updated on 11-Aug-2023 14:16:53

8K+ Views

In Python, you can use the import statement to use functions or variables defined in another module. Here are some code examples that demonstrate how to use multiple modules with Python import statement: Suppose you have two modules module1.py and module2.py that contain some functions: Example #module1.py def say_hello(name): print("Hello, " + name + "!") #module2.py def say_goodbye(name): print("Goodbye, " + name + "!") To use these modules in another Python program, you can import them using the import statement: import module1 import module2 module1.say_hello("John") module2.say_goodbye("Jane") ... Read More

What is the use of import statement in Python?

Rajendra Dharmkar
Updated on 11-Aug-2023 15:10:44

2K+ Views

The import statement in Python is used to bring code from external modules or libraries into your program. This is a powerful feature that allows you to reuse code and avoid duplicating code across multiple programs. Here are some examples of how to use the import statement: Import a single module Let's say you want to use the math module in your program, which provides a variety of mathematical functions. To import the math module, you simply use the import statement followed by the name of the module: Example In this example, we import the math module and use the ... Read More

What is the difference between a python module and a python package?

Rajendra Dharmkar
Updated on 11-Aug-2023 14:36:34

3K+ Views

In Python, both modules and packages are used to organize and structure code, but they serve slightly different purposes. A Python module is a single file containing Python code that can be imported and used in other Python code. A module can define variables, functions, classes, and other Python constructs that can be used by other code. Modules are a great way to organize code and make it reusable across multiple programs. A Python package, on the other hand, is a collection of related Python modules that are organized in a directory hierarchy. A package can contain one or more ... Read More

What is zfill() method in Python?

SaiKrishna Tavva
Updated on 06-Mar-2025 18:51:51

269 Views

The zfill() method in Python is used to pad a string with leading zeros to achieve a specified total length. If the string is already longer than the specified length, zfill() does nothing. Syntax Following is the syntax for the zfill() method string.zfill(width) Consistent File Naming Consistent file naming with leading zeros is crucial for sequential processing scripts because it ensures correct sorting and handling of files. Example Here, even though i ranges from 1 to 10, each number is formatted to have three digits with leading zeros, ensuring consistent filename length. for i in range(1, 11): ... Read More

How to detect vowels vs consonants in Python?

Yaswanth Varma
Updated on 13-Jun-2025 17:39:56

6K+ Views

In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter is a ... Read More

How to delete consonants from a string in Python?

Yaswanth Varma
Updated on 28-Aug-2025 12:32:39

1K+ Views

The consonants are the alphabetic characters that is not a vowel (a, e, i, o, u). In Python, String manipulation is the common task, In this article we will explore how to delete consonants from a string using different approaches, including loops, list comprehensions and regular expression. Using Loop In this approach, we are going to declare the variable assignment with vowels in both upper and lower case and then looping through each character in the string. If the character is vowel or a non-alphabet character we will retrieve them and add to result. Example Let's ... Read More

Advertisements