Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
What is the difference between del, remove and pop on lists in python ?
When working with Python lists, you have three main methods to remove elements: remove(), del, and pop(). Each method serves different purposes and behaves differently depending on your needs. Using remove() The remove() method removes the first matching value from the list, not by index but by the actual value ? numbers = [10, 20, 30, 40] numbers.remove(30) print(numbers) [10, 20, 40] If the value doesn't exist, remove() raises a ValueError ? numbers = [10, 20, 30, 40] try: numbers.remove(50) # Value not in ...
Read MoreHow are arguments passed by value or by reference in Python?
Python uses a mechanism known as Call-by-Object, sometimes also called Call by Object Reference or Call by Sharing. Understanding how arguments are passed is crucial for writing predictable Python code. If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different when we pass mutable arguments like lists or dictionaries. Immutable Objects (Call-by-value behavior) With immutable objects, changes inside the function don't affect the original variable ? def modify_number(x): x = 100 print("Inside function:", x) num = ...
Read MoreWhat is the difference between Cython and CPython?
Python has multiple implementations, with CPython being the default interpreter and Cython being a superset language that compiles to C. Understanding their differences helps choose the right tool for your project. What is CPython? CPython is the reference implementation of Python written in C. It's the standard Python interpreter that most developers use daily. CPython interprets Python code at runtime, converting it to bytecode and then executing it on the Python Virtual Machine. Example A simple Python program running on CPython − def fibonacci(n): if n
Read MoreDifference between mutable and immutable in python?
Python defines variety of data types of objects. These objects are stored in memory and object mutability depends upon the type. Mutable objects like Lists and Dictionaries can change their content without changing their identity. Immutable objects like Integers, Floats, Strings, and Tuples cannot be modified after creation. Mutable Objects - Lists Lists are ordered collections that can be modified after creation. You can change individual elements, add new items, or remove existing ones ? # Creating a list of numbers numbers = [1, 2, 3, 4, 5] print("Original list:", numbers) # Modifying an element ...
Read MoreWhat is calendar module in python?
The calendar module in Python provides functions to display calendars and perform calendar-related operations. By default, these calendars have Monday as the first day of the week and Sunday as the last. Display the Calendar of an Year To display the calendar of an year, use the calendar() method and set year as the parameter − import calendar # Set the year year = 2022 # Display the calendar print(calendar.calendar(year)) The output of the above code is − ...
Read MoreInterpreter base classes in Python
Python's interactive mode works on the principle of REPL (Read - Evaluate - Print - Loop). The code module in Python's standard library provides classes and convenience functions to set up REPL environment from within Python scripts. Core Classes in the Code Module The code module defines two main classes: InteractiveInterpreter: This class deals with parsing and interpreter state (the user's namespace). InteractiveConsole: Closely emulates the behavior of the interactive Python interpreter and is a subclass of InteractiveInterpreter. Convenience Functions interact(): Convenience function to run a read-eval-print loop. compile_command(): Useful for programs that ...
Read MorePackage extension utility in Python
The pkgutil module in Python provides utilities for extending and working with Python packages. It allows you to modify the module search path, load resources from packages, and iterate through available modules. extend_path() Function The extend_path() function extends the search path for modules within a package. It's commonly used in a package's __init__.py file ? import pkgutil # This would typically be in a package's __init__.py file __path__ = pkgutil.extend_path(__path__, __name__) print("Extended package path:", __path__) The function scans sys.path for directories containing subdirectories named after your package and combines them into a single ...
Read Morezipapp - Manage executable Python zip archives
The zipapp module was introduced in Python 3.5 to manage executable Python zip archives. This module allows you to package Python applications into a single executable .pyz file that can be run directly by the Python interpreter. Creating a Basic Executable Archive To create an executable archive, you need a directory containing Python code with a main function. Let's create a simple example ? First, create a directory called myapp and add an example.py file: def main(): print('Hello World') if __name__ == '__main__': main() ...
Read MoreAccess to the underlying platform's identifying data in Python
The platform module in Python provides functions to access information about the underlying system's hardware, operating system, and interpreter version. This is useful for system administration, debugging, and creating platform-specific code. Basic System Information architecture() This function queries the given executable (defaults to the Python interpreter executable) for various architecture information − import platform print(platform.architecture()) ('64bit', '') machine() This function returns the machine type, e.g. 'i386'. An empty string is returned if the value cannot be determined − import platform print(platform.machine()) x86_64 ...
Read MoreShort Circuiting Techniques in Python?
Short-circuiting is an optimization technique where Python stops evaluating boolean expressions as soon as the result is determined. This behavior can be confusing for beginners but is essential for writing efficient code. Understanding the Confusion New programmers often misunderstand how and and or operators work. Let's examine these expressions ? print('x' == ('x' or 'y')) print('y' == ('x' or 'y')) print('x' == ('x' and 'y')) print('y' == ('x' and 'y')) True False False True How OR Short-Circuiting Works With or, Python evaluates the first value. If it's truthy, Python returns ...
Read More