- 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 34487 Articles for Programming
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
756 Views
Both List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type.SimilaritiesConcatenation, repetition, indexing and slicing can be done on objects of both types>>> #list operations >>> L1=[1, 2, 3] >>> L2=[4, 5, 6] >>> #concatenation >>> L3=L1+L2 >>> L3 [1, 2, 3, 4, 5, 6] >>> #repetition >>> L1*3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> #indexing >>> L3[4] 5 >>> #slicing >>> L3[2:4] [3, 4]>>> #tuple operations >>> T1=(1, 2, 3) >>> T2=(4, 5, 6) >>> #concatenation >>> T3=T1+T2 >>> ... Read More
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
464 Views
List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type. However, main difference between list and tuple is that list object is mutable whereas tuple object is immutable. Immutable object can not be modified once it is created in memory. Hence it is not possible to add, modify or remove item from tuple object. On the other hand these operations can be performed on a list.
![Malhar Lathkar](https://www.tutorialspoint.com/assets/profiles/7345/profile/60_93814-1511173378.jpg)
22K+ Views
Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end. As we want second to last element in list, use -2 as index.>>> L1=[1,2,3,4,5] >>> print (L1[-2]) 4
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
80K+ Views
In this article, we will show you how to get the last element of the input list using python. Now we see 4 methods to accomplish this task − Using negative indexing Using slicing Using pop() method Using For loop Assume we have taken a list containing some elements. We will return the last element of the given input list using different methods as specified above. Method 1: Using negative indexing Python allows for "indexing from the end, " i.e., negative indexing. This means that the last value in a sequence has an index of −1, the second ... Read More
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
2K+ Views
In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we are going to append the list to another list(Concatenate Lists) in Python. The following are the different methods to accomplish this task − Using Concatenation(+) Operator Using the append method of the list object Using extend() Method Using itertools.chain() Method Obtain a list without duplicates Assume we have taken a list containing some elements. We will return the concatenated list of the given ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
338 Views
The getattr() methodThe getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.SyntaxThe syntax of getattr() method is −getattr(object, name[, default])The getattr() method can take multiple parameters −The getattr() method returns −value of the named attribute of the given objectdefault, if no named attribute is foundAttributeError exception, if named attribute is not found and default is not definedThe setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
83 Views
Python delattr()The delattr() deletes an attribute from the object if the object allows it.SyntaxThe syntax of delattr() is −delattr(object, name)The delattr() method takes two parameters −The delattr() doesn't return any value (returns None). It only removes an attribute (if object allows it).Exampleclass Coordinate: x = 12 y = -7 z = 0 point1 = Coordinate() print('x = ', point1.x) print('y = ', point1.y) print('z = ', point1.z) delattr(Coordinate, 'z') print('--After deleting z attribute--') print('x = ', point1.x) print('y = ', point1.y) # Raises Error ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
138 Views
The setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() method returns None.Exampleclass Male: name = 'Abel' x = Male() print('Before modification:', x.name) # setting name to 'Jason' setattr(x, 'name', 'Jason') print('After modification:', x.name)OutputThis gives the output('Before modification:', 'Abel') ('After modification:', 'Jason')
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
193 Views
The hasattr() method in PythonThe hasattr() method returns true if an object has the given named attribute and false if it does not.SyntaxThe syntax of hasattr() method is −hasattr(object, name)The hasattr() is called by getattr() to check to see if AttributeError is to be raised or not.The hasattr() method takes two parameters −The hasattr() method returns −True, if object has the given named attributeFalse, if object has no given named attributeExampleclass Male: age = 21 name = 'x' x = Male() print('Male has age?:', hasattr(x, 'age')) print('Male has salary?:', hasattr(x, 'salary'))OutputThis gives the output('Male has age?:', ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
274 Views
Python getattr()The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.SyntaxThe syntax of getattr() method is −getattr(object, name[, default])The getattr() method can take multiple parameters −The getattr() method returns −value of the named attribute of the given objectdefault, if no named attribute is foundAttributeError exception, if named attribute is not found and default is not definedExampleclass Male: age = 21 name = "Abel" x = Male() print('The age is:', getattr(x, "age")) print('The age is:', x.age)OutputThis gives the output('The age is:', 21) ... Read More