Now that we understand variables a little bit, let's look at data types attached to variables. We will start with some basic examples and then move to complex applications.

There are many types of data, but this tutorial will look at two types: Strings and Integers.

Strings:

Basic character text. Can include numbers, text, symbols, etc.

Examples:

  • var1 = "Brian"
  • var2 = "Abc123"
  • var3 = "90210"
  • var4 = "Brian?123&654"
  • var5 = " "

These variables can be just about anything. This is used for non-math applications. If you don't intend on using math, strings are often used. I used the popular zip code 90210 as an example because although it is a number, we wouldn't do any mathematical applications to it. Multiplying a zip code by 5 makes no sense.

 

Integers:

Integers are math-based numbers. We use these to add, subtract, divide, multiply, etc. If you plan on doing mathematics with it, use an integer.

Examples:

  • var6 = 45
  • var7 = -99
  • var8 = 1200

We know what integers are from math. Basically, the whole numbers and their negative counterparts. If you incorporate decimals into the mix, Python will consider them and turn the variable type into a float. Again, for this introductory tutorial, let's consider them as integers.

Also note that we dropped the quotations for integers. This is how we distinguish between a zip code ("90210") and a bank account balance ($90,210).

 

Program 2.2.1 - Using Integers

Let's look at a very simple programming finding the sum and product of two variables:

#set two variables to integer values
a = 5
b = 6
#display messages showing the sum and product
print("The sum is")
print(a + b)
print("The product is")
print(a * b)

This program simply takes two variables and gives them a value as an integer. Python knows we want to use math because there are no quotation marks.

 

You can play around with this by changing values. You could add a third variable, etc. We will learn more about Mathematics in Chapter 3.

 

Program 2.2.2 - Multiply an Integer Times a String

Okay, so what happens if we were to combine an integer with a string mathematically? Go ahead and try making a variable with your first name, and then add 5 to it. It should give you an error, but it is worth seeing that error in action.

But what about multiplication?

#set a variable to hold first name
firstname = "Brian"
#set a variable to 5
x = 5
#print the product of the two variables
print(firstname * x)

Maybe this will give us an error, or maybe Python will understand that we want the firstname to be multiplied by 5? That kinda makes sense...

Ah, it does indeed look like we got our name printed 5 times. If you add a space character inside the firstname variable, this will make it look nice. This is almost a sneak preview at a future chapter involving repetition applied to programming, or loops. More on that in later chapters.

 

Program 2.2.3 - Combining Strings and Integers Into a Print Statement

Let's say you wanted to have Python display the sum of two numbers with some text.

#set two variables for the prices of objects
object1 = 4
object2 = 1

#display the cost of each item
print("Object 1 costs " + str(object1))
print("Object 2 costs",str(object2))
#display the total cost of both items
print("The sum of the objects is " + str(object1 + object2))

I used two different examples in this program. Let's look at the first print statement. By telling Python to str() a variable, I asked it to turn the integer variable into a string. Python too the value 4 and turned it into "4".

The second print statement used the comma to combine two ideas together. This should give a little insight of the plus sign vs the comma. I will leave the decision up to you when deciding which is better to use for now. I will be using the plus sign because it expresses the idea of "combining" things a bit better.

 

 

Program 2.2.4 - Tip Calculator

Okay, now we're going to step a bit outside of our comfort zones and make a simple tip calculating program. This program will calculate a 12%, 15% and 18% tip based on the amount we put into the bill. We will use decimal places in this, but we can pretend we are working with integers.

#variable for dinner cost
total_cost = 42.76
#variables for different tip amounts
tip12 = total_cost * 0.12
tip15 = total_cost * 0.15
tip18 = total_cost * 0.18
#print statements with each tip amount
print("Bad service: $" + str(tip12))
print("Okay service: $" + str(tip15))
print("Great service: $" + str(tip18))

You should be able to follow the math, but I created a variable for each tip amount (12,15,18%) and multiplied the total_cost times the decimal representations of that percent.

 

Although we have some extra decimal places, we got the results we want. We will learn to round and apply other mathematical concepts in Chapter 3.

 

Assignments:

  • Using Program 2.2.4, improve the program by telling the customer the total amount they could be paying with the tips and total_cost for each percentage.
  • Create 5-10 variables for different food items such as eggs, sugar, milk, bread, etc. Add all of the items together into one variable. Also find the tax at any percentage (8.125% is my rate). Then, combine the total costs into a grand total. Display it in Python like so:
    • Eggs cost $2.99
    • Bread costs $1.49
    • Milk costs $3.69
    • etc.
    • (add a blank line to split up the details)
    • The total for all of your groceries is $16.21585
    • Your tax owed is $2.91
    • (add a blank line to split up the details)
    • Your grand total is $16.21585
  • Improve the program above by applying a 10% employee discount only to the products and not the tax. Employees may get discounts, but that does not apply to tax owed. The tax should represent the total cost of the items before the employee discount applies, but should lower the total cost of the groceries by 10%.