What is wrong with this code?

num1 = 65
num2 = input()

output = num1 - num2

This program is supposed to subtract 65 from a number but instead it just crashes. Fix the program so that it now displays the correct result such that if the user put in 75 the program would display 10.

What is wrong with this code?

num1 = 65
num2 = int(input())

output = num2 - num1

This program is supposed to subtract 65 from a number but instead it just crashes. Fix the program so that it now displays the correct result such that if the user put in 75 the program would display 10.

Data types

Learning Intentions

  • Develop our understanding of variables

  • Understand how to use comments within our programs

  • Know the different data types

Success Criteria

  • I can develop my own program using variables
  • I can develop a program that performs checking the data types of variables

Variables in Python

print("Please insert a number")
y = int(input())
z = 2

x = y * z

print(x)

Write a very short three-line program that will ask the user "Please insert your name" then take in a user’s name and stores it in a variable and then display "Hello " and the user’s name.

Step 1: Print to the screen "Please insert your name"
Step 2: Get the user’s input
Step 3: Display the "Hello " and the user’s input

Task

Comments

  • One of the most important things in programming, especially if you choose to do Computing Science in National 4/5 is something known as internal comments.
  • A comment is a piece of text within the program that describes what your code does. Python ignores comments when it is run.
  • In Python, a comment line starts with a hashtag sign #
  • It is important to note that comments only go across one line, so each line after that will need a # at the start too

Using comments

  • We should get used to using comments so that when we look back at our programs, we can understand what each piece of code does without needing to look it up.

#Display to the user Please insert a number
print("Please insert a number")
#Get the number from the user and put it in y
y = int(input())
z = 2

#Do the calculation
x = y * z
#Print the result
print(x)

Data types

  • A data type is the type of the data you are storing in a variable.
    For instance, when we are storing a number using the int(input()) function we are getting an integer.
    Python has 5 data types we need to know about.

     

Why do we need to know about data types?

  • Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the input() function we have been using in the past examples the computer expects text even if we want a number.

  • To convert the text into a number so that we can use it in calculations we use the int() function. This tells the computer that it should now consider that string as an integer.

     

Data type conversion

  • We have multiple ways of converting data that we are going to use for the next part of this lesson:

     

Data types

  • For each of the following pieces of data, write down the data type you would expect the value variable to be:

String

value = "Hello world"

Character

value = "0"

Boolean

value = True

Float

value = 0.5

String

value = "Jack101"

Character

value = "c"

Why do we need to know about data types?

  • Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the input() function we have been using in the past examples the computer expects text even if we want a number.

  • To convert the text into a number so that we can use it in calculations we use the int() function. This tells the computer that it should now consider that string as an integer.

     

Conversion

  • Python has a very useful way of checking the data type of a variable using the type() function.
  • For example:
name = "Jack"
print(type(name))
age = 12
print(type(age))

Your task is to complete the written task on page 7 of the Python booklet. Once you have done that you can go back to your computer and create a program to test these types using the type() function.

Task

JB
6 Software Development : 3.1 Data types
© J Balfour
Tools