CODE EXAMPLES

Underneath this major page header you will find a plethora of short code examples using many commands and concepts found in Python 3.5 and later.  Meanwhile, for young beginners, it helps to have an exposure to a couple of very basic ideas/concepts.

Level: Basic to early intermediate
Examples tested on: Windows, sometimes on Linux

Really Really Basic Variables

One on the LESS consistent, and most critical, things about computer languages is the way they decide to handle variables. In any language, if you want to have, hold, save and/or manipulate some value you have to name it and more or less be aware of what “type” of variable it is. Based on that “type” you are expected, as a programmer, to know the ways you can, and cannot, use it.

You can get a much more technically precise explanation of Python variables with a completely different (and more accurate) perspective on the Python site at: https://docs.python.org/3/library/stdtypes.html.  That site will explain numerics, sequences, mappings, classes, instances and exceptions and all their sub-types plus lots of other stuff you may eventually actually need to know.

What follows here is kind of a beginner or dummy treatment to get you started:

Lets consider four common “types” of individual values: strings, integers, decimal numbers, and Boolean values.  These are all pretty common, but there are many more depending on the language.  Here is a quick explanation of these four:

String: any group of alphanumeric or special characters that are defined by surrounding them by single or double quotes.  “Dog” is a string but so is “123.456” if it is surrounded by quotes. Put quotes around one of Shakespeare’s plays and it becomes a string. The maximum length of a string theoretically depends on how much memory your computer has and can allocate. “BR-549” may be your phone number, but surrounded by quotes it is a string.

Integer: a number with no decimal component. There are a bunch of different “kinds” of “regular” integers and each kind has its own minimum and maximum value across various languages.  Some may be signed, some are not.  Generally they range from a minimum of -32,768 to a maximum of +65536 and if you need an integer larger than that you use a different variable, perhaps called a long integer – this was true in earlier versions of Python.  In modern Python (version 3.4 on up) there is no limit to the size of your integer other than the limitations of the memory in your computer.

Decimal Number: a base 10 number with a fractional (decimal) component. Like 3.141592653589793238462643383279502884197169399375105820974944592307816406286. They can be very precise, very big, and very small.

Boolean: true or false.  Don’t overlook how important Boolean values can be.

Sometimes each “type” of value can be either a “constant” (fixed and never changing once assigned) or a “variable” (something you can change and manipulate).

When we assign a name or label to a variable, we basically assign storage space and rules for it as well as establishing an easy way for us to access it.  If we programmatically say myolddog = “Skippy” we assign the name of our mutt to the variable myolddog.  Any time we say to print myolddog [print(myolddog)] in immediate mode, Python will reply: Skippy. If Skippy runs away and you get a dog named “Rover” you might change the value of myolddog with the statement myolddog=”Rover”, Python forgets about Skippy and when asked will reply: Rover.

If this is all new to you, you should stop now and try it for yourself in immediate mode.  Bring up Python (don’t run IDLE – IDLE is a good beginner’s environment for writing code but it is not the same as calling up the Python interpreter in real time).  If you’ve gotten to the right place, regardless of whether you are on a Linux computer or a Windows machine, your screen will look something like this:

 

Now don’t confuse this with either the Linux or Windows command line – they look similar.

Type at the >>> prompt: Myolddog = “Skippy”
Type >>>   Myolddog      #  and the computer responds
‘Skippy’

Now change it and see what happens. Cool!

Python assigns value types on the fly based on the data you initially store in the variable. In immediate mode, assign two variables like this:

Str_mythree=’3’      #Note the single quotes around the number, making it a string, not a numeric value. Num_mythree=3    #Now this is a numeric value, initially it is an integer because there is no decimal part.

Let’s test these variables:

print(num_Mythree/2)    #and you will get

0.6666666666666666    #good, just what we want, now try

print(str_Mythree/2)        #and you will get this 3-line error message

Traceback (most recent call last):
    File “<stdin>”, line 1, in <module>
TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’

This message is telling you that when you tried to use the divide command (“/”) Python looked at the type of variables involved and couldn’t figure out how to do what you wanted. Python is not smart enough to look at str_Mythree and figure out that it can be interpreted as a number and that probably you would like for it to recognize the string “3” as a number since it is followed by a numeric operation. It does not have what is called contextual inference.  Together, however, you and Python can overcome this obstacle.  You just have to realize your string is going to bomb and tell Python to convert it to a number.  To do this, you use the handy “int” command and presto-changeo Python will convert your string variable to an integer.

Enter:

>>>print(int(str_Mythree)/2)    #and you get the numerical answer

See the “String Handling” and Number Handling” sections in Big Daddy’s Python Toolbox under Major Built-In Functions, for lots of cool and powerful things you can do to manipulate different kinds of variables.