Found 10784 Articles for Python

Which data types are immutable in Python?

Vikram Chiluka
Updated on 09-Sep-2023 09:39:52

12K+ Views

In this article, we will explain the immutable datatypes in Python. Python considers everything to be an object. A unique id is assigned to it when we instantiate an object. We cannot modify the type of object, but we may change its value. For example, if we set variable a to be a list, we can't change it to a tuple/dictionary, but we may modify the entries in that list. In Python, there are two kinds of objects. On the one hand, there are objects that can change their internal state (the data/content inside the objects), i.e. they can be ... Read More

How to map two lists into a dictionary in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

323 Views

Easiest way is to create a zip object that returns a generator of tuples, each having an item each from two lists. The zip object can then be transformed into a dictionary by using built-in dict() function >>> l1=['name', 'age', 'marks'] >>> l2=['Ravi', 23, 56] >>> z=zip(l1,l2) >>> newdict=dict(z) >>> newdict {'name': 'Ravi', 'age': 23, 'marks': 56}

How to access Python dictionary in simple way?

Pythonic
Updated on 30-Jul-2019 22:30:21

108 Views

There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value. >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1.get('age') 23 Another way is to use key inside square brackets in front of dictionary object >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1['age'] 23

How to create a Python dictionary from an object's fields?

Pythonic
Updated on 30-Jul-2019 22:30:21

254 Views

The __dict__ attribute returns a dictionary out of fields of any object. Let us define a class person >>> class person: def __init__(self): self.name='foo' self.age = 20 def show(self): print (self.name, self.age) We now declare an object of this class and obtain its __dict__ attribute which turns out to be dictionary object >>> p = person() >>> d = p.__dict__ >>> d {'name': 'foo', 'age': 20}

How to convert Python Dictionary to a list?

Vikram Chiluka
Updated on 23-Aug-2023 13:02:48

98K+ Views

In this article, we will show you how to convert a python dictionary to a list. Below are the methods to accomplish this task: Using list & items() Method Using keys() Method Using values() Method Using List Comprehension Using Zip() Function Using map() Function Using for loop & items() Method Dictionaries are Python's version of an associative array data structure. A dictionary is a collection of key-value pairs. Each key pair is represented by a key pair and its associated value. A dictionary is defined by a list of key-value pairs enclosed in curly braces and ... Read More

How to iterate over dictionaries using 'for' loops in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

268 Views

Even though dictionary itself is not an iterable object, the items(), keys() and values methods return iterable view objects which can be used to iterate through dictionary. The items() method returns a list of tuples, each tuple being key and value pair. >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for t in d1.items(): print (t) ('name', 'Ravi') ('age', 23) ('marks', 56) Key and value out of each pair can be separately stored in two variables and traversed like this − >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for k, v in d1.items(): print (k, ... Read More

How to replace backward "" slash from a string

Malhar Lathkar
Updated on 23-Jun-2020 14:44:01

519 Views

In Python it does give the desired result>>> var  = "aaa\bbb\ccc and ddd\eee" >>> var.split('\') ['aaa', 'bbb', 'ccc and ddd', 'eee']

How to generate prime numbers using Python?

Pythonista
Updated on 26-Feb-2020 12:46:08

5K+ Views

A prime number is the one that is not divisible by any other number except 1 and itself.In Python % modulo operator is available to test if a number is divisible by other. Assuming we have to find prime numbers between 1 to 100, each number (let us say x) in the range needs to be successively checked for divisibility by 2 to x-1. This is achieved by employing two nested loops.for x in range(1,101): for y in range(2,x): if x%y==0:break else: print (x,sep=' ', end=' ')Above code generates prime numbers between 1-1001 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

What keyboard command we have to stop an infinite loop in Python?

Pythonista
Updated on 26-Feb-2020 12:44:52

322 Views

Any loop is formed to execute a certain number of times or until a certain condition is satisfied. However, if the condition doesn't arise, loop keeps repeating infinitely. Such an infinite loop needs to be forcibly stopped by generating keyboard interrupt. Pressing ctrl-C stops execution of infinite loop>>> while True: print ('hello') hello hello hello hello hello hello Traceback (most recent call last): File "", line 2, in print ('hello') KeyboardInterrupt

How to use else conditional statement with for loop in python?

Pythonista
Updated on 27-Feb-2020 05:22:06

168 Views

The else block in a loop (for as well as while) executes after all iterations of loop are completed and before the program flow exits the loop body. The syntax is as follows −Syntaxwhile expr==True:     #statements to be iterated while expr is true. else:    #this statement(s) will be executed afteriterations are over#this will be executed after the program leaves loop bodyexamplefor x in range(6): print (x) else: print ("else block of loop") print ("loop is over")OutputThe output is as shown below −0 1 2 3 4 5 else block of loop loop is over

Advertisements