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 by Gireesha Devara
Page 6 of 18
How to convert float to integer in Python?
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 MoreHow to convert an integer to a character in Python?
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 MoreWhat is Python equivalent of the ! operator?
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 MoreHow can I convert Python strings into tuple?
Converting Python strings into tuples is a common operation with multiple approaches depending on your needs. You can create a tuple containing the whole string, split the string into individual characters, or parse delimited strings into separate elements. Using Comma to Create Single-Element Tuple The simplest way is to add a comma after the string variable to treat the entire string as a single tuple element ? s = "python" print("Input string:", s) t = s, print("Output tuple:", t) print("Type:", type(t)) Input string: python Output tuple: ('python', ) Type: ...
Read MoreHow to create a dictionary with list comprehension in Python?
Python provides several ways to create dictionaries using list comprehension. The dict() method combined with list comprehension offers an elegant approach to generate key-value pairs dynamically. Syntax The basic syntax for creating a dictionary with list comprehension ? # Using dict() with list comprehension dict([(key, value) for item in iterable]) # Direct dictionary comprehension (alternative) {key: value for item in iterable} Using Unicode Characters as Keys Create a dictionary where keys are Unicode characters and values are their corresponding integers ? dict_obj = dict([(chr(i), i) for i in range(100, 105)]) ...
Read MoreWhat does ** (double star) and * (star) do for parameters in Python?
While creating a function, the single asterisk (*) is used to accept any number of positional arguments, and the double asterisk (**) is used to accept any number of keyword arguments. These operators provide flexibility when you don't know in advance how many arguments will be passed to your function. Using * (Single Asterisk) for Positional Arguments The single asterisk (*) collects extra positional arguments into a tuple. By convention, this parameter is named *args. Example Create a function that accepts an arbitrary number of positional arguments ? def sum_numbers(*args): ...
Read MoreHow can I convert a Python tuple to string?
A tuple is a collection of objects that is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are that tuples cannot be changed, unlike lists, and tuples use parentheses, whereas lists use square brackets. Converting a Python tuple to a String There are three different ways we can convert a Python tuple to a string: Using a for loop ...
Read MoreHow can I iterate through two lists in parallel in Python?
In Python, iterating through two or more lists in parallel is a common task. Python provides several methods to achieve this, each with different behaviors for handling lists of unequal lengths. Using range() with Index-Based Access The most basic approach uses range() with the len() function to iterate through both lists using indices ? Example When both lists have the same length ? letters = ['a', 'b', 'c', 'd', 'e'] numbers = [97, 98, 99, 100, 101] length = len(letters) # Assuming both lists have same length for i in range(length): ...
Read MoreHow do I check what version of Python is running my script?
Python is being updated regularly with new features and support. Starting from 1994 to the current release, there have been lots of updates in Python versions. Using Python standard libraries like sys or platform modules, we can get the version information of Python that is actually running on our script. In general, the Python version is displayed automatically on the console immediately after starting the interpreter from the command line ? Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. ...
Read MoreIs there any Python object inspector?
Python doesn't have a built-in object inspector, but it provides several powerful functions and modules for examining objects. Functions like type(), help(), dir(), vars(), and the inspect module help you discover attributes, properties, and methods of any object. Additional functions like id(), getattr(), hasattr(), globals(), locals(), and callable() are useful for examining object internals. Let's explore these inspection techniques using a simple class example. Sample Class for Demonstration First, let's create a Height class that we'll use throughout our examples ? class Height: """ A height class ...
Read More