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
Permutation and Combination in Python?
In this section, we are going to learn how to find permutation and combination of a given sequence using Python programming language. Python's itertools module provides built-in functions to generate permutations and combinations efficiently. A permutation is an arrangement of items where order matters, while a combination is a selection where order doesn't matter. Understanding Permutations vs Combinations Before diving into code, let's understand the difference: Permutation: Order matters. For items [A, B], permutations are (A, B) and (B, A) Combination: Order doesn't matter. For items [A, B], there's only one combination: (A, B) ...
Read MorePython program to Rearrange a string so that all same characters become d distance away
Given a string and an integer d, we need to rearrange the string such that all same characters are at least distance d apart from each other. If it's not possible to rearrange the string, we return an empty string. Problem Understanding The key challenge is to place characters with higher frequencies first, ensuring they have enough positions to maintain the required distance d. Example 1 str_input = "tutorialspoint" d = 3 # Result: "tiotiotalnprsu" # Same characters are at least 3 positions apart Example 2 str_input = "aaabc" d = ...
Read MorePython program to Find the first non-repeating character from a stream of characters?
In this article, we will find the first non-repeating character from a stream of characters. A non-repeating character is one that appears exactly once in the string. Let's say the following is our input ? thisisit The following should be our output displaying first non-repeating character ? h Using While Loop We will find the first non-repeating character by comparing each character with others using a while loop. This approach removes all occurrences of each character and checks if only one was removed ? # String my_str = "thisisit" ...
Read MorePython program to Count words in a given string?
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 MoreIncrement and Decrement Operators in Python?
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 MorePrint Single and Multiple variable in Python?
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 MoreSwap two variables in one line in using Python?
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 MoreWhen to use yield instead of return in Python?
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 MoreReturning Multiple Values in Python?
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 MoreWhat is the maximum possible value of an integer in Python?
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