Intro
Now that we have our Python environment set up lets discuss some basic coding concepts. In part 2 of this series we discussed the sections of code but let’s do a quick review and add on a few additional concepts.
The first thing we typically start with are comments about our program’s purpose and other useful information. Next, we import the necessary modules or libraries we need to complete our program. After that, you can optionally include what are known as Global Variables. These are variables that we will declare to use throughout our classes, functions, and main part of our program. Then, we declare our classes and function that will be used throughout the main part of the program. Finally, we write the code that will function as our driver or main part of our program.
So let’s see a few examples of what this might look like. First, lets declare some variables see below for an example of integers and strings.
On the left side of the equals sign we use a unique descriptive name for the integer and then we set that variable name to a value by using the equals sign followed by the value. Above we set int1 and int2 to an integer and str1 and str2 to some string values. There are other data types such as floats, lists, tuples, dictionaries, and boolean values to name a few. A good reference for data types can be found at https://www.w3schools.com/python/python_datatypes.asp.
Lets see what we can do with some of these variables when we combine it with the print statement we have previously used. The complete main part of our program will now look like this:
Starting this program will then result in the following in a python terminal.
Notice when we use the ‘+’ operator inside of aprint statement with two strings it will combine those two strings on the same line to form a longer string. However when we use the same ‘+’ operator in a print statement with two integers python adds those two integer values together. Now lets simplify the above statement to put the resulting value of the two integers on the same line as ‘Adding 7 + 9 together equals: ‘.
Using the ‘+’ operator again we typecast the two integer values by placing them inside a set of parenthesis of their own and prepend that with the typecast value str to indicate that we want this to be a string value. We need to do this to avoid a concatenation error that could occur by trying to combine both a string value of ‘Adding 7 + 9 …’ and an integer value when we add the two integer variables int1 and int2. Now we are able to combine the last two print statements onto a single line making our program output a little cleaner. See more about type casting and variable types here https://www.w3schools.com/python/python_variables.asp and more about the operators available in Python here https://www.w3schools.com/python/python_operators.asp.
Let’s look at a custom function that will help us develop cleaner code when we start develop more complex programs. Functions are prebuilt blocks of code that we use repeatedly throughout a program. To declare a function we use a def statement followed by the name of our function and any values that we want to pass in inside of some parenthesis.
Now that we have declared our function lets use it to print to the screen our previous statements. To call a function we use the name of the function and then pass in any values we need for that function.
This example demonstrates an important concept that we can use functions to repeatedly perform a task. This example might not seem useful but as we develop more complex programs you will understand the power of functions. Here is the resulting output from calling these functions and passing in some values (parameters).
In part 4 we will develop a more complex program and continue to build upon or coding skills introducing loops, if-then statements, and boolean operators.