1/102
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
String
Sequence of characters that can be stored in a variable
String literal
string value specified in the source code of a program
Give an example of a string literal
any text in between quotation marks
The string type is a special construct known as a
sequence type
What is a sequence type
A type that specifies a collection of objects ordered from left to right
What is the index position of a string
The first character is at index 0, the second character is at index 1
How are strings assigned?
Strings can be assigned to variables just like other data types. For example, str1 = 'Hello' or str1 = str2
What is an Empty String?
An empty string is a sequence type with 0 elements, created using two quotes (e.g., my_str = '').
How do you find the length of a string?
The len() function is used to find the length (number of characters) of a string.
What does the len() function do?
It finds the number of characters in a string
How do you access characters in a string?
Index Positions, variable[index], —> this accesses the characters from left to right, negative numbers access characters from the right
What is String Concatenation?
It combines strings together (***without adding spaces)
What would ‘New’ + ‘York’ result in?
‘NewYork’
Are strings mutable?
Strings are immutable they cannot be changed in place. Instead a new string is created each time.
How do you make changes to a string if its immutable?
You create a new string with the desired modifications
What is an empty string
string with 0 characters, they are used as placeholders
Can you change individual characters in a string in Python? Why or why not?
No, you cannot change individual characters in a string in Python. Strings are immutable, meaning that once created, they cannot be altered in place. If you want to change a string, you create a new string with the desired modifications.
Unicode
Unicode is a character encoding standard that assigns a unique number (code point) to every possible character, including letters, numbers, symbols, and more. Python uses Unicode to represent characters as code points, allowing it to work with a wide range of characters from different languages and scripts.
Code Point
numerical value that represents a specific character in the Unicode standard
Escape Sequence
two item sequence in a string that represents a special character using a backlash followed by a specific character
What is \n
newline
\\
backslash
\’
single quote
\”
double quote
How do you create a raw string?
add an r before a string literal
Give an example of formatting a raw string
r’text’
What do raw strings do
escape sequences are treated as literal characters, not interpreted
what does the ord() function do?
returns the encoded integer value for a string of length one—> helpful in finding the numerical encoding of a character
what does the chr() function do
it returns a string of one character for an encoded integer —> you can convert an encoded value back into a character
What does ‘Jen\nJoe\\nMaya\\\nAlex’
Jen
Joe\nMaya\
Alex
What is a formatted string literal / f-string?
allows a programmer to create a string with placeholder expressions that are evaluated as the program executes
How do you start a formatted string?
f’blahblah’
What does the equal sign do in an f-string
it prints both the expression and the result Ex: f'{2*4=}' produces the string "2*4=8".
What do double braces do in an f-string?
They place a curly brace x: f'{{Jeff Bezos}}: Amazon' produces the string "{Jeff Bezos}: Amazon".
What does format specification inside a repacement field do?
Allows a values formatting in the string to be customized
How is a format specification introduced inside a replacement field
a colon is used to separate the WHAT from the HOW Ex: {4:.2f} formats 4 as 4.00.
What does a presentation type do in a format specification
determines how to represent a value in text form, such as an integer (4), a floating point (4.0), a fixed precision decimal (4.000), a percentage (4%), a binary (100), etc.
what does the format specification :s do?
string(can be omitted as it is defult)
what does the format specification :d do?
decimal(integer values only) 12:03d —> 012
what does the format specification :b do?
binary(integer values only)
what does the format specification :x/X do?
Hexadecimal in lowercase(x) and upper case(X) int values only
what does the format specification :e do?
exponent notation (e to the___)
what does the format specification :f do?
fixed point notation(six places of precision)
what does the format specification :.[precision]f do?
fixed-point notation(you can specify how many decimals!
what does the format specification :0[precision]d do?
leading 0 notation —> 4:03d would be 004
What is a container
construct used to group related values together and contains references to other objects instead of data
What is a list
a container created by surrounding a sequence of variables or literals with brackets [ ].
What is a list item called
An element
the index in a list
starts with 0
How do you access a specific item in a list
listname[index]
Are lists mutable?
Lists are mutable so, you can update list elements
How do you add an element to a list
list.append(value)
How do you remove an element from a list
list.pop(i) —> removes the element at index i from list
list.remove(value) —> removes the first element that is value
len(list)
length of 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)
Count the number of occurrences of the value val in the list.
Define tuple
stores a collection of data, like a list, but is immutable. supports len, indexing and other sequence functions
how is a tuple generated
creating a list of csv like (5,15,30)
When is a tuple used
Used when context of values is known ex latitude and longitude are always in a specific order
What is a set, and what are its properties
A set is an unordered collection of unique elements
Elements are unordered: Elements in the set do not have a position or index.
Elements are unique: No elements in the set share the same value.
What does the set() function do
it accepts a sequence-type iterable object(list, tuple,string)
How do you make a set literal?
{value,val,v} same can be done with set([1,2,3])
What happens if you pass a list into set()
it causes any duplicates to be omitted in the created set
What happens if you try to access an index in a set
RUNTIME error, there is no order!
What does remove() and pop() do to a set
removes a specific value, pop removes a random value
len(set)
number of elements in a set
set1.update(set2)
adds the elements in set2 to set1
set.add(value)
adds value into the set
set.remove(value)
removes value from the set
set.clear()
clears all elements from the set
set.intersection(a,b,c)
Returns a new set containing only the elements in common between set and all provided sets.
set.union(set_a, set_b, set_c...)
Returns a new set containing all of the unique elements in all sets.
set.difference(set_a, set_b, set_c...)
Returns a set containing only the elements of set that are not found in any of the provided sets.
set_a.symmetric_difference(set_b)
Returns a set containing only elements that appear in exactly one of set_a or set_b
Dictionaries include
Key:Value Pairs
curly braces to surround key:value pairs
createes dictionary contents
How to access dictionary entries
dictionary[key]
dict[k] = v
Adds a new key-value pair
del dict[k]
deletes value
Sequence types include
string, list, and tuple are containers for collections of objects ordered by position in the sequence
Mapping Type includes
dict type
Which types are immutable
tuple
Is a list invalid if it has multiple types?
No
Do dictionaries use the append function?
No
What is the syntax for tuples
parentheses
What is a membership operator
in and not in operators which yield True or False. Can be used to check if a string is a substring
Membership in dictionary
implies that a specific key exists in the dictionary, does not return value
What is an implicit conversion
type conversion automatically made by interpreter5.5
While loop
A while loop is a construct that repeatedly executes an indented block of code (known as the loop body) as long as the loop's expression is True.
sentinel value
a value that when evaluated by the loop expression causes the loop to terminate.
For loop
loops over each element in a container one at a time, assigning a variable with the next element that can then be used in the loop body.
How do you iterate a for loop backwards
for name in reversed(list)
range(Y)
sequence of all non-negative integers less than Y
range(X,Y)
Sequence of integers >=X and less than Y
range(X,Y,Z)
>=X and <Y, incrementing by Z
When should you use a for loop
when the number of iterations is computable or when accessing the elements of a container