Saturday, May 28, 2022

String methods

We will cover some of the most commonly found methods -

str.capitalize(): Returns a copy of the string with only the first character capitalized and all others lowercase.

str.endswith(suffix[, start[, end]]): Returns True if the string ends with the specified suffix; otherwise, returns False. To look for multiple suffixes, a tuple can be used. The optional start is the index to start the search at, and the optional end is the ending index.

str.format(*args, **kwargs): Conducts a string formatting operation. This has been shown previously in other examples. There are many additional parameters that can be used with string formatting, so the official documentation should be referenced. On an additional note, the *args and **kwargs arguments are frequently found in the Python documentation. They simply indicate what types of arguments are accepted by a function or method. For *args, any argument passed in will be processed; **kwargs indicates key=value arguments are accepted. Naturally, if the argument passed in is not known, an error will be generated.

str.isalpha(): Returns True if all characters in the string are alphabetic. There are also methods for alphanumeric, numbers, ASCII-only, lowercase, and so on, checks.

str.ljust(width[, fillchar]): Returns a string that is left-justified with a length of width. By default, any extra space is padded with space characters, but fillchar can be used to provide alternative characters.

str.lower(): Returns a copy of the string that is all lowercase.

str.splitlines([keepends]): Returns a list of the individual lines within a string, as determined by common line separator characters.

The line breaks themselves are not included, unless keepends is True.

str.strip([chars]): Returns a copy of the string with the lead and trailing characters removed. By default, all whitespace is removed, but the optional chars argument can specify specific characters to remove.

str.title(): Returns a copy of the string in title case. Due to the algorithm used, apostrophe characters can cause problems with the expected output, so a review of the documentation is suggested prior to use.

As strings are simply specialized lists, they also have access to all the sequence methods listed in Sequence methods. Please note that these are not all the special methods available to strings.the full list can be found in the Python documentation.

Share:

Thursday, May 26, 2022

Sequence methods

The following methods are common to most sequence types, such as lists, tuples, sets, and strings, except where indicated:

  • x in seq: True if an item within the sequence is equal to x; otherwise, False is returned. This also applies to a subset of a sequence, such as looking for a specific character within a string.
  • x not in seq: True if no item within the sequence is equal to x; otherwise, False is returned.
  • seq1 + seq2: Concatenates two sequences; if immutable sequences, a new object is created.
  • seq * n: Adds a sequence to itself n times.
  • seq[i]: Returns the i'th item of a sequence, with the first object's index value = 0.
  • seq[i:j]: Returns a slice of the sequence, from i (inclusive) to j (exclusive). Not available with sets.
  • seq[i:j:k]: Returns a slice of the sequence, from i (inclusive) to j (exclusive), skipping every k values. Not available with sets.
  • len(seq): Returns the length of a sequence; that is, the number of items within the sequence.
  • min(seq): Returns the smallest item in a sequence.
  • max(seq): Returns the largest item in a sequence.
  • seq.index(x[, i[, j]]): Returns the index value for the first occurrence of value x in a sequence; optionally, the first occurrence at or after index value i but before index j. Not available with sets.
  • seq.count(x): Returns the total number of occurrences of x in a sequence. Not available with sets.

The following methods are common to all mutable sequence types, such as lists and strings, except where indicated:

  • seq[i] = x: Item i within a sequence is replaced with x.
  • seq[i:j] = iter: A slice of seq, from i (inclusive) to j (exclusive), is replaced with the contents of iterable object iter. Not available with sets.
  • del seq[i:j]: Deletes the given slice in seq. Not available with sets.
  • seq[i:j:k] = iter: A slice of the sequence, from i (inclusive) to j (exclusive), skipping every k values, is replaced by the contents of iter. Not available with sets.
  • del seq[i:j:k]: Deletes a slice of seq, skipping every k value. Not available with sets.
  • seq.append(x): Appends x to the end of seq. Not available with sets; use set.add(x) instead.
  • seq.clear(): Deletes all contents of seq.
  • seq.copy(): Makes a new copy of seq.
  • seq.extend(iter): Extends the sequence with the contents of iter.
  • Not available with sets; use set.union(*others) instead.
  • seq *= n: Updates the sequence with n copies of itself. Not available with sets.
  • seq.insert(i, x): Inserts item x into the sequence at index value i.
  • seq.pop([i]): Returns the item at index i and removes it from the sequence.
  • s.remove(x): Deletes the first item from seq that equals x.
  • s.reverse(): Reverses the sequence in-place.

Next we'll cover the string methods.

