Cassie Nesler Guest Blog

RubberDucking

Cassie Nesler

When the author of my story Along the River Road, S. C. Ashby, first asked me to be a guest blogger for her author webpage, I had to promise not to use too much geekinese, so readers would not have to reference some urban dictionary constantly. I hesitated after the author asked me to guest because I didn’t think I had much to say, but since the book’s release in 2018, I’ve been getting tons of coding questions from noobs to bit-twiddlers to code jockeys to app-slappers. Most recently, questions have centered around Python, so I thought for my guest blog over the next few months, I would focus some on Python. If you are not acquainted with Python, let me just say that it’s known for its lack of syntax. When looking at Python code, you won’t see lots of curly braces and semicolons – features often called syntactical sugar. Alan Perlis, a well-known programmer, once commented about programming languages that have a lot of syntactical sugar, “Syntactical sugar causes cancer of the semicolon.” lmao

Versatility is also another cool feature of Python. Python can be used for all kinds of things. Need to code up a quick web crawler? Python does this easily. Need to compute some statistics? Python is expanding its rep as one of the best programming languages for statistics. Another cool feature of Python is dynamic typing, but I’ll get to that later. More than anything else, I really like Python’s flexibility. Python is the type of programming tool that gets out of your way and lets you get to work.

Probably the best part about Python is that I don’t have to download and install some over-hyped IDE or compiler. Python is an interpreted language and all I need to do is download the Python interpreter and then I can run it directly from my desktop. Or, if you want to, you can run Python directly from your web browser using Python.org’s web-based Python interpreter. I suggest you spend a little time looking over python.org webpage. Follow the python webpage instructions for downloading Python or find the web-based interpreter and get some progging done.

Let me give you some examples of Python’s languaging by starting with the classic first program – Hello, world! Here’s how you write in Python:

>>> print('Hello, world!')

Type this into the interpreter and press the Enter key. You will see:

Hello, world!
>>>

The Python interpreter uses what is called REPL. That stands for Read, Evaluate, Print, Loop.

The interpreter reads what you type in, that is, it evaluates what it reads and figures out what to do. The computer then prints the result of its evaluation, and then loops back to the prompt (>>>). When you type in the function print(‘Hello, world!’), Python reads the input, determines that it is supposed to display the phrase ‘Hello, world!’ on the screen, places it to the screen, and then displays the prompt for more input. Pretty simple. Even noobs will catch on quickly.

Let’s try something else. Type in the number 1 and see what happens:

>>> 1
1

When the interpreter evaluates a numeral, it simply keeps the number as is. So, numerals evaluate to themselves, so typing in 1 gets a 1 as output. Now try this:

>>2 + 2
4

Python reads the input, which is an arithmetic statement. It then evaluates the statement and determines the result – 4. It prints the 4 and then displays the prompt for more input.

Now try this:

>>>'Goodbye, world!'
Goodbye, world!

You must put the phrase inside single quotes or you get an error:

>>> Goodbye world!
File "<stdin>", line 1
Goodbye world!
            ^
SyntaxError: invalid syntax

We’ll talk a lot more about errors later.

Now let’s look at how to use variables in Python. If you know another programming language, such as C++ or Java, try creating a variable as you would in one of those languages:

>>> int num = 100
File "<stdin>", line 1
int num = 100
      ^
SyntaxError: invalid syntax

Uh oh. What happened? What happened is Python doesn’t have explicit data types like you’ve experienced in other languages. To create a variable, just start using it:

>>> num = 100
>>>

Variables are just created and used in Python without declaring its data type. That doesn’t mean that Python doesn’t keep track of the data type of its variables. Type this to see how Python handles types:

>>>type(num)
<class 'int'>

That’s Python’s way of saying that the variable num is storing an integer (int). Now watch this:

>>> num = 'Hello, world!'
>>

WTH? You can store a string in a variable that’s storing a number? Yep, can-do. This showcases another cool feature of Python – dynamic typing. Variables can hold whatever type of value you want them to hold at any time. If we check the type of num now, we see that it is now storing a string:

>>> type(num)
<class 'str'>
>>>

Dynamic typing allows you to create programs that are easy to modify, and programs that don’t need a lot of planning to create. Of course, there’s a downside to this (More on that later: ml8r), but for now we’ll stress the pros that dynamic programming languages are easier to use because they’re so flexible.

While demonstrating the dynamic nature of Python variables, we also run across another feature of the language – assignment statements. An assignment statement is how we give a variable a value, as in num = 100. Assignments are always made with the variable on the left side, the equal sign (the assignment operator) in the middle, and a value or expression on the right side. Here are some examples:

>>> name = "Jane"
>>> salary = 35000
>>> greeting = 'Hello world!'
>>> sum = 23 + 67

Note that text (strings) can be entered with either single quotes or double quotes. The last example shows that you can assign an expression to a variable. In this case, the expression is evaluated first (90) and then the value is assigned to the variable. L8R, we’ll see that expressions such as this one (called an arithmetic expression) can be as complex as you need them to be.

One last thing I want to talk about is how variables are named in Python. Variable names must start with an alphabetic character and the rest of the name can contain more alphabetic characters or numbers or the underscore character (_). If you start a variable name with a number or try to include other characters, such as symbols, into a variable name, Python will complain with an error message and not let you use that variable.

For those of you new to Python, I hope this short intro will lead you to rtm and explore more. Check back next month and I’ll cover how to create functions using Python.

Leave a Reply