Found 10805 Articles for Python

Add a key value pair to dictionary in Python

Pradeep Elance
Updated on 23-Aug-2023 13:10:11

64K+ Views

Python dictionaries are an unordered collection of key value pairs. In this tutorial we will see how we can add new key value pairs to an already defined dictionary. Below are the two approaches which we can use.Assigning a new key as subscriptWe add a new element to the dictionary by using a new key as a subscript and assigning it a value.ExampleCountryCodeDict = {"India": 91, "UK" : 44 , "USA" : 1} print(CountryCodeDict) CountryCodeDict["Spain"]= 34 print "After adding" print(CountryCodeDict)OutputRunning the above code gives us the following result −{'India': 91, 'USA': 1, 'UK': 44} After adding {'Spain': 34, 'India': 91, ... Read More

Accessing Attributes and Methods in Python

Pradeep Elance
Updated on 30-Jun-2020 08:49:39

12K+ Views

As an object oriented programming language python stresses on objects. Classes are the blueprint from which the objects are created. Each class in python can have many attributes including a function as an attribute.Accessing the attributes of a classTo check the attributes of a class and also to manipulate those attributes, we use many python in-built methods as shown below.getattr() − A python method used to access the attribute of a class.hasattr()  − A python method used to verify the presence of an attribute in a class.setattr() − A python method used to set an additional attribute in a class.The below ... Read More

Absolute and Relative Imports in Python

Pradeep Elance
Updated on 30-Jul-2019 22:30:26

465 Views

Many times when we create python code we find that we need to access code from another python file or package. This is when you need to import that other python file or package into your current code. So the straight forward way to achieve this is just written the below statement at the top of your current python program.import package_name or module_name or from pacakge_name import module_name/object_nameWhen the above statement is parsed the interpreter does the following.The interpreter will look for names in the cache of all modules that have already been imported previously. The name of this cache ... Read More

a.sort, sorted(a), np_argsort(a) and np.lexsort(b, a) in Python

Pradeep Elance
Updated on 30-Jun-2020 08:34:19

468 Views

Ordering of data elements in a specific order is a frequently needed operation. To sort elements in an array, python uses the functions named sorted() and array.sort().sorted(array)This function returns a sorted array without modifying the original array.a = [9, 5, 3, 1, 12, 6] b = sorted([9, 5, 3, 1, 12, 6]) print "Sorted Array :", print (b) print "Original Array :", print (a)Running the above code gives us the following result −Sorted Array : [1, 3, 5, 6, 9, 12] Original Array : [9, 5, 3, 1, 12, 6]list.sort()The sort function returns a sorted array by doing in-place modification ... Read More

__name__ (A Special variable) in Python

Pradeep Elance
Updated on 30-Jun-2020 08:36:00

2K+ Views

Unlike other programming languages, python is not designed to start execution of the code from a main function explicitly. A special variable called __name__ provides the functionality of the main function. As it is an in-built variable in python language, we can write a program just to see the value of this variable as below.print type(__name__) print __name__Running the above code gives us the following result − __main__As you can see above the value of the __name__ variable is of string data type and equals to ___main__.Below are the two key features of __name__ variable.1. When you run any well-written stand-alone ... Read More

Using Iterations in Python Effectively

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

125 Views

In this article, we will learn about how to implement iterators and their effective implementation in Python 3.x. Or earlier. Let’ s take a look at various methods available in python which implements iterators.Type 1 − Implementation Of While Loop With Known LengthExample Code Live Demogenre = ("Python", "C", "C++", "Java") print("The topic available on Tutorial's Point are:") i = 0 while (i < len(genre)): print (genre[i]) i += 1ExplanationDue to its less compact structure, this method is not favored. Error Handling is also difficult in this case . Large-scale programs or designs doesn’t use ... Read More

Using List as Stack and Queues in Python

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

5K+ Views

In this article, we will learn about Stack & Queue structures in Python 3.x. Or earlier. Here we will discuss the working and modification within these data structures −This includes −Insertion operation (Push, Enqueue)Deletion operation (Pop, Dequeue)Display / Traversing OperationPrerequisites: List & List OperationsRelated Data Structure: List ManipulationRelated ImagesStackIn stacks, objects are stored one over another, and these objects get removed in the reverse order of the arrival i.e. LIFO concept is followed. LIFO means Last in First Out type arrangement is followed in the Stack data structure.Operations on a Stack −Addition / Appending of Element: This increases the stack ... Read More

Using Set() in Python Pangram Checking

karthikeya Boyini
Updated on 30-Jun-2020 08:14:53

198 Views

In this article, we will learn how to determine whether a string is “pangram” or not in Python 3.x. Or earlier. A pangram string contains every letter in the list of English language alphabets . Let us look at the illustration below −Provided Input: str = 'This is the python blog on Tutorial point' Desired Output: No Provided Input : str='I want to contribute to a 'dxyzwuvghlkfmq' open source project' Desired Output: YesBy definition, a perfect pangram includes every letter of the ‘26 English alphabets’ exactly once. This tutorial doesn’t include the concept of ‘perfect pangram’.Now Let’s look at the ... Read More

vars() function in Python

Syed Javed
Updated on 30-Jun-2020 08:13:57

420 Views

vars() function belongs to the collection of inbuilt functions provided by the Python standard library. It returns the __dic__ attribute of an associated object to the console.Syntaxvars(object)Return TypeParametersvars() function accepts only one parameter. It takes an object as its parameter which can be any module, class or any object having __dict__ attribute associated with it.This parameter is optional in nature. In case the function is used without parameters A dictionary containing local symbol table is displayed.Exceptions InvolvedIf the argument passed doesn’t match the attribute, it raises the TypeError exception.ScopeVars() acts like locals() method when no argument is passed.The locals() method ... Read More

Python zip() Function

Hafeezul Kareem
Updated on 30-Jul-2019 22:30:26

318 Views

zip() function is used to group multiple iterators. Look at the doc of the zip() function using help method. Run the following code to get the help on zip() function.Example Live Demohelp(zip)If you run the above program, you will get the following results.OutputHelp on class zip in module builtins: class zip(object)    | zip(iter1 [, iter2 [...]]) --> zip object    |    | Return a zip object whose .__next__() method returns a tuple where    | the i-th element comes from the i-th iterable argument. The .__next__()    | method continues until the shortest iterable in the argument sequence   ... Read More

Advertisements