Share:

Monday, May 23, 2022

Sets

Sets are unordered collections of hashable objects; in other words, each object is unique. Sets are commonly used to see if a collection of objects contains a particular item, remove duplicates from a sequence, and compute a variety of mathematical operations.

Sets look like dictionaries, in that curly braces {} are used to create a set. However, unlike dictionaries, sets only have values; there are no key names within a set.

The following example shows how to create a set:

knights_set = {"Sir Galahad", "Sir Lancelot", "Sir Robin"}

Sets are also like dictionaries in that the objects they contain are unordered, and it is likely that calling a set will show a different order of objects compared to what was originally set.

There are actually two types of sets: set and frozenset. A regular set is mutable, in that it can be modified in-place. A frozenset is immutable and cannot be altered after creation. Therefore, a frozenset can be used as a dictionary key, like a tuple, but a regular set cannot.

Operations that are specific to sets and frozensets generally provide a way to quickly compare and shift out common/uncommon items between the different sets. Examples of the following methods can be seen in the next screenshot. The following non-exhaustive listing of set methods is an example of the more common set methods.

However, be sure to review the official Python documentation as there are some differences between set and frozenset methods:

there are some differences between set and frozenset methods:

  1. set1.isdisjoint(set2): Returns True if set1 has no elements in common with set2
  2. set1.issubset(set2): Returns True if every element in set1 exists in set2
  3. set1 < set2: Returns True if set1 is a true subset of set2 but not exactly equal to set2
  4. set1.issuperset(set2): Returns True if every element in set2 is in set1
  5. set1 > set2: Returns True if set1 is a true superset of set2 but not exactly equal to set2
  6. set1.union(set2, set3, ...): Returns a new set that includes elements from all given sets
  7. set1.intersection(set2, set3, ...): Returns a new set with all common elements between the given sets
  8. set1.difference(set2, set3, ...): Returns a new set with elements that exists in set1 but are not in any others
  9. set1.symmetric_difference(set2): Returns a new set with elements that are unique to each set
  10. set1.copy(): Returns a new set with a copy of the elements from set1

The following screenshot shows an example of set method:


Lines 104 and 105 create two different sets. Lines 106-110 are self explanatory, based on the previous definitions.

With line 111, we create a new set by merging set1 with set2. Line 112 shows a returned, empty set because there are no common elements between the two sets.

Line 113 shows the elements that exist in set1 but not set2, while line 114 shows all the unique elements.

Finally, line 115 presents a copy of set1; normally, this would be assigned to a new variable for later use.

In the next post we will discuss some of the more common methods available to Python data types.

Share:

Friday, May 20, 2022

Tuple unpacking

To create a tuple, we simply create a variable and assign items to it, separated by commas. The term for this is packing a tuple, because the data is packed into the tuple, all wrapped up and ready to go. To remove items from a tuple, you simply unpack it, as shown in the following screenshot:


In line 60, the tuple is packed with a sequence of numbers, and in line 61, the items in the tuple (the numbers) are unpacked and assigned to individual variables. Lines 62-65 demonstrate that each number has been assigned to separate variables.

Line 66 shows the same thing, except the tuple parentheses, have been dropped to show that they aren't necessary.

Tuple unpacking is nice when you have a lot of items to work with. Rather than having a separate variable for each item, you can pack them all into a tuple and work with that. When you need to, you can unpack the tuple and work with the individual items directly.

One benefit of tuple packing/unpacking is that you can swap items in place. With other languages, you have to create the logic to swap variables; with tuples, the logic is inherent in the data type, as shown in the following screenshot:


Tuple unpacking and in-place swapping is one of the neatest features of Python, in my opinion. Rather than creating the logic to pull each item from a collection and place it in its own variable, tuple unpacking allows you to do everything in one step. In-place swapping is also a shortcut; you don't need to create temporary variables to hold the values as you switch places.

Next topic of discussion will be Sets.

Share:

Wednesday, May 18, 2022

Tuples

The final built-in data type is the tuple. Python tuples work exactly like Python lists except they are immutable; that is, they can't be changed in place. They are normally written inside parentheses to distinguish them from lists (which use square brackets), but as you'll see, parentheses aren't always necessary; however, a comma is always required, as expressions can use parentheses too. Since tuples are immutable, their length is fixed. To grow or shrink a tuple, a new tuple must be created.

Since parentheses can surround expressions, you have to show Python when a single item is actually a tuple by placing a comma after the item. A tuple without parentheses can be used when a tuple is unambiguous. However, it's easier to just use parentheses than to screenshot out when they're optional.

