Found 34494 Articles for Programming

Python Context Manager Types

Chandu yadav
Updated on 25-Jun-2020 13:51:46

245 Views

In python, the runtime context is supported by the with statement. The context is defined by the context manager. Using the context manager, we can create user defined classes to define the runtime context. It enters into the task before executing the statement body, and when the statement body is completed, it ends.There are two different methods for context manager. These methods are −Method __enter__()The __enter__() method is used to enter into the runtime context. It will return either the current object or another related object. The returned value is bound to the identifier in as clause of the with ... Read More

Python Mapping Types

George John
Updated on 30-Jul-2019 22:30:23

12K+ Views

The mapping objects are used to map hash table values to arbitrary objects. In python there is mapping type called dictionary. It is mutable. The keys of the dictionary are arbitrary. As the value, we can use different kind of elements like lists, integers or any other mutable type objects. Some dictionary related methods and operations are − Method len(d) The len() method returns the number of elements in the dictionary. Operation d[k] It will return the item of d with the key ‘k’. It may raise KeyError if the key is not mapped. Method iter(d) This method will ... Read More

Python Set Types

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

3K+ Views

The sets are basically an unordered collection of distinct hash-table objects. We can use the set for some mathematical operations like set union, intersection, difference etc. We can also use set to remove duplicates from a collection. The set do not record the element position. It does not support the indexing, slicing or other sequence related operations. In python there are basically two types of sets. The set and the frozenset. The set type is mutable, whether the frozenset is immutable. We can perform add(), remove() and these kind of operations on set, but it is not possible for frozenset. ... Read More

Python Binary Sequence Types

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

3K+ Views

The byte and bytearrays are used to manipulate binary data in python. These bytes and bytearrys are supported by buffer protocol, named memoryview. The memoryview can access the memory of other binary object without copying the actual data. The byte literals can be formed by these options. b‘This is bytea with single quote’ b“Another set of bytes with double quotes” b‘’’Bytes using three single quotes’’’ or b“””Bytes using three double quotes””” Some of the methods related to byte and bytearrays are − Method fromhex(string) The fromhex() method returns byte object. It takes a string where each byte is ... Read More

Python Text Sequence Types

Chandu yadav
Updated on 30-Jul-2019 22:30:23

574 Views

In python the str object, handles the text or string type data. Strings are immutable. The strings are sequence of Unicode characters. We can use single quote, double quotes or triple quotes to define the string literals. ‘This is a string with single quote’ “Another Text with double quotes” ‘’’Text using three single quotes’’’ or “””Text using three double quotes””” We can use triple quotes to assign multiline strings in python. There is different string related functions. Some of the String methods are as follows − Sr.No. Operation/Functions & Description 1 s.capitalize() Convert first ... Read More

Python Sequence Types

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

6K+ Views

Some basic sequence type classes in python are, list, tuple, range. There are some additional sequence type objects, these are binary data and text string. Some common operations for the sequence type object can work on both mutable and immutable sequences. Some of the operations are as follows − Sr.No. Operation/Functions & Description 1 x in seq True, when x is found in the sequence seq, otherwise False 2 x not in seq False, when x is found in the sequence seq, otherwise True 3 x + y Concatenate two sequences x ... Read More

Python Numeric Types

AmitDiwan
Updated on 11-Aug-2022 11:00:28

3K+ Views

The Numeric Types in Python are the integer datatypes. It includes integers, floatimg point, complex, etc. The complex includes real and imag parts. Also, includes Hexadecimal and Octal types. Python int datatype The Numeric Types include the int datatypes − a = 5 print("Integer = ", a) print("Type = ", type(a)) Output Integer = 5 Type = Python float datatype The Numeric Types include the float datatypes − Example a = 7E2 print("Float = ", a) print("Type = ", type(a)) Output Float = 700.0 Type = Python complex datatype The Numeric Types include the ... Read More

Python Boolean Operations

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

268 Views

The basic Boolean operations are and, or, not operations. The and operation − The basic syntax of and operation is: x and y. It indicates that when the x is false, then return x, otherwise returns y. The or operation −The basic syntax of or operation is: x or y. It indicates that when the x is false, then return y, otherwise returns x. The not operation − The basic syntax of and operation is: not x. It indicates that when the x is false, then it returns true, otherwise it returns false. Example Code Live ... Read More

Python Truth Value Testing

AmitDiwan
Updated on 12-Aug-2022 12:54:26

901 Views

What is Truth Value We can use any object to test the truth value. By providing the condition in the if or while statement, the checking can be done. Until a class method __bool__() returns False or __len__() method returns 0, we can consider the truth value of that object is True. The value of a constant is False, when it is False, or None. When a variable contains different values like 0, 0.0, Fraction(0, 1), Decimal(0), 0j, then it signifies the False Value. The empty sequence ‘‘, [], (), {}, set(0), range(0), Truth value of these elements are ... Read More

Python program for removing n-th character from a string?

AmitDiwan
Updated on 11-Aug-2022 10:03:48

866 Views

In this article, we will remove the nth character from a string in Python. Let’s say we have the following input string − Amitdiwan The output should be the following after removing nth character i.e. 2nd index − Amt Python program for removing n-th character from a string In this example, we will remove the nth character from a string − Example def removechar(str1, n): x = str1[ : n] y = str1[n + 1: ] return x + y # Driver Code if __name__ == '__main__': str1 = input("Enter a String =") n = int(input("Enter the ... Read More

Advertisements