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.
KS3, GCSE, A-Level Computing Resources
To display information to the user, you must use the "print" function.
print (“Hello, welcome to your first Python lesson.”)
Hello, welcome to your first Python lesson.
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.
cat = “This is cool” print (cat)
This is cool
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.
cat = input(“Enter your name: ”) print (“Your name is:”) print (cat)
Enter your name: Mr Ahmed Your name is: Mr Ahmed
A constant is like a variable. It stores data, but the data does not change.
user = "Mr Ahmed" miles = input("How far did you run today? ") print (user) print ("You ran") print (miles) print ("Miles today!!, Well Done!!")
How far did you run today? 45 Mr Ahmed You ran 45 Miles today!!, Well Done!!
Data types dictate how you store, structure and manipulate data. Here are some common data types:
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”.
Enter your name: Mr Ahmed Enter your student number: 564934 Your name is Mr Ahmed Your student number is 564934
Is this answer correct???
num1 = input("Enter your first number: ") num2 = input("Enter your second number: ") answer = num1 + num2 print ("The sum of the numberses is...") print (answer)
Enter your first number: 5 Enter your second number: 5 The sum of the numberses is... 55
The “int” function has been used to turn the string data type into an integer data type.
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)
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.
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)
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.
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))
Enter your first number: 5 Enter your second number: 5 The sum of the numberses is... 10
The “float” function has been used to turn the string data type into a real/float data type.
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))
Enter the cost of the first item: 12.15 Enter the cost of the second item: 23.67 The total cost is... 36.17
The “float” function has been used to turn the string data type into a real/float data type.
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))
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