Tuples typically store heterogeneous data, similar to how lists typically hold homogeneous data. It's not a hardcoded rule but simply a convention that some Python programmers follow. Because tuples are immutable, they can be used to store different data about a certain thing. For example, a contact list could conceivably be stored within a tuple; you could have a name and address (both strings) plus a phone number (integer) within a data object.

The biggest thing to remember is that standard operations, such as slicing and iteration, return new tuple objects. Commonly, lists are used for everything except when a developer doesn't want a collection to change. It cuts down on the number of collections to think about; plus, tuples don't let you add new items to them or delete data. You have to make a new tuple in those cases.

There are a few times when you simply have to use a tuple because your code requires it. However, a lot of times you never know exactly what you're going to do with your code and having the flexibility of lists can be useful.

So why use tuples? Apart from sometimes being the only way to make your code work, there are few other reasons to use tuples:

Tuples are processed faster than lists. If you are creating a constant set of values that won't change, and you need to simply iterate through them, use a tuple.

The sequences within a tuple are essentially protected from modification. This way, you won't accidentally change the values, nor can someone misuse an API to modify the data. (An API is an application programming interface. It allows programmers to use a program without having to know the details of the whole program.)

Tuples can be used as keys for dictionaries. One possible use of this is a crude inventory system, such as the following screenshot:


Tuples are great when you want to return multiple values from a function. Normally, you can only return a single value from a function. If you return a tuple, however, multiple items can be placed into a single tuple object, so you aren't violating the single value rule, because it is a single tuple, yet you still get all the items that are contained in the tuple.

In the next post we'll see how to create Tuples.

Share:

Monday, May 16, 2022

Working with dictionaries

There are a large number of methods that can be used with dictionaries. We won't talk about all of them, but some of the more common ones are shown in the following screenshot:


Line 36 checks to see whether a specified key exists within the dictionary.

Line 37 returns all the items that exist within the dictionary—both keys and their associated values. For more flexibility, you can look for just a dictionary's keys (line 38) or just the values (line 39).

To remove entries within a dictionary, you can delete single items, as demonstrated in line 40. To remove all entries in dictionary d1, you would use the d1.clear() method .

Since dictionaries are changeable, you can add and delete values to them without creating a new dictionary object, as shown in lines 23 and 40. Adding a new object to a dictionary only requires making a new key and value, whereas lists will return an index out-of-bounds error if the offset is past the end of the list. Therefore, you must use append() to add values to lists but simply make new key value entries for dictionaries.

The following screenshot is a more realistic dictionary example. The following example creates a table that maps programming language names (the keys) to their creators (the values). You fetch a creator name by indexing on the language name:


From this example, you might notice that the last command is similar to string and list iteration using the for command. However, you'll also notice that, since dictionaries aren't sequences (that is, the stored items are indexed by keyword and not position), you can't use the standard for statement. You must use the keys() method to return a list of all the keys which you can then iterate through like a normal list.

You may have also noticed that dictionaries can act like light weight databases. The preceding example creates a table, where the programming language column is matched by the creator's row. If you have a need for a database, you might want to consider using a dictionary instead. If the data will fit, you will save yourself a lot of unnecessary coding and reduce the headaches you would get from dealing with a full-blown database. Granted, you don't have the flexibility and power of a true database, but for quick-and-dirty solutions, dictionaries will suffice. 

There are a few key points about dictionaries that you should be aware of:

Sequence operations don't work. As previously stated, dictionaries are mappings, not sequences. Because there's no order to dictionary items, functions such as concatenation and slicing don't work.

Assigning new indexes adds entries. Keys can be created when making a dictionary (that is, when you initially create the dictionary) or by adding new values to an existing dictionary. The process is similar and the end result is the same.

Keys can be anything immutable. The previous examples showed keys as string objects, but any non-mutable object (such as numbers) can be used for a key. Numbers can be used to create a list-like object but without the ordering. Tuples are sometimes used to make compound keys; class instances that are designed not to change can also be used if needed. 

Share:

Friday, May 13, 2022

Dictionaries

Python lists, as previously shown, are ordered collections that use a numerical offset. To select an item in a list, you need to know its position within the list. Python dictionaries are unordered collections of objects, matched to a key name; in other words, you can reference an item simply by knowing its associated key.

Because of their construction, dictionaries can replace many typical search algorithms and data  structures found in C and related languages. For those coming from other languages, Python dictionaries are just like a hash table or associative array, where an object is mapped to a key name.

