Python Short Notes: Chapter 2: Variables and Simple Data Types
The document summarizes key concepts about working with lists in Python. It discusses how to access elements in a list, modify elements, add and remove elements, sort lists, find the length of lists, and use for loops to iterate through lists. Some key points include accessing elements using indexes, changing values by assigning to the index, adding to the end using append or to a specific index using insert, and removing using del, pop, or remove.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0 ratings0% found this document useful (0 votes)
46 views2 pages
Python Short Notes: Chapter 2: Variables and Simple Data Types
The document summarizes key concepts about working with lists in Python. It discusses how to access elements in a list, modify elements, add and remove elements, sort lists, find the length of lists, and use for loops to iterate through lists. Some key points include accessing elements using indexes, changing values by assigning to the index, adding to the end using append or to a specific index using insert, and removing using del, pop, or remove.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2
PYTHON SHORT NOTES
Chapter 2: Variables and Simple Data Types
Print(name.title()) converts first letter to capital
Print(name.upper()) to full caps and similarly name.lower() \t prints after a few spaces print("Languages:\nPython\nC\nJavaScript") Languages: Python C JavaScript you can combine \n and \t as well. print(“Hello\n\tWorld) To remove blank space at right end use “rstrip()” Ex favourite_language=”Python “ print(favourite_language.rstrip()_ Similarly use lstrip() and also to strip from both sides simultaneously use simply ‘strip()’
Accessing an Element in a list print(bicylces[0])
Use [-1] to access the last element Changing/modifying an element from list
For example, let’s say we have a list of motorcycles, and the first item in the list is 'honda'.
How would we change the value of this first item?
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles[0] = ‘ducati’
Adding an Element to the end : motorcyles.append(‘ducati’)
Adding an Element to a specific place motorcycles.insert(0, 'ducati') Deleting: del motorcycles[0] Pop an element. See this code first_owned = motorcycles.pop(0) : to pop any specific one If you’re unsure whether to use the del statement or the pop() method, here’s a simple way to decide: when you want to delete an item from a list and not use that item in any way, use the del statement; if you want to use an item as you remove it, use the pop() method. To remove something, but don’t know it’s positional value, then use motorcycles.remove('ducati') #removes ducati from the list The remove() method deletes only the first occurrence of the value you specify. If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to determine if all occurrences of the value have been removed. You’ll learn in chapter 7 to loop Sort elements in a list alphabetically : cars.sort() cars.sort(reverse=True) # to sort it in reverse alphabetic order This is permenant, you cannot revert back. So how to revert? See this code
To reverse a list order use: cars.reverse() # simply reverses the order
Len(cars) # to find the length of a list
Working With Lists – 4
Say we have a list of magicians, Magicians = [‘alice’ , ‘david’ , ‘carolina’ ]
For magician in Magicians print magician
#for magician in Magicians, the python retrieves the first value from Magicians and store it in magician as per the code