In Python we can use a simple print command. Print is actually an inbuilt function within Python, more about functions later on in this section of the website. Print doesn't mean printing your content to the printer, it means display on the screen, but obviously print is much shorter.
# Hello World print("Hello World")
# Displays the message on screen and stores the input from the user in a variable called name name = input("What is your name? ") #Displays Hello and calls the variable name print("Hello",name)
The code for the program below will allow the user to enter various pieces of information and store them in different variables. The print() function is then used to output all of the information.
# Program to store user data number = input("Enter your house number: ") street = input("Enter your street name: ") town = input("Enter your town/city: ") county = input("Enter your county: ") postcode = input("Enter your postcode: ") print("\nAddress Details:\n" + "Street: " + number + " " + street + "\nTown/City: " + town + "\nCounty: " + county + "\nPostcode: " + postcode)
You can concatenate (join together) variables with strings in a print() function.
In the address example:
print("Street: " + number + " " + street + "\nTown/City: " + town)
will combine the strings “Street” and “Town/City” with the variables number, street and town\n is used to start a new line when it is displayed on screen.
A variable is used to temporarily store a piece of data.
number = 10
In the code above the variable is called (number) and the value it is storing is 10. Variables can hold any type of data. Using variables makes it easier for people to understand what is going on.
cost = 15 VAT = 3 total_cost = cost + VAT
Python will automatically decide what type of data a variable should be, sometimes this isn’t right, therefore you will have to convert/cast variables to a different data type.
The program below will multiply a number by 5. When data is input from the user it will store it as a string. You will need to convert the variable number to an integer before performing a calculation. An example of how you do this is shown below:
number = input("Enter a whole number ") answer = int(number) * 5 #converts the variable number to an integer and multiplies it by 5. print(answer)
Another method of performing the operation above is to convert the users input at the point of entry as shown below.
number = int(input("Enter a whole number ")) answer = number * 5 #calls the integer and multiplies it by 5. print(answer)
The program below will ask the user to enter their weight in kilograms (this could be a decimal) and convert it to pounds. You will need to convert the variable kg to a float before converting it. An example of how you do this is shown below:
kg = input("Enter the weight in KG ") pounds = float(kg) * 2.2 print(pounds)
Using the program above, if you wanted to improve the print message so that it said Your weight in pounds is X you would need to convert the variable pounds to a string as it is currently a float.
print("Your weight in pounds is " + pounds)
The code above won't work correctly and will cause an error. We need to change the code so that we can convert the data stored as a float to a string.
print("Your weight in pounds is " + str(pounds))