Found 10805 Articles for Python

How To Do Math With Lists in python ?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

7K+ Views

We  not only use lists to store a collection of values, but we also use it to perform some mathematical calculations or operations to do.Example 1import math data = 21.6 print('The floor of 21.6 is:', math.floor(data))OutputThe floor of 21.6 is: 21How To Calculate the Weighted Average of a ListExample 2cost = [0.424, 0.4221, 0.4185, 0.4132, 0.413] cases = [10, 20, 30, 40, 50] cost = [23, 10, 5, 32, 41] weight= [10, 20, 30, 40, 50] for i in range(len(cost)): cost[c] = (cost[i] * weight[i] / sum(weight)) cost = sum(cost) print(cost)Output72.84444444444445Example 3import math degree = 180 radian = math.radians(degree) ... Read More

How does Python's super() work with multiple inheritance?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

752 Views

Before going to explain super() first we need to know about multiple inheritance concept.Multiple inheritance : Means one child class can inherit multiple parent classes.In the following example Child class inherited attributes methods from the Parent class.Exampleclass Father:    fathername = ""    def father(self):    print(self.fathername) class Mother:    mothername = ""    def mother(self):    print(self.mothername) class Child(Father, Mother):    def parent(self):    print("Father :", self.fathername)    print("Mother :", self.mothername) s1 = Child() s1.fathername = "Srinivas" s1.mothername = "Anjali" s1.parent()OutputFather : Srinivas Mother : AnjaliIn the following example shows ( i.e)  super() works with multiple inheritancessuper() : ... Read More

What is the difference between del, remove and pop on lists in python ?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

420 Views

It does't matter how many lines of code you write in a program. When you want to remove or delete any elements from the Python list, you have to think about the difference between remove, del and pop in Python List and which one to useremove : remove() removes the first matching value or object, not a specific indexing. lets say list.remove(value)Examplelist=[10, 20, 30, 40] list.remove(30) print(list)Output[10, 20, 40]del : del removes the item at a specific index. lets say del list[index]Examplelist = [10, 20, 30, 40, 55] del list[1] print(list)Output[10, 30, 40, 55]pop : pop removes the item at a specific index and returns ... Read More

How are arguments passed by value or by reference in Python?

Sri
Sri
Updated on 30-Jul-2019 22:30:26

11K+ Views

Python uses a mechanism, which is known as "Call-by-Object", sometimes also called "Call by Object Reference" or "Call by Sharing"If you pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It's different, if we pass mutable arguments.All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.Examplestudent={'Archana':28, 'krishna':25, 'Ramesh':32, 'vineeth':25} def test(student):    new={'alok':30, 'Nevadan':28}    student.update(new)    print("Inside the function", student)    return test(student) print("outside the function:", student)OutputInside the function ... Read More

Does Python support polymorphism?

AmitDiwan
Updated on 12-Aug-2022 12:21:42

888 Views

Yes, Python supports polymorphism. The word polymorphism means having many forms. Polymorphism is an important feature of class definition in Python that is utilised when you have commonly named methods across classes or sub classes. Polymorphism can be carried out through inheritance, with sub classes making use of base class methods or overriding them. There are two types of polymorphism Overloading Overriding Overloading Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding Overriding means having two methods with the same method name and parameters (i.e., method signature). One ... Read More

Does Python support multiple inheritance?

AmitDiwan
Updated on 12-Aug-2022 12:19:02

2K+ Views

Yes, Python supports multiple inheritance. Like C++, a class can be derived from more than one base classes in Python. This is called Multiple Inheritance. In multiple inheritance, the features of all the base classes are inherited into the derived class. Let us see the syntax − Syntax Class Base1: Body of the class Class Base2: Body of the class Class Base3: Body of the class . . . Class BaseN: Body of the class Class Derived(Base1, Base2, Base3, … , BaseN): Body of the class The Derived class inherits from both Base1, Base2 and Base3classes. ... Read More

How many types of inheritance are there in Python?

Sri
Sri
Updated on 29-Jun-2020 11:16:47

388 Views

Inheritance is concept where one class accesses the methods and properties of another class.Parent class is the class being inherited from, also called base class.Child class is the class that inherits from another class, also called derived class.There are two types of inheritance in python −Multiple InheritanceMultilevel InheritanceMultiple Inheritance −In multiple inheritance one child class can inherit multiple parent classes.Exampleclass Father:    fathername = ""    def father(self):       print(self.fathername) class Mother:    mothername = ""    def mother(self):       print(self.mothername) class Daughter(Father, Mother):    def parent(self):       print("Father :", self.fathername)     ... Read More

Write a sorting algorithm for a numerical dataset in Python?

AmitDiwan
Updated on 12-Aug-2022 12:07:03

612 Views

Numerical Dataset in Python is for numerical datatypes. Sorting algorithm for integers, includes the following in Python − Bubble sort Shell sort Selection sort Bubble Sort in Python Bubble Sort is comparison-based sorting. The adjacent elements are compared and swapped to make the correct sequence − Example def bubblesort(myList): # Swap the elements to arrange in order for iter_num in range(len(myList)-1, 0, -1): for idx in range(iter_num): if myList[idx]>myList[idx+1]: temp = myList[idx] myList[idx] = myList[idx+1] myList[idx+1] = temp # Unsorted List myList = [40, 23, 7, 49, 32, 98, 76, 48, 87] print("Unsorted List = ", ... Read More

What is the difference between Cython and CPython?

AmitDiwan
Updated on 12-Aug-2022 12:09:59

7K+ Views

CPython CPython is the implementation of the language called “Python” in C. Python is an interpreted programming language. Hence, Python programmers need interpreters to convert Python code into machine code. Whereas Cython is a compiled programming language. The Cython programs can be executed directly by the CPU of the underlying computer without using any interpreter. Cython Cython is designed as a C-extension for Python. The developers can use Cython to speed up Python code execution. But they can still write and run Python programs without using Cython. But the programmers have to install both Python and C-compiler as a pre-requisite ... Read More

Difference between mutable and immutable in python?

Sri
Sri
Updated on 09-Sep-2023 09:42:39

2K+ Views

Python defines variety of data types of objects. These objects are stored in memory and object mutability depends upon the type, like Lists and Dictionaries are mutable it means that we can change their content without changing their identity. Other objects like Integers, Floats, Strings, and Tuples have no provision to change there assigned value for an index.List is mutable: Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it a most powerful tool in Python. Lists are mutable, and hence, they can be altered even after their creation.Example#Write a python ... Read More

Advertisements