An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
September 13, 2022 12:43 pm GMT
Original Link: https://dev.to/jindalkeshav82/append-function-in-python-4an
Append() function in Python.
1.What is Append function?
Append function is used to add a item at the end of the sequence if the condition is True.
Example1
subjects = ['Maths','Science','Physics']subjects.append('Chemistry')print(subjects)
Explanation:
Statement 1:
subjects = ['Maths','Science','Physics']
Statement 2:
append functions is being used in Statement 2 So, 'Chemistry' will be added at the end of the sequence.
Final Output:
['Maths', 'Science', 'Physics', 'Chemistry']
Example 2
list1=['Mathematics', 'AI', 'Science','Political Science']list2=[]for i in list1: if 'a' in i: list2.append(i)print(list2)
Explanation:
Iteration1:
"a" in 'Mathematics' , condition becomes True and it will be added at the end of list2.
Iteration1:
"a" in 'AI' , condition becomes FALSE and it will not be added in sequence.
Final output
['Mathematics', 'Political Science']
Original Link: https://dev.to/jindalkeshav82/append-function-in-python-4an
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To