Jayashree

Jayashree

19 Articles Published

Articles by Jayashree

Page 2 of 2

How to Swap Two Variables using Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 463 Views

By using a temporary variable −>>> x=10 >>> y=20 >>> z=x >>> x=y >>> y=z >>> x,y (20, 10)Without using temporary variable>>> a,b=5,7 >>> a,b (5, 7) >>> a,b=b,a >>> a,b (7, 5)

Read More

How to print a value for a given key for Python dictionary?

Jayashree
Jayashree
Updated on 26-Feb-2020 8K+ Views

Python dictionary is collection of key value pairs. Value associated with a certain key is returned by get() method.>>> D1={'a':11,'b':22,'c':33} >>> D1.get('b') 22You can also obtain value by using key inside square brackets.>>> D1['c'] 33

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 to extract subset of key-value pairs from Python dictionary object?

Jayashree
Jayashree
Updated on 04-Dec-2019 3K+ Views

Use dictionary comprehension technique.We have dictionary object having name and percentage of students>>> marks = {    'Ravi': 45.23,    'Amar': 62.78,    'Ishan': 20.55,    'Hema': 67.20,    'Balu': 90.75 }To obtain dictionary of name and marks of students with percentage>50>>> passed = { key:value for key, value in marks.items() if value > 50 } >>> passed {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}To obtain subset of given names>>> names = { 'Amar', 'Hema', 'Balu' } >>> lst = { key:value for key,value in marks.items() if key in names} >>> lst {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}

Read More

How will you explain Python Operator Overloading?

Jayashree
Jayashree
Updated on 30-Jul-2019 317 Views

Every class in Python, whether built-in or user defined is inherited from object class. The object class has a number of properties whose name is preceded and followed by double underscores (__). Each of these properties is a wrapper around a method of same name. Such methods are called special or magic methods.The magic methods __lt__(), __gt__(), __eq__(), __ne__(), etc. are overridden in a class to overload == and != operators respectively.

Read More

How to create a Python dictionary from text file?

Jayashree
Jayashree
Updated on 30-Jul-2019 13K+ Views

Assuming a following text file (dict.txt) is present1 aaa2 bbb3 cccFollowing Python code reads the file using open() function. Each line as string is split at space character. First component is used as key and second as valued = {} with open("dict.txt") as f: for line in f:     (key, val) = line.split()     d[int(key)] = val print (d)The output shows contents of file in dictionary form{1: 'aaa', 2: 'bbb', 3: 'ccc'}

Read More

What is @ operator in Python?

Jayashree
Jayashree
Updated on 30-Jul-2019 873 Views

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.we have two different kinds of decorators in Python:Function decoratorsClass decorators A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to ...

Read More
Showing 11–19 of 19 articles
« Prev 1 2 Next »
Advertisements