Pythonista

Pythonista

18 Articles Published

Articles by Pythonista

18 articles

Python - How to convert this while loop to for loop?

Pythonista
Pythonista
Updated on 24-Mar-2026 1K+ Views

Converting a while loop to a for loop in Python can be achieved using itertools.count() which creates an infinite iterator. This is particularly useful when you need to iterate indefinitely until a specific condition is met. Understanding itertools.count() The count() function from the itertools module generates an iterator of evenly spaced values. It takes two optional parameters: start − Starting value (default is 0) step − Step size between values (default is 1) Using default parameters creates an infinite iterator, so you must use break to terminate the loop when needed. Example: Converting ...

Read More

How to overload Python comparison operators?

Pythonista
Pythonista
Updated on 24-Mar-2026 15K+ Views

Python provides magic methods to define custom behavior for comparison operators. The comparison operators (=, == and !=) can be overloaded by implementing __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods respectively. Magic Methods for Comparison Operators Operator Magic Method Description == __eq__() Equal to != __ne__() Not equal to = __ge__() Greater than or equal to Example: Overloading Comparison Operators The following example demonstrates overloading the == and >= operators for a custom Distance class − class Distance: ...

Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

Pythonista
Pythonista
Updated on 24-Mar-2026 4K+ Views

Python provides magic methods to define custom behavior for comparison operators. The operators =, == and != can be overloaded using the __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods respectively. Basic Implementation Let's create a Distance class that overloads the operators to compare distances in feet and inches ? class Distance: def __init__(self, feet=5, inches=5): self.feet = feet self.inches = inches def __str__(self): ...

Read More

Is there a "not equal" operator in Python?

Pythonista
Pythonista
Updated on 24-Mar-2026 475 Views

Python provides the not equal operator to compare values and check if they are different. The != operator is the standard way to perform "not equal" comparisons in modern Python. The != Operator The != operator returns True when two values are not equal, and False when they are equal ? # Comparing numbers result1 = 5 != 3 result2 = 5 != 5 print("5 != 3:", result1) print("5 != 5:", result2) 5 != 3: True 5 != 5: False Using != with Different Data Types The not equal ...

Read More

How to insert a Python tuple in a MySQL database?

Pythonista
Pythonista
Updated on 24-Mar-2026 2K+ Views

Inserting a Python tuple into a MySQL database is a common task when working with structured data. This tutorial shows how to insert tuple data into a MySQL table using the PyMySQL module. Prerequisites Before starting, ensure you have: A MySQL database named test A table named employee with fields: fname, lname, age, gender, salary PyMySQL module installed (pip install PyMySQL) Sample Tuple Data Let's define a tuple containing employee data ? # Sample employee data as tuple employee_data = ('Mac', 'Mohan', 20, 'M', 2000) print("Employee data:", employee_data) ...

Read More

How to convert a string to a integer in C

Pythonista
Pythonista
Updated on 15-Mar-2026 726 Views

In C programming, converting a string to an integer is a common task that can be accomplished using several built-in functions. The most commonly used functions are atoi(), strtol(), and sscanf(). Syntax int atoi(const char *str); long strtol(const char *str, char **endptr, int base); int sscanf(const char *str, const char *format, ...); Method 1: Using atoi() Function The atoi() function converts a string to an integer. It stops reading when it encounters a non-digit character − #include #include int main() { char str[] = "12345"; ...

Read More

Why we can't use arrow operator in gets and puts?

Pythonista
Pythonista
Updated on 22-Jun-2020 234 Views

You can't read user input in an un-initialized pointer. Instead, have a variable of the struct data type and assign its address to pointer before accessing its inner elements by → operatorexample#include struct example{ char name[20]; }; main(){ struct example *ptr; struct example e; puts("enter name"); gets(e.name); ptr=&e; puts(ptr->name); }OutputTypical result of above codeenter name Disha You entered Disha

Read More

How to emulate a do-while loop in Python?

Pythonista
Pythonista
Updated on 19-Jun-2020 498 Views

Python doesn't have an equivalent of do-while loop as in C/C++ or Java. The essence of do-while loop is that the looping condition is verified at the end of looping body. This feature can be emulated by following Python code −Examplecondition=True x=0 while condition==True:      x=x+1      print (x)      if x>=5: condition=FalseOutputThe output is as follows −1 2 3 4 5

Read More

What is vertical bar in Python bitwise assignment operator?

Pythonista
Pythonista
Updated on 02-Mar-2020 1K+ Views

Vertical bar (|) stands for bitwise or operator. In case of two integer objects, it returns bitwise OR operation of two>>> a=4 >>> bin(a) '0b100' >>> b=5 >>> bin(b) '0b101' >>> a|b 5 >>> c=a|b >>> bin(c) '0b101'

Read More

How to use else conditional statement with for loop in python?

Pythonista
Pythonista
Updated on 27-Feb-2020 292 Views

The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −Syntaxwhile expr==True:     #statements to be iterated while expr is true. else:    #this statement(s) will be executed afteriterations are over#this will be executed after the program leaves loop bodyexamplefor x in range(6): print (x) else: print ("else block of loop") print ("loop is over")OutputThe output is as shown below −0 1 2 3 4 5 else block of loop loop is over

Read More
Showing 1–10 of 18 articles
« Prev 1 2 Next »
Advertisements