Found 10538 Articles for Python

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 do multiple imports in Python?

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

9K+ 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

2K+ 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

181 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 check if a character in a string is a letter in Python?

SaiKrishna Tavva
Updated on 25-Mar-2025 14:30:20

11K+ Views

In Python, there are several methods to check if a given character in a string is an alphabetic letter. Here, we'll explore three effective techniques: using the isalpha() method, the string module, and regular expressions. Using the isalpha() method The isalpha() method is a built-in method in Python that returns True if all the characters in a string are alphabets (letters) and False otherwise. Example In this example, we have a string "Hello World" and we want to check if the character at index 1 is a letter. We use the isalpha() method to check if the character is ... Read More

What is a Python bytestring?

SaiKrishna Tavva
Updated on 25-Mar-2025 15:53:05

2K+ Views

A bytestring in Python is a sequence of bytes, represented using the bytes data type in Python 3. Bytestrings are primarily used to handle binary data or data that doesn't conform to the ASCII or Unicode encodings, such as images, audio files, and more. They are crucial for tasks that require low-level data manipulation. Creating a Bytestring To create a bytestring in Python, prefix a string literal with the letter b. This indicates to Python that the string should be interpreted as a sequence of bytes. Example In this example, we create a bytestring with the contents "This is a ... Read More

How to replace \\ with in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:23:04

830 Views

There are two ways to go about replacing \ with \ or unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello,world"' >>> print ast.literal_eval(a) Hello, worldAnother way is to use the decode('string_escape') method from the string class. For example,>>> print "Hello,world".decode('string_escape') Hello, world

How to correctly sort a string with a number inside in Python?

Yaswanth Varma
Updated on 20-May-2025 13:12:58

3K+ Views

Sorting the string that contains the number, such as ("xy1", "xy2", "xy10"),  can be complex in Python. For example, if we sort the list ["xy1", "xy2", "xy10"] using the built-in sort() method, it results in ["xy1", "xy10", "xy2"]. But we will expect "xy2" to come before "xy10". It is because the Python default sorting uses the lexicographical order and compares the characters from left to right based on their Unicode values. Since the character '1' in "xy10" comes before "2" in "xy2", the "xy10" is treated as smaller, even though the number 10 is greater than 2. This ... Read More

Advertisements