How To Create A Shopping List In Python
- Create a list in Python
- Access an item or items in a list
- Loop through a Python List
- Check if an item exists in a list
- Get the length of a Python List.
- Sort a Python List
- Reverse sort items in a Python List
- Add a single item to a Python List
- Add multiple items to a Python List
- Add an item to a Python List at a specific Location
- Remove an item from a Python List by index using pop()
- Remove an item from a Python List by index using del keyword
- Remove an item from a Python List by value
- Remove all items from a Python List (empty a list)
Features of a List
A list is a collection of objects. It contains the following features:
- It is ordered
- It is changeable
- It allows duplicate members
Create a list in Python
#Lists are written with square brackets shoppinglist = ["milk","bread","apples","eggs","rice"] print(shoppinglist)
Access an item or items in a list
Lists are start at position 0. You can use slice notation like we covered in the strings chapter
#Create a list shoppinglist = ["milk","bread","apples","eggs","rice"] #print the first item in the list print(shoppinglist[0]) #print the last item in the list print(shoppinglist[-1]) #print items starting at position 2 and ending before position 4 print(shoppinglist[2:4])
Loop through a Python List
You can use a for loop to iterate through a list.
shoppinglist = ["milk","bread","apples","eggs","rice"] for x in shoppinglist: print(x)
Check if an item exists in a list
Use the "in" keyword to check if an item exists in a list.
shoppinglist = ["milk","bread","apples","eggs","rice"] if "milk" in shoppinglist: print("milk is in your shopping list")
Get the length of a Python List.
Use the len() method to get the length of a list
shoppinglist = ["milk","bread","apples","eggs","rice"] print(len(shoppinglist)
Sort a Python List
Use the sort() method to sort a list.
shoppinglist = ["milk","bread","apples","eggs","rice"] shoppinglist.sort() print(shoppinglist)
Reverse sort items in a Python List
Use the reverse() method to reverse sort a list.
shoppinglist = ["milk","bread","apples","eggs","rice"] shoppinglist.reverse() print(shoppinglist)
Add a single item to a Python List
Add a single item to a Python list using the append() method.
shoppinglist = ["milk","bread","apples","eggs","rice"] #add cookies to the list using append shoppinglist.append("cookies") print(shoppinglist)
Add multiple items to a Python List
Add multiple items to a Python List using the extends() method.
#Use extend to add a collection to a list proteinlist = ["steak","chicken","fish"] shoppinglist.extend(proteinlist) print("using extend") print(shoppinglist)
Add an item to a Python List at a specific Location
To add an item to a list at a specific location use the insert() method.
#Use extend to add a collection to a list shoppinglist = ["steak","chicken","fish"] shoppinglist.insert(1,"detergent") print("using insert") print(shoppinglist)
Remove an item from a Python List by index using pop()
To remove an item from a Python List by index use the pop() method.
shoppinglist = ["milk","bread","apples","eggs","rice"] #Use pop to remove an item by index print("use pop") shoppinglist.pop(0) print(shoppinglist)
Remove an item from a Python List by index using del keyword
To remove an item from a Python List by index use the del keyword.
shoppinglist = ["milk","bread","apples","eggs","rice"] print("use delete") del shoppinglist[0] print(shoppinglist) Remove an item from a Python List by value
To remove an item from a Python List by value use the remove() method.
shoppinglist = ["milk","bread","apples","eggs","rice"] print("use remove") shoppinglist.remove("bread") print(shoppinglist)
Remove all items from a Python List (empty a list)
To remove all items from a Python List use the clear() method.
print("use clear") shoppinglist = ["milk","bread","apples","eggs","rice"] shoppinglist.clear() print(shoppinglist)
- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling
How To Create A Shopping List In Python
Source: http://buildandteach.com/python-tutorials/lesson-11-lists-in-python/
Posted by: jordanwhass1990.blogspot.com

0 Response to "How To Create A Shopping List In Python"
Post a Comment