Strings, Lists, Dictionaries, Tuples, and Sets. Flashcards
What are strings?
Strings are sequences of characters, using the syntax of either single quotes or double quotes:
Ex. ‘hello’, “Hello”, “I don’t do that”.
Because strings are ordered sequences it means we can use _______ or ________.
indexing, slicing.
Indexing allows you to grab a single character or a subsection of multiple characters of a ordered sequence?
a single character.
Slicing allows you to grab a single character or a subsection of multiple characters of a ordered sequence?
subsection of multiple characters of a string.
Give the syntax for indexing the last character of the string Hi = “Hello World”.
In: Hi[-1]
or the one liner: “Hello World”[-1].
Give the syntax to reverse the order of the string Hi = “Hello World” using slicing.
In: Hi[ : : -1]
True/False: Arithmetic can be applied to variables.
True, two methods that was given was catenation and string multiplication.
Describe what string catenation does.
String catenation uses slicing to grab a subsection of a string to transform into another string. ex. In: name = 'Sam' In: last_letters = name[1: ] Out: am In: 'P' + last_letters Out: Pam
Describe what string multiplication does.
String multiplication multiplies a string. ex. In: letter = 'z' In: letter*10 Out: 'zzzzzzzzzz'
String interpolation, sticking variables into strings, was taught using two methods. The format method and f-string. Give a basic example of how to use format method.
Format method:
Ex1. print(‘The {2} {1} {0}’.format(‘fox’, ‘brown’, ‘quick’))
Ex2. name = ‘Carlos’
print(‘Hello, my name is {}’.format(‘name’))
String interpolation, sticking variables into strings, was taught using two methods. The format method and f-string. Give a basic example of how to use f-string method.
f-string method:
Ex1. name = ‘Carlos’
age = ‘35’
print(f’{name} is my name and I am {age} years old.’)
What are lists?
Lists are ordered sequences that can hold a variety of object types.
Since lists are ordered sequences, they support what three concepts?
Slicing, indexing and nesting.
Give a general syntax of indexing.
In: variable = data type
In: variable[index_number]
or
In: ‘data_type’[index_number] for strings only.
Give a general syntax of slicing.
In: variable = data type
In: variable[start:stop:step]
or
In: ‘data_type’[start:stop:step] for strings only.
What are dictionaries in python?
Dictionaries are unordered mappings for storing objects.
What is the key difference between dictionaries and lists?
Dictionaries are unordered mappings so they cannot utilize functions like slicing, indexing and sorting; whereas lists are ordered sequences which can.
___________ & ____________ are mutable, but ___________ are immutable.
Lists, dictionaries, tuples.
When creating one of these data types, each data type uses a unique character to enclose their data. Strings use ___, lists use ___, dictionaries use ___, tuples use ___.
When recalling data though, they all use the same characters __.
’’, [], {}, (); []
Tuples are similar to lists. The key difference is that tuples are __________.
immutable.
What are sets?
Sets are unordered collections of unique elements. They give only one representation of multiple elements. Thus, In: list = [1,2,1,1,3,4,4,4] In: set(list) Out: (1,2,3.4)
What are booleans?
Booleans are operators that allow you to convey True or False statements.