Programming Articles

Page 635 of 2547

How to know Maximum and Minimum values for ints in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 192 Views

Python's core library provides built-in functions max() and min() to find maximum and minimum values from sequences like lists, tuples, or multiple arguments. Additionally, Python offers ways to discover the system limits for integer values. Using max() and min() Functions With Multiple Arguments You can pass multiple numbers directly to max() and min() − print(max(23, 21, 45, 43)) print(min(23, 21, 45, 43)) 45 21 With Lists and Tuples These functions also work with sequence objects like lists and tuples − numbers_list = [20, 50, 40, 30] numbers_tuple ...

Read More

How to create a Python dictionary from an object\'s fields?

Disha Verma
Disha Verma
Updated on 24-Mar-2026 542 Views

In Python, objects are instances of classes containing attributes and methods, while dictionaries are collections of key-value pairs. Converting an object's fields to a dictionary is useful for serialization, debugging, or data processing. Python provides two main approaches for this conversion. Using __dict__ Attribute Every Python object has a __dict__ attribute that stores the object's attributes and their values as a dictionary. This attribute directly exposes the internal namespace of the object ? Example class Company: def __init__(self, company_name, location): self.company_name = company_name ...

Read More

How to prevent loops going into infinite mode in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 3K+ Views

In Python, while loops can run infinitely if the loop condition never becomes False. To prevent infinite loops, you need to modify the condition variable inside the loop body or use control statements like break. Using a Counter Variable The most common approach is to use a counter that gets incremented in each iteration ? count = 0 while count < 5: print('Python!') count += 1 Python! Python! Python! Python! Python! Here, the count variable is incremented in each iteration, ensuring the condition ...

Read More

How to print a value for a given key for Python dictionary?

Jayashree
Jayashree
Updated on 24-Mar-2026 8K+ Views

Python dictionary is a collection of key-value pairs. There are several ways to print or access the value associated with a specific key. Using the get() Method The get() method returns the value for a given key. If the key doesn't exist, it returns None by default ? D1 = {'a': 11, 'b': 22, 'c': 33} print(D1.get('b')) print(D1.get('d')) # Key doesn't exist 22 None Using get() with Default Value You can provide a default value to return when the key is not found ? D1 = {'a': ...

Read More

How to convert float to integer in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 3K+ Views

In Python there are two main numeric data types: integers and floats. Integers are whole numbers without decimal points, while floats contain decimal values. Python provides several built-in methods to convert float values to integers ? Using the int() Function The int() function converts floating-point numbers to integers by truncating the decimal part. It does not round values − it simply removes everything after the decimal point. Basic Float to Integer Conversion Here's a simple example of converting a float to an integer ? num = 39.98 print('Data type of num:', type(num).__name__) ...

Read More

How to convert an integer to a character in Python?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 39K+ Views

To convert an integer to a character in Python, we can use the chr() method. The chr() is a Python built−in function that returns a Unicode character corresponding to the given integer value. Syntax chr(number) Parameters number − An integer between 0 and 1, 114, 111 (0x110000 in hexadecimal). Return Value Returns a Unicode character corresponding to the integer argument. Raises ValueError if the integer is out of range (not in range(0x110000)). Raises TypeError for non−integer arguments. Basic Example Let's convert integer 100 to its corresponding Unicode character ? ...

Read More

nHow to print out the first character of each list in Python?

Shriansh Kumar
Shriansh Kumar
Updated on 24-Mar-2026 6K+ Views

A Python list is a built-in, mutable datatype that stores multiple items or elements, separated by commas, within square brackets [ ]. The index of a list in Python starts from 0 up to length-1. We can retrieve/access elements at a particular index as follows ? list_name[index] The given task is to write a Python program that prints the first character of each element in a list. But, before that, let's see some example scenarios: Scenario 1 For example, if our list contains string values, the output should be the first character of ...

Read More

What does \'is\' operator do in Python?

Disha Verma
Disha Verma
Updated on 24-Mar-2026 3K+ Views

The "is" operator in Python is an identity operator that checks whether two variables refer to the same object in memory. It returns True if both variables point to the same object, and False otherwise. Each object in Python's memory has a unique identification number assigned by the interpreter. The is operator compares these memory addresses using the id() function internally, making it different from the == operator which compares values. Syntax variable1 is variable2 The is operator returns True if both variables reference the same object in memory, False otherwise. Example with ...

Read More

What is "not in" operator in Python?

Pythonic
Pythonic
Updated on 24-Mar-2026 5K+ Views

In Python, the not in membership operator evaluates to True if it does not find a variable in the specified sequence and False otherwise. Syntax element not in sequence Example with Lists Let's check if elements are not present in a list ? a = 10 b = 4 numbers = [1, 2, 3, 4, 5] print(a not in numbers) print(b not in numbers) True False Since a (10) doesn't belong to numbers, a not in numbers returns True. However, b (4) can be found in ...

Read More

What is Python equivalent of the ! operator?

Gireesha Devara
Gireesha Devara
Updated on 24-Mar-2026 3K+ Views

In some languages like C / C++, the "!" symbol is used as a logical NOT operator. !x returns true if x is false, else returns false. The equivalent of this "!" operator in Python is the not keyword, which also returns True if the operand is false and vice versa. Basic Usage with Boolean Values Example with True Value In the following example, the variable operand_X holds a boolean value True. After applying the not operator, it returns False − operand_X = True print("Input: ", operand_X) result = not operand_X print('Result: ', result) ...

Read More
Showing 6341–6350 of 25,466 articles
« Prev 1 633 634 635 636 637 2547 Next »
Advertisements