Found 10784 Articles for Python

Non-decreasing Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 09:12:39

1K+ Views

Suppose we have an array with n integers, our task is to check whether it could become nondecreasing by modifying at most one element. We can define an array is non-decreasing if it satisfies this rule: array[i] 0if arr[i - 1] > arr[i + 1], then arr[i + 1] := arr[i]return trueExample (Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):    def checkPossibility(self, nums):       if len(nums) nums[i+1]:             if ans:                return False           ... Read More

Shortest Unsorted Continuous Subarray in Python

Arnab Chakraborty
Updated on 27-Apr-2020 08:55:20

207 Views

Suppose we have an integer array, we need to find one continuous subarray such that, if we only sort that subarray in ascending order, then the whole array will be sorted too. We need to find the shortest such subarray and output its length. So if the array is [2, 6, 4, 8, 10, 9, 15], then the output will be 5. The array will be [6, 4, 8, 10, 9]To solve this, we will follow these steps −res := sort the nums as an arrayans := 0set r as a linked listfor i in range 0 to the length ... Read More

Palindrome Linked List in Python

Arnab Chakraborty
Updated on 27-Apr-2020 08:50:56

1K+ Views

Suppose we have a linked list. We have to check whether the list elements are forming a palindrome or not. So if the list element is like [1, 2, 3, 2, 1], then this is a palindrome.To solve this, we will follow these steps −fast := head, slow := head, rev := None and flag := 1if the head is empty, then return truewhile fast and next of fast is availableif next of the next of fast is available, then set flag := 0 and break the loopfast := next of the next of fasttemp := slow, slow := next ... Read More

Loop Control Statements in Python

Mohd Mohtashim
Updated on 24-Jan-2020 12:04:27

559 Views

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.Python supports the following control statements. Click the following links to check their detail.Let us go through the loop control statements brieflySr.NoOperator & Description1break statementTerminates the loop statement and transfers execution to the statement immediately following the loop.2continue statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.3pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command ... Read More

Python Membership Operators

Mohd Mohtashim
Updated on 24-Jan-2020 12:00:24

118 Views

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −Sr.NoOperator & DescriptionExample1inEvaluates to true if it finds a variable in the specified sequence and false otherwise.x in y, here in results in a 1 if x is a member of sequence y.2not inEvaluates to true if it does not finds a variable in the specified sequence and false otherwise.x not in y, here not in results in a 1 if x is not a member of sequence y.Example Live Demo#!/usr/bin/python a = 10 b = 20 list ... Read More

Data Type Conversion in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:41:39

9K+ Views

Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type name as a function.There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.Sr.No.Function & Description1int(x [, base])Converts x to an integer. base specifies the base if x is a string.2long(x [, base] )Converts x to a long integer. base specifies the base if x is a string.3float(x)Converts x to a floating-point number.4complex(real [, imag])Creates a complex number.5str(x)Converts object x to a string representation.6repr(x)Converts object x to ... Read More

Dictionary Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:32:11

13K+ Views

Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.ExampleDictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). For example − Live Demo#!/usr/bin/python dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john', 'code':6734, 'dept': 'sales'} print dict['one'] # Prints value for 'one' key print ... Read More

Tuple Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:31:28

798 Views

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.ExampleThe main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists. For example − Live Demo#!/usr/bin/python tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Prints ... Read More

List Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:30:34

4K+ Views

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.ExampleThe values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is ... Read More

String Data Type in Python

Mohd Mohtashim
Updated on 24-Jan-2020 11:29:57

3K+ Views

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.ExampleThe plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example − Live Demo#!/usr/bin/python str = 'Hello World!' print str # Prints complete string print str[0] # Prints first character of the string ... Read More

Advertisements