Python Lists

SelfDeterminationFluxus avatar
SelfDeterminationFluxus
·
·
Download

Start Quiz

Study Flashcards

8 Questions

What is a common method for adding an item to the end of a list?

append()

What is the main difference between a set and a list?

Sets do not allow duplicates, lists do.

What is the purpose of the keys() method in a dictionary?

Returns a view object of all keys.

What is a characteristic of strings in Python?

They are immutable.

What is the purpose of the tuple() function?

Creates a new tuple.

What is the difference between the add() and discard() methods in a set?

add() raises an error if the item is already in the set, discard() does not.

What is the purpose of the index() method in a tuple?

Returns the index of the first occurrence of an item.

What is the purpose of the upper() method in a string?

Returns a new string with all characters uppercase.

Study Notes

Lists

  • Ordered collection of items
  • Can contain duplicates
  • Can be indexed and sliced
  • Mutable (can be modified)
  • Can be created using square brackets [] or the list() function
  • Methods:
    • append(): adds an item to the end of the list
    • extend(): adds multiple items to the end of the list
    • insert(): inserts an item at a specific position
    • remove(): removes the first occurrence of an item
    • sort(): sorts the list in ascending order
    • reverse(): reverses the order of the list

Sets

  • Unordered collection of unique items
  • No duplicates allowed
  • Mutable (can be modified)
  • Can be created using curly braces {} or the set() function
  • Methods:
    • add(): adds an item to the set
    • remove(): removes an item from the set
    • discard(): removes an item from the set if it exists
    • union(): returns a new set with all items from two sets
    • intersection(): returns a new set with items common to two sets
    • difference(): returns a new set with items in one set but not the other

Dictionaries

  • Unordered collection of key-value pairs
  • Each key is unique
  • Values can be of any data type
  • Mutable (can be modified)
  • Can be created using curly braces {} or the dict() function
  • Methods:
    • keys(): returns a view object of all keys
    • values(): returns a view object of all values
    • items(): returns a view object of all key-value pairs
    • get(): returns the value for a given key
    • update(): updates the dictionary with new key-value pairs

Strings

  • Sequence of characters
  • Immutable (cannot be modified)
  • Can be created using quotes ' or ", or the str() function
  • Methods:
    • upper(): returns a new string with all characters uppercase
    • lower(): returns a new string with all characters lowercase
    • strip(): removes leading and trailing whitespace
    • split(): splits the string into a list of substrings
    • join(): joins a list of strings into a single string

Tuples

  • Ordered collection of items
  • Immutable (cannot be modified)
  • Can be created using parentheses () or the tuple() function
  • Methods:
    • index(): returns the index of the first occurrence of an item
    • count(): returns the number of occurrences of an item

Lists

  • Ordered collections of items that can contain duplicates and be indexed and sliced.
  • Can be created using square brackets [] or the list() function.
  • Lists are mutable, meaning they can be modified.
  • Methods for modifying lists include:
    • append() to add an item to the end of the list.
    • extend() to add multiple items to the end of the list.
    • insert() to insert an item at a specific position.
    • remove() to remove the first occurrence of an item.
    • sort() to sort the list in ascending order.
    • reverse() to reverse the order of the list.

Sets

  • Unordered collections of unique items, meaning no duplicates are allowed.
  • Sets are mutable, meaning they can be modified.
  • Can be created using curly braces {} or the set() function.
  • Methods for modifying sets include:
    • add() to add an item to the set.
    • remove() to remove an item from the set.
    • discard() to remove an item from the set if it exists.
  • Set operations include:
    • union() to return a new set with all items from two sets.
    • intersection() to return a new set with items common to two sets.
    • difference() to return a new set with items in one set but not the other.

Dictionaries

  • Unordered collections of key-value pairs, where each key is unique.
  • Values can be of any data type.
  • Dictionaries are mutable, meaning they can be modified.
  • Can be created using curly braces {} or the dict() function.
  • Methods for accessing and modifying dictionaries include:
    • keys() to return a view object of all keys.
    • values() to return a view object of all values.
    • items() to return a view object of all key-value pairs.
    • get() to return the value for a given key.
    • update() to update the dictionary with new key-value pairs.

Strings

  • Sequences of characters that are immutable, meaning they cannot be modified.
  • Can be created using quotes ' or ", or the str() function.
  • Methods for modifying strings include:
    • upper() to return a new string with all characters uppercase.
    • lower() to return a new string with all characters lowercase.
    • strip() to remove leading and trailing whitespace.
    • split() to split the string into a list of substrings.
    • join() to join a list of strings into a single string.

Tuples

  • Ordered collections of items that are immutable, meaning they cannot be modified.
  • Can be created using parentheses () or the tuple() function.
  • Methods for accessing tuples include:
    • index() to return the index of the first occurrence of an item.
    • count() to return the number of occurrences of an item.

Learn about the characteristics and methods of lists in Python, including indexing, slicing, and mutability.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Python Data Structures Quiz
10 questions

Python Data Structures Quiz

ProficientRubellite avatar
ProficientRubellite
Python Lists Creation Quiz
3 questions

Python Lists Creation Quiz

DurableExtraterrestrial avatar
DurableExtraterrestrial
Python Lists
5 questions

Python Lists

EnchantingBugle avatar
EnchantingBugle
Use Quizgecko on...
Browser
Browser