Articles on Trending Technologies

Technical articles with clear explanations and examples

Python program to Count words in a given string?

Chandu yadav
Chandu yadav
Updated on 25-Mar-2026 4K+ Views

Sometimes we need to count the number of words in a given string. Python provides several approaches to accomplish this task, from manual counting using loops to built-in methods like split(). Using For Loop This method counts words by detecting whitespace characters manually − test_string = "Python is a high level language. Python is interpreted language." total = 1 for i in range(len(test_string)): if test_string[i] == ' ' or test_string[i] == '' or test_string[i] == '\t': total = total + 1 print("Total ...

Read More

Increment and Decrement Operators in Python?

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 66K+ Views

Python does not have unary increment/decrement operators (++/--) like C or Java. Instead, Python uses compound assignment operators for incrementing and decrementing values. Basic Increment and Decrement Operations To increment a value, use the += operator ? a = 5 a += 1 # Increment by 1 print(a) 6 To decrement a value, use the -= operator ? a = 5 a -= 1 # Decrement by 1 print(a) 4 Why Python Doesn't Have ++ and -- Operators Python follows the ...

Read More

Print Single and Multiple variable in Python?

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 28K+ Views

To display single and multiple variables in Python, use the print() function. For multiple variables, separate them with commas or use formatting methods. Variables are reserved memory locations to store values. When you create a variable, you reserve space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Print Single Variable in Python To display a single variable, pass it directly to the print() function ? Example # Printing single values print(5) print(10) # Printing variables name = ...

Read More

Swap two variables in one line in using Python?

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 3K+ Views

Python provides several ways to swap two variables in a single line. The most Pythonic approach uses tuple unpacking, but you can also use arithmetic or bitwise operators ? Using Tuple Unpacking (Recommended) Python's tuple unpacking allows you to swap variables in one elegant line using the comma operator ? a = 5 b = 10 print("Before swapping:") print("a =", a) print("b =", b) # Swap two variables in one line using tuple unpacking a, b = b, a print("After swapping:") print("a =", a) print("b =", b) Before swapping: a ...

Read More

When to use yield instead of return in Python?

Chandu yadav
Chandu yadav
Updated on 25-Mar-2026 1K+ Views

In Python, yield and return serve different purposes. The return statement terminates function execution and returns a value, while yield pauses execution and creates a generator that can resume from where it left off. Understanding return Statement The return statement stops function execution immediately and optionally returns a value to the caller ? def get_numbers(): print("Starting function") return [1, 2, 3, 4, 5] print("This line never executes") numbers = get_numbers() print(numbers) Starting function [1, 2, 3, 4, 5] ...

Read More

Returning Multiple Values in Python?

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 7K+ Views

Python functions can return multiple values, which is a powerful feature not available in many programming languages like C++ or Java. When a function returns multiple values, they can be stored in variables directly or accessed using different data structures. There are several approaches to return multiple values from a function: tuples, lists, dictionaries, classes, and generators. Each method has its own advantages depending on your specific use case. Using Tuples The simplest way to return multiple values is using a tuple. Python automatically packs multiple return values into a tuple ? def calculate_values(x): ...

Read More

What is the maximum possible value of an integer in Python?

Ankith Reddy
Ankith Reddy
Updated on 25-Mar-2026 3K+ Views

Python has different integer limits depending on the version. In Python 2, integers had a maximum value before switching to long type, but Python 3 has unlimited precision integers bounded only by available memory. Python 2 Integer Behavior In Python 2, integers automatically switched to long type when they exceeded their limit ? import sys print(type(sys.maxint)) print(type(sys.maxint + 1)) Python 2 Maximum and Minimum Values import sys print(sys.maxint) # Max Integer value print(-sys.maxint - 1) # Min Integer value 2147483647 -2147483648 ...

Read More

Whatsapp using Python?

Chandu yadav
Chandu yadav
Updated on 25-Mar-2026 814 Views

WhatsApp automation using Python allows developers to send messages programmatically. Since WhatsApp doesn't provide official API access for personal accounts, we use Selenium to automate WhatsApp Web through a browser interface. Requirements To create a WhatsApp automation script, you need three essential components ? Install Selenium Install Selenium using pip ? pip install selenium Chrome WebDriver Download the Chrome WebDriver compatible with your Chrome browser version from the official ChromeDriver website. Extract it to a known location on your system. WhatsApp Account Ensure you have an active WhatsApp account ...

Read More

Changing Class Members in Python?

Chandu yadav
Chandu yadav
Updated on 25-Mar-2026 660 Views

Python object-oriented programming allows variables to be used at the class level or the instance level. Class variables are shared among all instances of a class, while instance variables are unique to each object. Understanding the difference between class and instance variables is crucial when modifying class members. Let's explore how to properly change class variables through examples. Class vs Instance Variables Here's a basic example demonstrating class and instance variables ? # Class Shark class Shark: animal_type = 'fish' # Class Variable ...

Read More

Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 617 Views

In this example, we will count negative numbers in a column-wise and row-wise sorted matrix. A sorted matrix has elements arranged in ascending order both horizontally (across rows) and vertically (down columns). Understanding the Problem For a sorted matrix, once we encounter a positive number in a row, all subsequent elements in that row will also be positive. This property allows us to optimize our counting approach. Creating the Matrix Let's start by creating a sample sorted matrix ? matrix = [ [-7, -3, 2, 3], ...

Read More
Showing 1–10 of 61,304 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements