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.