- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
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
![Pythonic](https://www.tutorialspoint.com/assets/profiles/15007/profile/60_156531-1514381552.jpg)
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}
![Pythonic](https://www.tutorialspoint.com/assets/profiles/15007/profile/60_156531-1514381552.jpg)
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
![Pythonic](https://www.tutorialspoint.com/assets/profiles/15007/profile/60_156531-1514381552.jpg)
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}
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
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
![Pythonic](https://www.tutorialspoint.com/assets/profiles/15007/profile/60_156531-1514381552.jpg)
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
![Pythonista](https://www.tutorialspoint.com/assets/profiles/15003/profile/60_128639-1514576467.png)
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
![Pythonista](https://www.tutorialspoint.com/assets/profiles/15003/profile/60_128639-1514576467.png)
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
![Pythonista](https://www.tutorialspoint.com/assets/profiles/15003/profile/60_128639-1514576467.png)
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