Lecture 2: Python basics – Data Types, Product, and Difference

In this section, we’ll take a look at the main data types you would typically work with in the field of Computer science. Open a Repl instance and type the following:

print("This program calculates the sum of two numbers:")
num1 = 5
num2 = 7

print("The first number is: ", num1)
print("The second number is: ", num2)
print("The sum is: ", num1 + num2)

Let’s break this code down for a bit, first we print a statement to the user prompting them that the program calculates the sum of two numbers:

print("This program calculates the sum of two numbers:")

Then we create a variable called num1 with the number 5 num2 and assign thenumber 7:

num1 = 5
num2 = 7

Next, we’ll print the values that were stored in num1 and num2 to the user.

print("The first number is: ", num1)
print("The second number is: ", num2)

To wrap things up, we take the value in num1 and num2 and find the product of them with the + symbol passing in the arguments through the print function:

print("The sum is: ", num1 + num2)

We’ll also print out the product that was calculated using the .format() function, passing in the sum as an argument:

Programming challenge: to gain a further understanding of how this works in Python, let’s calculate the difference between the two numbers here. Replace the # comments with your code to run. (Hint: Subtract num2 from num1 to find the difference):

print("This program calculates the sum and difference of two numbers:")
num1 = 5
num2 = 7

print("The first number is: ", num1)
print("The second number is: ", num2)
print("The sum is: ", num1 + num2)
# Write your difference code here

Answer:

print("This program calculates the sum and difference of two numbers:")
num1 = 5
num2 = 7

print("The first number is: ", num1)
print("The second number is: ", num2)
print("The sum is: ", num1 + num2)
print("The difference is: ", num2 - num1)

Output:

The first number is 5
The second number is 7
The sum is 12
The difference is 2

Let’s break down the new lines of code we just added, First we subtracted num2 from num1 and stored it in a variable called difference:

difference = num2 - num1

Next, we printed the difference to the user using the format function, passing in the difference variable as the argument:

print("The difference is: {}".format(difference))

Now let’s Calculate the average:

print("This program calculates the sum and difference of two numbers:")
num1 = 5
num2 = 7
# Add your average code here

print("The first number is: ", num1)
print("The second number is: ", num2)
# TODO
# Print your average code here

Let’s put a little bit of thought into this. In order to calculate the average, we have to add all the numbers together and divide by the amount of numbers in the group. Therefore, num1(5) + num2(7) = 12, Next we have the divide by the occurrence of numbers in the set which is 2, therefore 12 divided by 2 equals 6 which should be our final answer. Let’s see how we can accomplish this in Python.

print("This program calculates the sum and difference of two numbers:")
num1 = 5
num2 = 7

sum = num1 + num2
difference = num2 - num1
average = (num1 + num2) / 2

print("The first number is: ", num1)
print("The second number is: ", num2)
#TODO

Dissecting this code we add a variable called average from there we add the two values inside num1 and num2; The parenthesis tells python to evaluate the argument first and then we divide the value inside the parenthesis by the occurrence of numbers which is 2. We then store this value in the variable that we created called average.

average = (num1 + num2) / 2

Next, we print the average to the user:

print ("The average is: {}".format(average))

Our output should look something like this:

The first number is 5
The second number is 7
The sum is 12
The difference is 2
The average is 6

Congratulations on writing your beginner Python program!