Dictionaries include the following properties:

  • They are accessed by a key, not an offset. Each item in the dictionary has a corresponding key; the key is used to call the item.
  • Stored objects are in a random order to provide faster lookup. When created, a dictionary stores items in a particular order that makes sense to Python, but may not make sense to the developer.
  • To get a value, simply supply the key. If you need to order the items within a dictionary, there is a container called OrderedDict that was added in Python 2.7, but it has to be imported from the collections library.
  • Dictionaries are variable-length, can hold objects of any type (including other dictionaries), and support deep nesting (multiple levels of items can be in a dictionary, such as a list within a dictionary within another dictionary).
  • They are mutable but can't be modified like lists or strings. They are the only data type that supports mapping; that is, a key is linked to a value.

Internally, a dictionary is implemented as a hash table. As previously stated, you create dictionaries and access items through a key. The key can be of any immutable type, such as a string, number, or tuple; basically, anything that can't be changed. Each key's associated value can be any type of object, including other dictionaries. The basic use of dictionaries is displayed in the following screenshot:


Line 19 creates the dictionary. Note that the brackets for dictionaries are curly braces, the separator between a key word and its associated value is a colon, and that each key:value is separated by a comma. In this example, the first mapping is a string to a string, the second is a string to an integer, and the last is a list to an integer.

Line 20 shows how to see how many items are contained within a dictionary. This value is only the number of mappings, not the individual keys/values contained within the dictionary.

Line 21 returns the value associated with the key cow. If you want to add a new item, you have to use the format in line 23—the name of the dictionary, followed by the new key within square brackets, and then what that key is equal to. If you try to make a new dictionary entry by trying to directly map the value to its key through a colon character (line 22), you will get an error.

After the new entry is created in line 23, we can verify it is there by simply calling the dictionary (line 24). Values in dictionary entries are completely accessible; in the case of line 25, we can increment the integer value by directly adding 1 to the appropriate key.

Compare this to lines 21 and 23. In line 21, calling the key returned its associated value. In line 23, adding the = sign to a key made a new dictionary entry. Thus, line 25 acts like a combination of those two—it gets the value associated to a key, and then makes a new dictionary entry by performing an operation on the value. In this case, we are simply adding 1 to the value, and then reassigning it as the key's associated value. Line 26 returns the entire dictionary to show that the new value associated with the chicken key has been incremented from 3 to 4.

In the next post we'll look into methods that can be used with dictionaries.

Share:

Thursday, May 12, 2022

Mutability

As mentioned several times, one of the special things about lists is that they are mutable; that is, they can be modified in place without creating a new object. The big concern with this is remembering that, if you do this, it can affect other references to it. However, this isn't usually a large problem; it's more of something to keep in mind if you get program errors.

The following screenshot is an example of changing a list using index offset, slicing, and deleting elements:


Line 12 changes the value for the element at index 1 (second position in the list). Line 14 swaps out the first two elements for new values. Line 16 deletes the first element; removing multiple elements through slicing is also allowed.

Next to lists, dictionaries are one of the most useful data types in Python, this we'll cover in coming posts.

Share:

Tuesday, May 10, 2022

Adding list elements

Adding new items to a list is extremely easy. You simply tell the list to add them, as shown in the  following screenshot. This also demonstrates how any item can be placed in a list, even disparate data types:


The append() method simply adds a single item to the end of a list; it's different from concatenation since it takes a single object and not a list. The append() method changes the list in-place and doesn't create a brand new list object, nor does it return the modified list. To view the changes, you have to expressly call the list object again, as shown in line 3. So be aware of that in case you are confused about whether the changes actually took place.

If you want to put the new item in a specific position in the list, you have to tell the list which position it should be in; that is, you have to use the index of what the position is. This is demonstrated in line 4 of the previous screenshot.

You can add a second list to an existing one by using the extend() method. Essentially, the two lists are concatenated (linked) together, as shown in the following screenshot:


Be aware that there is a distinct difference between extend() and append(). The extend() function takes a single argument, which is always a list, and adds each of the elements of that list to the original list; the two lists are merged into one. The append() function takes one argument, which can be any data type, and simply adds it to the end of the list; you end up with a list that has one element, which is the appended object.

Compare line 10 in the following screenshot to line 8 in the previous screenshot. Whereas appending the new_l list to the original list simply added each item from new_l to the original, essentially increasing the number of elements, when extending the exact same new_l list to the original, the entire list object was added, rather than the individual elements.


One of the special things about lists is that they are mutable, this we'll discuss in the  next post


Share:

