Python Articles

Page 850 of 855

What does 'is not' operator do in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 593 Views

In Python, is and is not operators are called identity operators. Each object in computer's memory is assigned a unique identification number (id) by Python interpreter. Identity operators check if id() of two objects is same. 'is not' operator returns true of id() values are different and false if they are same.>>> a=10 >>> b=a >>> id(a), id(b) (490067904, 490067904) >>> a is not b False >>> a=10 >>> b=20 >>> id(a), id(b) (490067904, 490068064) >>> a is not b True

Read More

What does 'in' operator do in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 711 Views

In Python, in and not in operators are called membership operators. Their purpose is to check if an object is a member of a certain sequence object like string, list, or tuple. The in operator returns true if object is present in sequence, false if not found>>> 'p' in 'Tutorialspoint' True >>> 'c' in 'Tutorialspoint' False >>> 10 in range(0,5) False

Read More

How do we convert a string to a set in Python?

Pythonista
Pythonista
Updated on 25-Feb-2020 822 Views

Python’s standard library contains built-in function set() which converts an iterable to set. A set object doesn’t contain repeated items. So, if a string contains any character more than once, that character appears only once in the set object. Again, the characters may not appear in the same sequence as in the string as set() function has its own hashing mechanism>>> set("hello") {'l', 'h', 'o', 'e'}

Read More

How to convert an object x to a string representation in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 24-Feb-2020 317 Views

Most commonly used str() function from Python library returns a string representation of object.>>> no=100 >>> str(no) '100' >>> L1=[1,2,3,4] >>> str(L1) '[1, 2, 3, 4]' >>> d={'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}"However, repr() returns a default and unambiguous representation of the object, where as str() gives an informal representation that may be readable but may not be always unambiguous.>>> str(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(d) "{'a': 1, 'b': 2, 'c': 3, 'd': 4}" >>> repr(L1) '[1, 2, 3, 4]' >>> repr(no) '100'

Read More

How to Find the Sum of Natural Numbers using Python?

Jayashree
Jayashree
Updated on 21-Feb-2020 1K+ Views

You can use while loop to successively increment value of a variable i by one and adding it cumulatively.s,i=0,0 n=10 while i

Read More

How to generate armstrong numbers in Python?

Jayashree
Jayashree
Updated on 21-Feb-2020 2K+ Views

Any three digit number is called an Armstrong number of sum of cube of its digits equals the number itself. In order to check if a number satisfies this condition, each digit from it is successively separated from right and its cube is cumulatively added. In the end if the sum is found to be equal to original number, it is called Armstrong number.ExampleFollowing Python code prints all armstrong numbers between 100 to 999for num in range(100, 1000):   temp=num   sum=0   while temp>0:     digit=temp%10     sum=sum+digit**3     temp=temp//10   if sum==num:     ...

Read More

How to get the number of capture groups in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

Read More

How to write Python regular expression to get all the anchor tags in a webpage?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 257 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

Read More

How to use wildcard in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

Read More

How to find all adverbs and their positions in a text using python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 512 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

Read More
Showing 8491–8500 of 8,546 articles
« Prev 1 848 849 850 851 852 855 Next »
Advertisements