Found 27104 Articles for Server Side Programming

How to calculate absolute value in Python?

Vikram Chiluka
Updated on 14-Feb-2023 10:04:35

8K+ Views

In this article, we will show you how to calculate the absolute value in python. Below are the methods to accomplish this task: Using User-Defined Function (Brute Method) Using abs() function Using math.fabs() function The magnitude of a number, whether positive or negative, is referred to as its absolute value. For example, the absolute value of -2, is 2, and 2 is simply 2. Important Points The absolute value is returned by the built-in abs() function. The math.fabs() function also returns the absolute value, but as a floating-point value. When we pass an integer or float value ... Read More

What is immutable in Python?

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

585 Views

Python defines variety of data types of objects. These objects are stored in memory. Contents of some objects can be changed after they are created while others can't be changed. Numeric objects such as integer, float and complex number objects occupy the memory and memory contents can not be changed. Such objects are called immutable. String and dictionary objects are also immutable. Tuple is also immutable. List object however is mutable because items in a list object can be modified, deleted or added in a list.

Which data types are immutable in Python?

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

11K+ 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

320 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

104 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

251 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

97K+ 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

267 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

518 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 submit an HTML form using JavaScript?

Abhishek
Updated on 25-Nov-2022 06:57:15

16K+ Views

In this tutorial, we will learn how we can submit an HTML form using JavaScript. To submit an HTML form using JavaScript, we are calling validate() to validate data when the onsubmit event is occurring. We will discuss the direct method of submitting the HTML form as well as the method which checks that none of the fields is empty. Both of these methods are listed below − Using the Form submit() Method Using the manual validations Let us discuss both of these methods in detail in code examples associated to each. Using the Form submit() Method The ... Read More

Advertisements