Hafeezul Kareem

Hafeezul Kareem

258 Articles Published

Articles by Hafeezul Kareem

Page 3 of 26

Take Matrix input from user in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 9K+ Views

In this tutorial, we will learn how to take matrix input from the user in Python. There are two common approaches: taking individual elements one by one, or taking entire rows with space-separated values. Method 1: Taking Individual Elements This method asks the user to input each matrix element individually ? Example # initializing an empty matrix matrix = [] # taking 2x2 matrix from the user for i in range(2): # empty row row = [] for j in range(2): ...

Read More

Sum of list (with string types) in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 3K+ Views

In this tutorial, we'll write a program that adds all numbers from a list containing mixed data types. The list may contain numbers in string or integer format, along with non-numeric strings. Problem Statement Given a list with mixed data types, we need to sum only the numeric values (both integers and numeric strings) while ignoring non-numeric strings ? Input random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020] Expected Output 4051 Solution Approach Follow these steps to solve the problem: Initialize the list with mixed data ...

Read More

Structuring Python Programs

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 550 Views

In this tutorial, we are going to see some best practices for structuring Python programs. Following these conventions makes your code more readable and maintainable. Use Consistent Indentation Python uses indentation to define code blocks. Use 4 spaces per indentation level as recommended by PEP 8. Avoid mixing tabs and spaces ? def calculate_area(length, width): # 4 spaces for indentation if length > 0 and width > 0: area = length * width ...

Read More

struct module in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

The struct module in Python converts native Python data types into strings of bytes and vice versa. It's a built-in module that uses C-style format characters to specify data types and their binary representation. Format Characters The struct module uses format characters similar to C language data types ? Data Type Format Character Description int i 4-byte signed integer char c 1-byte character string s char[] (requires length prefix) float f 4-byte floating point struct.pack() - Converting to Bytes The struct.pack() method converts ...

Read More

string.whitespace in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 495 Views

In this tutorial, we are going to learn about string.whitespace in Python. The string.whitespace constant is pre-defined in the string module and contains all ASCII whitespace characters: space, tab, linefeed, return, formfeed, and vertical tab. What is string.whitespace? The string.whitespace is a string constant that contains all characters considered as whitespace in ASCII. This includes: Space character (' ') Tab character ('\t') Newline character ('') Carriage return ('\r') Form feed ('\f') Vertical tab ('\v') Basic Example Let's see what string.whitespace contains ? import string print("Content of string.whitespace:") print(repr(string.whitespace)) print("Length:", len(string.whitespace)) ...

Read More

string.punctuation in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

In this tutorial, we are going to learn about the string.punctuation constant in Python. The string.punctuation is a pre-defined string constant in Python's string module that contains all ASCII punctuation characters. We can use it for text processing, password generation, and data validation. What is string.punctuation? The string.punctuation constant contains all printable ASCII punctuation characters as a single string ? import string # Display the punctuation string print("Punctuation characters:") print(string.punctuation) print(f"Total characters: {len(string.punctuation)}") Punctuation characters: !"#$%&'()*+, -./:;?@[\]^_`{|}~ Total characters: 32 Practical Examples Removing Punctuation from Text Remove all ...

Read More

string.octdigits in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 239 Views

In this tutorial, we are going to learn about the string.octdigits constant in Python. The string.octdigits is a pre-defined string constant in the string module of Python. It contains all the octal digits (0-7) as a single string. We can use this constant whenever we need to work with octal digits in our program by simply importing it from the string module. Basic Usage Let's see what string.octdigits contains ? import string # Print the octal digits string print(string.octdigits) print("Length:", len(string.octdigits)) 01234567 Length: 8 Checking the Data Type ...

Read More

SQL using Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 1K+ Views

In this tutorial, we are going to learn how to use SQL with Python using SQLite database. Python has a built-in module to connect with SQLite database. We are going to use sqlite3 module to connect Python and SQLite. We have to follow the below steps to connect the SQLite database with Python ? Import the sqlite3 module. Create a connection using the sqlite3.connect(db_name) method that takes a database name as an argument. It creates one file if it doesn't exist with the given name else it opens the file with ...

Read More

Split a string in equal parts (grouper in Python)

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 430 Views

In this tutorial, we will learn how to split a string into equal-sized parts using Python. This technique is often called a "grouper" function and is useful for processing data in chunks. Problem Overview Given a string and a desired chunk size, we want to divide the string into equal parts. If the string length is not evenly divisible, we'll pad the last chunk with a fill character. Example 1 Split 'Tutorialspoint' into chunks of 5 characters ? string = 'Tutorialspoint' each_part_length = 5 print(f"Input: '{string}' with chunk size {each_part_length}") Input: ...

Read More

Set update() in Python to do union of n arrays

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 406 Views

In this tutorial, we will use the set.update() method to find the union of multiple arrays. This method efficiently combines arrays while automatically removing duplicates, returning a one-dimensional array with all unique values. What is set.update()? The update() method adds elements from an iterable (like a list or array) to an existing set. It automatically handles duplicates by keeping only unique values. Syntax set.update(iterable) Example Let's see how to union multiple arrays using set.update() ? # initializing the arrays arrays = [[1, 2, 3, 4, 5], [6, 7, 8, 1, ...

Read More
Showing 21–30 of 258 articles
« Prev 1 2 3 4 5 26 Next »
Advertisements