Category Archives: Python

When to use yield instead of return in Python?

The yield statement suspends function’s execution and sends a value back to caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather them computing them… Read More »

Interesting facts about strings in Python | Set 1

1. Strings are Immutable Once a string is defined, it cannot be changed. Output: Traceback (most recent call last): line 3, in a[2] = ‘E’ TypeError: ‘str’ object does not support item assignment But below code works fine. Output: Geeks Geeksfor In the second program, interpreter makes a copy of the original string and then… Read More »

Division Operators in Python

Consider below statements in Python 2.7 Output: 2 -3 First output is fine, but the second one may be surprising if we are coming Java/C++ world. In Python 2.7, the “/” operator works as a floor division for integer arguments. However, the operator / returns a float value if one of the arguments is a… Read More »

trunc() in Python

Truncate in Python There are many built-in modules in python. Out of these module there is one interesting module known as math module which have several functions in it like, ceil, floor, truncate, factorial, fabs, etc. Out of these functions there is an interesting function called truncate which behaves as a ceiling function for negative… Read More »

Class or Static Variables in Python

Class or static variables are shared by all objects. Instance or non-static variables are different for different objects (every object has a copy of it).

Generate all permutation of a set in Python

Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by  n!.  For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3,… Read More »

Print Single and Multiple variable in Python

Consider below two Python code snippets in Python 2.x. There is no difference between code 1 and code 2 in case of single variable in Python 2.X, but in case of multiple variables, variable with brackets -() is treated as “tuple”. For multiple variable: “print variable” prints the variables without any brackets ‘()’ and splitted… Read More »

Project Idea | (A Game of Anagrams )

Project idea: The aim of this project is to create a game in python in which the user is presented with an anagram of a word and has to guess the right word within a limited number of attempts. Features of Project: The user is given a fixed number of attempts to guess the correct… Read More »

Increment and Decrement Operators in Python

If you’re familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and -- operators is mixing up the differences (both in precedence and in… Read More »