Monday, May 9, 2022

Lists

Lists in Python are one of the most versatile collection object types available. The other workhorses are dictionaries and tuples, but they are really more like variations of lists. Python lists do the work of most of the data collection structures found in other languages, and since they are built in, you don't have to worry about manually creating them. Lists can be used for any type of object, from numbers and strings to other lists. They are accessed just like strings (since strings are just specialized lists), so they are simple to use. Lists are variable in length; that is, they grow and shrink automatically as they're used, and they can be changed in place; that is, a new list isn't created every time, unlike strings. In reality, Python lists are C arrays inside the Python interpreter and act just like an array of pointers.

The following screenshot shows the creation of a list and a few examples of how to use it:


After the list is created in line 42, lines 43 and 44 show different ways of getting the values in a list; line 43 returns the list object while line 44 actually prints the items that are in the list. The difference is subtle, but will be more noticeable with more complicated code.

Line 45 returns the first item in the list, while line 46 pops out the last item. Returning an item doesn't modify the list, but popping an item does, as shown in line 47, where the list is visibly shorter.

The biggest thing to remember is that lists are series of objects written inside square brackets, separated by commas. Dictionaries and tuples will look similar except they have different types of brackets.

Lists are most often used to store homogeneous values; that is, a list usually holds names, numbers, or other sequences that are all one data type. They don't have to; they can be used with whatever data types you want to mix and match. It's just usually easier to think of a list as holding a standard sequence of items.

The most common use of a list is to iterate over the list and perform the same action to each object within the list, hence the use of similar data types. This simple iteration is shown in the following screenshot:


Line 48 defines the list as a sequence of string values. Line 49 creates a for loop that iterates through the list, printing out a phrase for each item.

Lines 50 and 51 show alternative ways of iterating through and creating lists. This method is called list comprehension and is frequently found in code as a shortcut to writing a normal for loop to make a new list. Line 51 demonstrates that additional information can be provided to the returned values, much like the values returned in line 49.

One thing to note right now, however, is that you can use whatever word for the placeholder that you want; that is, if you wanted to use the name number instead of item in the preceding examples, you can do that. This is key because it was a weird concept for me when I first encountered it in Python. In other languages, loops like this are either hardwired into the language and you have to use its format or you have to expressly create the x value beforehand so you can call it in the loop. Python's way is much easier because you can use whatever name makes the most sense.

In the next post we'll further explore lists and see how new items are added to a list.

Share:

Friday, May 6, 2022

Combining and separating strings

Strings can be combined (joined or concatenated) and separated (split) quite easily. Tokenization is the process of splitting something up into individual tokens; in this case, a sentence is split into individual words. When a web page is parsed by a browser, the HTML, JavaScript, and any other code in the page is tokenized and identified as a keyword, operator, variable, and so on. The browser then uses this information to display the web page correctly, or at least as well as it can.

Python does much the same thing. The Python interpreter tokenizes the source code and identifies the parts that are part of the actual programming language and the parts that are data. The individual tokens are separated by delimiters, characters that actually separate one token from another. If you import data into Excel or another spreadsheet program, you will be asked what it should use as a delimiter: a  comma, tab, space, and so on. Python does the same thing when it reads the source code.

In strings, the main delimiter is a whitespace character, such as a tab, a newline, or an actual space. These delimiters mark off individual characters or words, sentences, and paragraphs. When special formatting is needed, other delimiters can be specified by the programmer.

String concatenation was demonstrated in Basic string operations. An alternative way to combine strings is by joining them. Joining strings combines the separate strings into one string. The catch is that it doesn't concatenate the strings; the join() method creates a string in which the elements of a string sequence are joined by a given separator. The following screenshot demonstrates this action. Line 29 is a normal concatenation; the results are printed in line 31. Line 30 joins string 1 with string 2, with the results in line 32: 


As you can see, the results are not what you expect. The join() method is actually designed to be used to create a string where the individual characters are separated by a given separator character. The following screenshot demonstrates this more common use of join():


After a sequence of strings is created in line 35 (known as a tuple, and explained further in Tuples), the join() method is called in two different ways. Line 36 is a simple call of the function itself; the result is a string, with the quotation marks shown. Line 37 is the print() function calling join(); the resultant string is printed normally, without the quote marks.

Finally, splitting strings separates them into their component parts. The result is a list containing the individual words or characters. The following screenshot shows two ways to split a string: 


In line 39, the default split is performed, resulting in the string being split at the spaces between words. Line 41 performs the string split on the commas, though essentially any character can be used.


Share: