Ch.4 programming types

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:30 AM on 4/9/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

30 Terms

1
New cards

string

is a sequence of characters that represents textual data like a person's name, a location, or a message to the user.

2
New cards

string literal

is a string defined in the source code of a program by surrounding the text value with single or double quotes, like 'MARY' or "MARY".

3
New cards

sequence type

a type that orders a collection of objects into a sequence from first to last.

4
New cards

len()

built-in function that is used to find the length of a string (and any other sequence type like a list)

5
New cards

Brackets

A programmer can reference a character at a specific index by appending brackets [ ] containing the desired index after the name of a string variable:

6
New cards

string concatenation

A program can add new characters to the end of a string in a process

7
New cards

aster is 5 characters long
ageratum is 8 characters long

fav_flower1 = input()

fav_flower2 = input()

print(fav_flower1, "is", len(fav_flower1), "characters long")

print(fav_flower2, "is", len(fav_flower2), "characters long")

8
New cards

The third character of grape is a

fav_fruit = input()

print("The third character of", fav_fruit, "is", fav_fruit[2])

9
New cards

States I've visited: Utah, North Carolina, Nevada

visited_list_name = input()

place1 = input()

place2 = input()

place3 = input()

places_visited_str = visited_list_name + ": " + place1 + ", " + place2 + ", " + place3

print(places_visited_str)

10
New cards

Your name is May

name = "May"

print(f"Your name is {name}")


11
New cards

Container

is a construct used to group related values together and contains references to other objects instead of data.

12
New cards

List

is a container created by surrounding a sequence of variables or literals with brackets [ ]. Ex: my_list = [10, "abc"] creates a new list variable my_list that contains the two items: 10 and "abc".

13
New cards

Element

A list item is called an element. A list is mutable, meaning that the elements in a list can be replaced, reordered, or removed.

14
New cards

Index

A list is also a sequence type, meaning the contained elements are ordered by position in the list, known as the element's index,

15
New cards

method

instructs an object to perform some action and is specified by providing the object name followed by a "." symbol and then the method name.

16
New cards

append()

list method is used to add new elements to the end of a list.

17
New cards

pop()

removes the element at the given index from the list. "bw", which is at index 1, is removed and 'abc' is now at index 1. Using pop(), a specific element can be removed according to its position in the list

18
New cards

remove()

removes the first element with a given value. "abc" is removed and now the list only has one element. remove() is useful for removing a specific element from a list according to its value.

19
New cards

[10, “bw”]

After append: [10, “bw”, “abc”]

my_list = [10, “bw”]

print(my_list)

my_list.append(“abc”)

print(f”After append: {my_list}”)

20
New cards

house_prices[1] = "$175,000"

Write a statement that performs the desired action. Assume the list house_prices = ["$140,000", "$550,000", "$480,000"] exists.

1)

Update the price of the second item in house_prices to "$175,000".

21
New cards

Sequence-type methods

are functions built into the Python language definitions of sequence types like lists and strings.

22
New cards

Some of the functions and methods useful to lists

len(list)

Find the length of the list.

list1 + list2

Produce a new list by concatenating list2 to the end of list1.

min(list)

Find the element in the list with the smallest value. All elements must be of the same type.

max(list)

Find the element in the list with the largest value. All elements must be of the same type.

sum(list)

Find the sum of all elements of a list (numbers only).

list.index(val)

Find the index of the first element in the list whose value matches val.

list.count(val

23
New cards

List data_list is created with four integers read from input. Output the fourth element multiplied by the third element minus the first element.

data_list = [int(input()), int(input()), int(input()), int(input())]

print(data_list[3] * data_list[2] - data_list[0])

24
New cards

Input

9
6
10
5

Your output

['9', '10', '5']


# Reads three values from input into years_list

years_list = [input(), input(), input()]

years_list.pop(1)

new_year = input()

years_list.append(new_year)

print(years_list)

25
New cards
  • index_value with the index of the element 8 in data_list.

index_value = data_list.index(8)

26
New cards

tuple

stores a collection of data, like a list, but is immutable – once created, the tuple's elements cannot be changed.Typically, tuples are surrounded with parentheses, as in (5, 15, 20). Note that printing a tuple always displays surrounding parentheses.

27
New cards

namedtuple

allows the programmer to define a new simple data type that consists of named attributes

28
New cards

set

is an unordered collection of unique elements. A set can be created using the set() function, which accepts a sequence-type iterable object (list, tuple, string, etc.)

# Create a set using the set() function.
nums1 = set([1, 2, 3])

# Create a set using a set literal.
nums2 = { 7, 8, 9 }

# Print the contents of the sets.
print(nums1)
print(nums2)
{1, 2, 3}
{7, 8, 9}

29
New cards

30
New cards