Python Articles

Page 820 of 855

What does 'is not' operator do in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 26-Feb-2020 582 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 691 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 802 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 309 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 know Maximum and Minimum values for ints in Python?

Jayashree
Jayashree
Updated on 21-Feb-2020 180 Views

Python's core library has two built-in functions max() and min() respectively to find maximum and minimum number from a sequence of numbers in the form of list or tuple object.example>>> max(23,21,45,43) 45 >>> l1=[20,50,40,30] >>> max(l1) 50 >>> t1=(30,50,20,40) >>> max(t1) 50 >>> min(l1) 20 >>> min(t1) 20 >>> min(23,21,45,43) 21

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 I can check if A is superclass of B in Python?

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

We have the classes A and B defined as follows −class A(object): pass class B(A): passExampleA can be proved to be a super class of B in two ways as followsclass A(object):pass class B(A):pass print issubclass(B, A) # Here we use the issubclass() method to check if B is subclass of A print B.__bases__ # Here we check the base classes or super classes of BOutputThis gives the outputTrue (,)

Read More

How do we access class attributes using dot operator in Python?

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

A class attribute is an attribute of the class rather than an attribute of an instance of the class.In the code below class_var is a class attribute, and i_var is an instance attribute: All instances of the class have access to class_var, which can also be accessed as a property of the class itself −Exampleclass MyClass (object):     class_var = 2     def __init__(self, i_var):         self.i_var = i_var foo = MyClass(3) baz = MyClass(4) print (foo.class_var, foo.i_var) print (baz.class_var, baz.i_var)OutputThis gives the output(2, 3) (2, 4)

Read More

How to pop-up the first element from a Python tuple?

Pythonic
Pythonic
Updated on 20-Feb-2020 4K+ Views

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

Read More
Showing 8191–8200 of 8,547 articles
« Prev 1 818 819 820 821 822 855 Next »
Advertisements