1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 1. Variables IOs and Data Types


Lesson Objective

  • Learn how to display information on the screen and understand what a variable is.
  • Understand the different data types and how they can be manipulated using type casting methods and concatenation.

Lesson Notes

print

To display information to the user, you must use the "print" function.

Code:

print (“Hello, welcome to your first Python lesson.”)

Cmd Output:

Hello, welcome to your first Python lesson.

Variables

Variables are storage location in your code that store data. They are assigned "tags" and are referred to throughout a program.

The example below stores that phrase "This is cool" in the variable called "cat". The "print" function is then used to display the data that is stored in the cat variable. The variable can be called anything, you just have to type it out exactly the same everywhere in your code.

Code:

cat = “This is cool”
print (cat)

Cmd Output:

This is cool

Input

To get the user to enter data into your program, you would use the "input" function. You combine the input function with a variable and then create a prompt that will be displayed to the user.

Code:

cat = input(“Enter your name: ”)
print (“Your name is:”)
print (cat)

Cmd Output:

Enter your name: Mr Ahmed
Your name is:
Mr Ahmed

Constant

A constant is like a variable. It stores data, but the data does not change.

Code:

user = "Mr Ahmed"
miles = input("How far did you run today? ")
print (user)
print ("You ran")
print (miles)
print ("Miles today!!, Well Done!!")

Cmd Output:

How far did you run today? 45
Mr Ahmed
You ran
45
Miles today!!, Well Done!!

Data Types

Data types dictate how you store, structure and manipulate data. Here are some common data types:


Strings

Code:

name = input(“Enter your name: ”)
num = input(“Enter your student number: ”)
print (“Your name is ” + name)
print (“Your student number is ” + num)
#Input function automatically collects string data types.
#Adding 2 strings together joins them. This process is called “Concatenation”.

Cmd Output:

Enter your name: Mr Ahmed
Enter your student number: 564934
Your name is Mr Ahmed
Your student number is 564934

Integers

Is this answer correct???

Code:

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
answer = num1 + num2
print ("The sum of the numberses is...")
print (answer)

Cmd Output:

Enter your first number: 5
Enter your second number: 5
The sum of the numberses is...
55

Type Casting

The “int” function has been used to turn the string data type into an integer data type.

Code:

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
answer = int(num1) + int(num2)
print ("The sum of the numberses is...")
print (answer)

Cmd Output:

Enter your first number: 5
Enter your second number: 5
The sum of the numberses is...
10

Let's try to reduce the lines of code.

Code:

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
answer = int(num1) + int(num2)
print ("The sum of the numberses is..." + answer)

Cmd Output:

Enter your first number: 5
Enter your second number: 5
Traceback (most recent call last):
   File “C:\mra\desktop\typecast_Lesson.py”, line 4, in module
      print ("The sum of the numberses is..." + answer)
TypeError: can only concatenate str (not “int”) to str

The “str” function has been used to turn the integer data type into a string data type.

Code:

num1 = input("Enter your first number: ")
num2 = input("Enter your second number: ")
answer = int(num1) + int(num2)
print ("The sum of the numberses is..." + str(answer))

Cmd Output:

Enter your first number: 5
Enter your second number: 5
The sum of the numberses is... 10

Real (Float)

The “float” function has been used to turn the string data type into a real/float data type.

Code:

cost1 = input("Enter the cost of the first item: ")
cost2 = input("Enter the cost of the second item: ")
total = float(cost1) + float(cost2)
print ("The total cost is... " + str(total))

Cmd Output:

Enter the cost of the first item: 12.15
Enter the cost of the second item: 23.67
The total cost is... 36.17

Boolean

The “float” function has been used to turn the string data type into a real/float data type.

Code:

total = 0
loop = True
while loop == True:
    cost = input("Enter the cost of your item: ")
    total = total + float(cost)
    a = input("Would you like to enter more items? 'y' or 'n': ")
    if a == "n":
        loop = False
print ("The total cost is... " + str(total))

Cmd Output:

Enter the cost of your item: 10.50
Would you like to enter more items? 'y' or 'n': y
Enter the cost of your item: 5.50
Would you like to enter more items? 'y' or 'n': y
Enter the cost of your item: 2.20
Would you like to enter more items? 'y' or 'n': n
The total cost is... 18.2

3