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 valuesa = 5b = 6#display messages showing the sum and productprint("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.

 #set a variable to hold first namefirstname = "Brian"#set a variable to 5x = 5#print the product of the two variablesprint(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 objectsobject1 = 4object2 = 1 #display the cost of each itemprint("Object 1 costs " + str(object1))print("Object 2 costs",str(object2))#display the total cost of both itemsprint("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 costtotal_cost = 42.76#variables for different tip amountstip12 = total_cost * 0.12tip15 = total_cost * 0.15tip18 = total_cost * 0.18#print statements with each tip amountprint("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