In this ICS3U Grade 11 Computer Science lesson you will be learning how to:
Python Data Types
Computer programs work by manipulating data through user input or mathematical calculations
There are different types of data that we can get from the keyboard or use in our programs. In ICS3U we are concerned about
During a programs operation data must be stored and manipulated in the computers memory
You can’t access the computer memory locations directly, so you need to define a variable to represent that data location.
A variable in computer programming is exactly the same as a variable in math. Its a letter or a word that is used to represent a piece of data
You should follow the following naming conventions in ICS3U when using a variable in a program.
Variable names are case sensitive. The following variables are considered different
To assign a value to a variable you start with the variable name, then use an equal sign, followed by the value you wish to assign
The only thing that can go on the left side of an equal sign is a variable. Assignment statements go from Right to Left. That is, the value on the right side of the equal sign gets put into the variable on the left side of the equal sign
number = 27
interestRate = 4.75
finished = True
notFinished = False
first_Name = "Mickey"
lastName = 'Mouse'
After this code is executed those values are stored in the computer’s memory and that data can be accessed again by referring to the variable name at another point in the program
Printing the Value of Variables in Python
Just like in the last lesson you can print the value that is stored in a variables memory location to the screen using the print statement. Just don’t write the double quotes around the variable name
number = 27
interestRate = 4.75
finished = True
notFinished = False
first_Name = "Mickey"
lastName = 'Mouse'
print(number)
print(interestRate)
print(finished)
print(notFinished)
print(first_Name)
print(lastName)
You can also combine printing messages and the value of variables at the same time. You have to separate them using a comma
number = 27
interestRate = 4.75
notFinished = False
first_Name = "Mickey"
lastName = 'Mouse'
print("The number is ", number)
print("My first name is", first_Name, "and my last name is", lastName)
print("The interest rate on my car loan is", interestRate, "%")
Watch a video of your teacher summarizing the learning content for this section
Python Input Statements
Most computer programs don’t just define data in the program itself, there is often interaction between the program and the user. This can be accomplished by having the user type in values and the program will store that value in memory and eventually do something with it.
This is accomplished using an input statement in python
input()
When the program encounters an input statement, it will wait for the user to enter a value and hit Enter. Once you hit enter the value will be stored in the variable to be used at a later time in the program. You could then use that information somewhere else in your program.
Here is a program that is trying to get the user to enter their favorite color from keyboard and print it back to them
Type the following code into the Python Editor.
favColor = input()
print("The users favorite color is:", favColor)
The program you entered above, wasn’t very user friendly. You should always indicate to the user what you want them to enter with a prompt
Type the following code into the Python Editor.
favColor = input("Please Enter your favorite color: ")
print("The users favorite color is:", favColor)
Converting Strings to Numbers using Python
All data is read into the keyboard as a String value by default. This doesn’t matter if you are just going to display the data, but imagine if you wanted a program that added two numbers together. Python can’t add two words together even though the words might be just “2” and “3”. If you want to input integers or floats you have to convert them from Strings first
You can read in an integer from the keyboard by surrounding the input statement with an int statement.
int()
You can read in a decimal from the keyboard by surrounding the input statement with a float statement
float()
Typically I prefer to do the conversion from String immediately when reading in the data from the keyboard, like in the following example. The input statement gets executed first because its inside the brackets of the int( ) / float( ) and then the conversion gets done and stored in the variable.
wholeNumber = int(input("Please Enter an Integer: "))
decimalNumber = float(input("Please Enter a Decimal: "))
You won’t notice anything different when running this program, but having the data in the correct data type will allow you to manipulate it properly later on in the program
When converting data types the user needs to be aware of what type of number they should be entering.
Type the following code into the Python Editor.
myNumber = int(input("Please Enter an Integer: "))
You will see that the program will generate a runtime error and indicate in someway that it is unable to do the conversion. If this were a professional app, then you would need some way to deal with that without the program crashing, but for our course, you can assume the user will always enter the correct type.
Type the following code into the Python Editor.
cost = float(input("Please Enter the cost of your purchase: "))
print(cost)
You will see that the program did NOT generate an error this time. An integer value can be converted to a floating point number, just not the other way around. It makes sense, as the number printed is just 20.0
Watch a video of your teacher summarizing the learning content for this section
Try to code the following questions using an IDE of your choice (Spyder, IDLE, Pycharm, etc). You will save those files on your computer for future reference.
Each question has:
Try your best to solve the problems yourself without looking at the solutions.
Name Your File: “ICS3UwishList.py”
Write a program that asks the user for the following and store in an appropriate variable
Print the information on the screen in a nicely formatted Birthday Wish List that they could give to their parents.
fName = input("Enter your first name: ")
age = int(input("How old will you be? "))
item1 = input("Enter item: ")
item1Cost = float(input("How much does it cost? "))
item2 = input("Enter item: ")
item2Cost = float(input("How much does it cost? "))
item3 = input("Enter item: ")
item3Cost = float(input("How much does it cost? "))
totalCost = float(input("What was the total cost of everything? "))
print("\n\t\t",fName,"'s", age, "th Birthday List\n")
print("1st item: ", item1, "= $",item1Cost)
print("2nd item: ", item2, "= $",item2Cost)
print("3rd item: ", item3, "= $",item3Cost)
print("\nIt will cost a total of $", totalCost)
Name Your File: “ICS3UdateFormat.py”
Write a program that gets the weekday, number, month, and year from the user. Then print out the information in 4 different ways as shown in the picture
weekday = input("Enter the weekday: ")
day = int(input("Enter the day of the month: "))
month = input("Enter the month: ")
year = int(input("Enter the year: "))
print("")
print(weekday,"is the weekday.")
print(weekday, "", month, "", day)
print(month,"",day,",",year)
print(year,"/",day,"/",month)