Featured Post

Python Regex: The 5 Exclusive Examples

Image
 Regular expressions (regex) are powerful tools for pattern matching and text manipulation in Python. Here are five Python regex examples with explanations: 01 Matching a Simple Pattern import re text = "Hello, World!" pattern = r"Hello" result = re.search(pattern, text) if result:     print("Pattern found:", result.group()) Output: Output: Pattern found: Hello This example searches for the pattern "Hello" in the text and prints it when found. 02 Matching Multiple Patterns import re text = "The quick brown fox jumps over the lazy dog." patterns = [r"fox", r"dog"] for pattern in patterns:     if re.search(pattern, text):         print(f"Pattern '{pattern}' found.") Output: Pattern 'fox' found. Pattern 'dog' found. It searches for both "fox" and "dog" patterns in the text and prints when they are found. 03 Matching Any Digit   import re text = "The price of the

Python 'getsizeof' Command the Real Purpose

Here's a sample python program to get the size of a List array.  Getting the number of elements present in a List is not easy. So how to get this? To do you need the 'getsizeof' command.

Import 'sys' Library.

You need to import the 'sys' library to use the SIZE commands. 

>>> import sys

Congrats to you since you have now imported the SYS library. The command name is "getsizeof".

Check the below example.

print("An Integer:")
i = int(100)
print(sys.getsizeof(i))

The result will be: 100

What is the size command?  - is to get the size of elements. It gives the result based on the object you are being used. Suppose, if you use Strings, you will get String length.

Python Command to get the Size of an Object


How to use the 'getsizeof' command?

Example:1

print("Empty string:")
empty_string = ""
print(sys.getsizeof(empty_string))

Example:2

print("A normal string")
s = "Hello world"
print(sys.getsizeof(s))

Example:3

print("Increasing string:")
for i in range(0,5):
empty_string = empty_string + chr(i)
print(sys.getsizeof(empty_string))

Sources

Comments

Popular posts from this blog

Explained Ideal Structure of Python Class

6 Python file Methods Real Usage

How to Decode TLV Quickly