3 -Built-in Data Structures, Functions, and Files

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/31

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

32 Terms

1
New cards

What is a tuple in Python?

A fixed-length, immutable sequence of Python objects.

2
New cards

How do you create a tuple?

Using a comma-separated sequence of values, e.g., tup = 4, 5, 6 or tup = (4, 5, 6).

3
New cards

How do you access elements in a tuple?

Using square brackets [] and the index, e.g., tup[0].

4
New cards

Can you modify a tuple after creation?

No, tuples are immutable, but you can modify mutable objects inside a tuple (e.g., lists).

5
New cards

How do you concatenate tuples?

Using the + operator, e.g., (1, 2) + (3, 4) results in (1, 2, 3, 4).

6
New cards

What is tuple unpacking?

Assigning the elements of a tuple to variables, e.g., a, b, c = (1, 2, 3).

7
New cards

What method counts occurrences of a value in a tuple?

The count method, e.g., tup.count(2).

8
New cards

What is a list in Python?

A variable-length, mutable sequence of Python objects.

9
New cards

How do you create a list?

Using square brackets [] or the list() function, e.g., a_list = [2, 3, 7] or b_list = list((1, 2, 3)).

10
New cards

How do you add an element to the end of a list?

Using the append method, e.g., a_list.append(4).

11
New cards

How do you insert an element at a specific position in a list?

Using the insert method, e.g., a_list.insert(1, 'red').

12
New cards

How do you remove an element from a list by value?

Using the remove method, e.g., a_list.remove('red').

13
New cards

How do you remove and return an element from a list by index?

Using the pop method, e.g., a_list.pop(2).

14
New cards

How do you sort a list in-place?

Using the sort method, e.g., a_list.sort().

15
New cards

How do you create a sorted copy of a list?

Using the sorted function, e.g., sorted(a_list).

16
New cards

What is a dictionary in Python?

A flexibly sized collection of key-value pairs, also known as a hash map or associative array.

17
New cards

How do you create a dictionary?

Using curly braces {} and colons, e.g., d1 = {'a': 'some value', 'b': [1, 2, 3]}.

18
New cards

How do you access a value in a dictionary?

Using square brackets [] and the key, e.g., d1['a'].

19
New cards

How do you add or update a key-value pair in a dictionary?

By assignment, e.g., d1['new_key'] = 'new_value'.

20
New cards

How do you remove a key-value pair from a dictionary?

Using the del keyword or the pop method, e.g., del d1['a'] or d1.pop('a').

21
New cards

What method returns all keys in a dictionary?

The keys method, e.g., d1.keys().

22
New cards

What method returns all values in a dictionary?

The values method, e.g., d1.values().

23
New cards

What is a set in Python?

An unordered collection of unique elements.

24
New cards

How do you create a set?

Using the set() function or curly braces {}, e.g., set([2, 2, 2, 1, 3]) or {2, 2, 2, 1, 3}.

25
New cards

How do you add an element to a set?

Using the add method, e.g., a_set.add(4).

26
New cards

How do you remove an element from a set?

Using the remove method, e.g., a_set.remove(4).

27
New cards

What is a list comprehension?

A concise way to create a new list by filtering and transforming elements of a collection, e.g., [x.upper() for x in strings if len(x) > 2].

28
New cards

What is a generator in Python?

A function that yields a sequence of values lazily, pausing after each one until the next is requested, using the yield keyword.

29
New cards

How do you open a file for reading in Python?

Using the open function with mode 'r', e.g., f = open('file.txt', 'r').

30
New cards

How do you read all lines from a file into a list?

Using f.readlines() or a list comprehension, e.g., [x.rstrip() for x in open('file.txt')].

31
New cards

How do you write to a file in Python?

Using the write or writelines methods, e.g., f.write('text') or f.writelines(list_of_strings).

32
New cards

What is the with statement used for with files?

To ensure a file is automatically closed after its block of code executes, e.g., with open('file.txt') as f: ....