Cassie Nesler Guest Blog

Rubber Ducking: More Python Features for Function Fun

Greetings, fellow snake charmers, that is: Python coders!

So, as a way to simply jog your memory about if-then statements: the if-then statement is a very basic control flow statement. It simply lets your program know to execute a certain section of code only if a particular test evaluates to true. For example, let’s say your programming for a tutorial concerning motion and scooters. The class of scooter could allow the brakes to decrease the scooter’s speed only if the scooter is currently in motion. So the “if” is currently in motion and the “then” is apply brakes. So, let’s get moving forward.

Photo courtesy: <a href=”https://www.freepik.com/free-photo/business-woman-working-on-laptop_1155973.htm”>Designed by Suksao</a>

 

 

 

 

 

 

 

 

 

 

 

 

Last time, we were looking at using Python to do some real word processing by writing a function that takes a word as an argument and returns the plural of the word. To really do this, though, we need to know some additional features of Python.

To form a word’s plural, we need to be able to decide how to do it based on the last part of the word—Oh, yuh, I know, English—we might need to look up all the rules on making plurals—not now, but later—now where did I put that English book?)

In order to make a decision in Python, we have to use a form called an if-statement. The if-statement makes a comparison between two objects and then executes one of several possible sets of Python statements based on the result of the comparison.

Graphic courtesy: Freepiks.com

 

Comparisons are made using one of the relational operators.

These operators are:

  • == (Equals)
  • > (Greater than)
  • >= (Greater than or equal to)
  • < (Less than)
  • <= (Less than or equal to)
  • != (Not equal to)

Here are some examples of using the relational operators:

  • x = 10
  • x < 100
  • x >= 10
  • x > 5
  • w = “hello”
  • w == “Hello”
  • w >= “hello”

The result of the comparison is either a True or a False value. These values are called Boolean values. These Boolean values are always the output of a relational operation.

Graphic courtesy: Freepiks.com

Using the relational operators, we can now make decisions in our Python code with the if-statement. The if-statement examines a relational expression, such as x == 10, (Said: “X equals ten.”) and then executes one or more Python statements based on the result of the relational expression.

For example:

x = 10

if x <= 10:

y = x

You can translate this code into, “Assign 10 to x. If the value of x is less than or equal to 10, assign the value of x to y.”

There are two things you need to notice here:

First, there is a colon after the relational expression.

This is necessary so that Python knows when the relational expression ends.

Also, the statement to execute if the relational expression is True must be indented from the line above it.

If you don’t do this, Python will send you an error message.

 

Also, notice that nothing happens if x is not less than or equal to 10.

That requires something called an if-else statement.

Graphic courtesy: https://fusioncell.ai/first-python-application-one-day/

What?!??!?? I hear you say with exasperation in your voice as you slump into your chair. You thought there were just if-then statements? You forgot about if-else statements?

So, okay—deal with it, there are if-else statements.

 

They looks like this:

x = 10

if x <= 10:

y = x

else:

y = 0

 

This code translates to: “Assign the value 10 to x.

If x’s value is less than or equal to 10, assign x’s value to y.

Otherwise, assign the value 0 to y.”

 

So, this is the first part of what we need, so that we can create a useful pluralizing function.

Now we need to learn how to examine the parts of a string.

You remember what strings are, right?

A string is a sequence of characters surrounded by quote marks, as I noted in an earlier blog.

We refer to this sequence as a list.

A list in Python is a sequence of values that can be grouped together.

Graphic courtesy: https://www.popsci.com/absolute-python-bundle

 

For example, we can create a list of characters like this:

>>> word = [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

The character sequence is ordered and we can refer to each value in the sequence by referring to its position in the list.

The first value, or element, is always at position 0.

We can get Python to return the value by writing this:

>>> word[0]

‘h’

>>> word[1]

‘e’

 

We can continue until we get to the last element:

>>> word[4]

‘o’

Using this notation, we can display the list as one entity:

>>> print(word[0]+word[1]+word[2]+word[3]+word[4])

hello

It turns out that a string can be treated exactly like a list.

 

For example:

>>> word = “hello”

>>> word[0]

‘h’

>>> word[4]

‘o’

 

Knowing this, we now have a way to find just the last letter of a word.

However, this technique is not perfect because not every word is five letters long, of course.

It turns out there’s a built-in function we can use to find the exact length of a string or a list: len.

 

Here’s how to use len:

>>> word = “hello”

>>> len(word)

5

>>> name = [‘M’, ‘a’, ‘r’, ‘y’]

>>> len(name)

4

 

With this function, we can find the last letter of a string by subtracting one from the length of the string:

>>> last_letter = word[len(word)-1]

>>> last_letter

‘o’

Now we’re to the point where we can examine a word’s last letter as the first step in determining its plural form.

For example, we know that some words that end in ‘y’ form a plural by removing the ‘y’ and adding ‘ies’.

But how do we do that?

We need another technique called a string splice.

A splice is a part of a string, either the first part, the middle part, or the last part.

We’ll concentrate here on how to get the first part of a word with a splice.

You form a splice by specifying a number in square brackets either before or after a colon.

We want the first part of a word, so the number comes after the colon.

 

For example, to return a splice with the first two characters of a word, write:

>>> word = “hello”

>>> word[:2]

‘he’

 

You can return all, but the last letter like this:

>>> word = “try”

>>> word[:word(len)-1]

tr

>>> word = “country”

>>> word[:word(len)-1]

countr

 

So, knowing this, it’s just a short hop to a function that can make a plural out of words that end in ‘y’

Oh, just kidding! This only works in certain cases, of course, that’s because we are using English!

Graphic courtesy: https://datafloq.com/read/why-python-programming-language-data-science/2426

 

Here is what it would look like in Python:

>>> def plural(word):

…   last_letter = word[len(word)-1]

…   if last_letter == ‘y’:

…     return word[:len(word)-1] + “ies”

…   else:

…     return word + “s”

…

>>> plural(‘try’)

‘tries’

>>> plural(‘country’)

‘countries’

>>> plural(‘kite’)

‘kites’

>>> plural(‘buy’)

‘buies’

 

OMG! That is not correct English!!

It looks like this function needs a little work.

In order to make this function give us proper English, we need to learn about repetition with loops.

So, next time I will cover loops and we will know all the code components we need to make this work.

So, work on the plural function to make it more useful and

stop being such a noob—rtm.

ltr!

Cassie

Leave a Reply