"is" vs "=="
-------------------------
- "is" expressions evaluate to True if two variables point to the same object.
- "==" evaluates to True if the objects referred to by the variables are equal.
>>> a = [1, 2, 3]>>> b = a >>> a is bTrue>>> a == bTrue >>> c = list(a) >>> a == cTrue>>> a is cFalse
Showing posts with label args and kwargs in python. Show all posts
Showing posts with label args and kwargs in python. Show all posts
Tuesday, 9 January 2018
difference between is and == in python
Sunday, 23 July 2017
args and kwargs in python
Normal Arguments and arbitrary arguments and keyword arguments in python:
Example:
def f1(req,*args,**kwargs):
if req:
print(req,type(req))
if args:
print(args,type(args))
if kwargs:
print(kwargs,type(kwargs))
f1(10)
f1(10,2.3,5,"siva")
f1(10,2.3,5,"siva",key1=20,key2="siva",key3=True)
Output:
10 <class 'int'>
10 <class 'int'>
(2.3, 5, 'siva') <class 'tuple'>
10 <class 'int'>
(2.3, 5, 'siva') <class 'tuple'>
{'key1': 20, 'key2': 'siva', 'key3': True} <class 'dict'>
- Normal argument type depends on value/data
- Arbitrary argument by default type is tuple, Represents *
- keyword argument by default type is dictionary,Represents **
Example:
def f1(req,*args,**kwargs):
if req:
print(req,type(req))
if args:
print(args,type(args))
if kwargs:
print(kwargs,type(kwargs))
f1(10)
f1(10,2.3,5,"siva")
f1(10,2.3,5,"siva",key1=20,key2="siva",key3=True)
Output:
10 <class 'int'>
10 <class 'int'>
(2.3, 5, 'siva') <class 'tuple'>
10 <class 'int'>
(2.3, 5, 'siva') <class 'tuple'>
{'key1': 20, 'key2': 'siva', 'key3': True} <class 'dict'>
Subscribe to:
Posts (Atom)