Professional Documents
Culture Documents
Ai05 2
Ai05 2
Ai05 2
Strings and
arrays
Table of contents
5.1 String
▪ Quote
– Strings can use both double and single quotes
– Use double quotes for strings that need to be printed in single quotes.
– Question?
Read string
▪ Index
– When you store a string in a variable, an array is automatically created.
– Python also provides an indexing method that starts with -1 from the last item and
decreases by 1.
String slicing
▪ If argument is omitted
– If there is only one colon (:)
If you put a '+' sign between two strings, the preceding and following
strings are merged.
String functions (methods)
▪ len() function
– Returns the length of the string (object) in parentheses
▪ method call
– Call various methods (functions) provided by string-type objects by
connecting them with dots (.)
String functions (methods)
▪ information collection
Question?
find vs rfind?
▪ in
– Determine whether a specific character or string exists in that string
String functions (methods)
▪ Changing information
– Methods (functions) that can modify the contents of a string
Swapcase ?
▪ Information segmentation
– Method to remove whitespace from string
Randomizing arrays
▪ choice()
– print random characters
▪ shuffle()
– In fact, the order of the array is reversed and stored
5.2 Types of Arrays
▪ List
– Multiple data types can also be entered
– Square brackets ([]) are used and each item is separated by a
comma (,)
▪ One-dimensional list
Copy of list
▪ Copy of list
- If you copy a to b and change the value stored at index 0 of b
▪ insert() method
– The first of the two input arguments is an index number
– Enter the value of the second factor at the position just before
the index number
List comprehension
▪ List comprehension
– For all elements stored in a specific list, only elements that meet the
condition are selectively added.
– After reading the elements of the list (①) into i(②) and testing the value in
the conditional expression (③), if the result is true, i(④) is entered into the
list.
[i for i in (List name) if (conditional expression)]
Delete an item in a list
▪ in
– Check if a string contains a specific character
▪ not in
– Example: Checking if a specific fruit is not present in the list
fruits
Repetition of elements
▪ len()
– Count the number of elements
▪ count()
– Measure the number of specific values
Find your index number
▪ index()
– Returns the index number of the first occurrence of x in the
array list
▪ sort()
– Example: Extract only items with the number of elements 2 from mylist and
create newlist
Tuple
▪ Tuple
– Arrays that cannot be changed after initialization
– Items are connected with a comma (,) in the format enclosed in
parentheses (())
– parentheses can be omitted
– Can contain strings, numbers, tuples, and lists
– Indexing, slicing, and method functionality are similar to lists.
Tuple
▪ indexing, slicing
▪ Superposition
Tuple
▪ Slicing ▪ Absorption
▪ reiteration
Functions of tuples compared to lists
▪ search function
– frequency check
▪ Dictionary dictionary
– A pair of key and value
– index by key (string, number, tuple)
– A dictionary must contain unique keys
– Cannot be used as a key if it contains an object that can be
directly or indirectly modified
Creating and inserting dictionaries
▪ Creation of a dictionary
– Curly braces { } create an empty dictionary
▪ Insertion
– Input as a set of unordered key:value pairs in the form of {key:value}
– Instead of using sequential numbers for indexes, use keys as if they were
indexes.
– If there are two or more items in the dictionary, they are connected by
commas (,)
Dictionary
▪ Merge dictionary
– Using the update() method
Create a dictionary with another array
▪ (Set)
– Composed of non-duplicate and ordered elements
– Use { } symbols as in a dictionary and separate elements with commas (,)
– Unlike a dictionary, a type with only keys and no values
– A list or tuple can be created in the form of a set using the set() function
Adding and deleting elements of a set
▪ Add
– add()
– update()
▪ Delete
– remove()
– pop()
set
▪ presence or absence
– Use 'in' or 'not in' to check if the corresponding value is a
member
set operation
Operator Meaning
| union
& intersection
- difference
^ Symmetric difference
Relational Operators on Sets
Operator Meaning
<= Check if all elements of the set on the left are on the right
< Check if all elements of the set on the left are on the right
However, the two must not be the same
>= Check if all elements of the set on the right are on the left
> Check if all elements of the set on the right are on the left
However, the two must not be the same
Big data analytics example
Question?
{'apple': 2, 'grape': 2, 'lemon': 1}