Data Types
flowchart TB
A["Python Data Types"]-->B["Numeric"]
B-->Ba["int"]:::highlight
B-->Bb["float"]:::highlight
A-->C["Sequences"]
C-->Ca["string"]:::highlight
C-->Cb["list"]
C-->Cc["tuple"]
A-->D["dict"]
A-->E["set"]
A-->F["bool"]:::highlight
A-->G["..."]
classDef highlight fill:#eeffcc
Above are some data types that we'll be learning in COMP10001. Highlighted are what we've learnt from the lectures so far. Python offers many more built-in data types that can be explored here.
String Operations, Functions and Methods
# print("Hello")
# input("Please enter some input: ")
# print(len("Hello"))
# print("Hello"[1:3]) # string slicing
# print("Hello"[4]) # string indexing
# print("HELLO".lower()) # .lower() method
# print("hello".upper()) # .upper() method
# print(type(123))
# print(1 + 1) #quickmaths
# print(1 // 2) # integer division rounds down
# print(type(3.1415))
# print(1 + 1.0) # int + float = float
# print(1 / 1) # int / int = float (division creates floats)
# print(type(True))
# print(bool("")) # empty string
# print(bool(" ")) # non-empty string
# print(bool())
Discussion 1
D1. What is a "data type"? Can the data type of an object change?
You can type your answer to D1 here...
Discussion 2
D2: As a class, fill in the below table with the data types we have studied so far. What is the difference between the second and third type, both being numerical?
Type | Example | What does it store? | What can we do with it (functions, operations...)? | How do we convert to it? |
---|---|---|---|---|
"Hello" | ||||
123 | ||||
3.1415 | ||||
True |
You can type your answer to D2 here...
Exercise 1
E1. Look at the following customer data form, and decide which data types (str, int, float, or bool) should be used to store each field.
You can type your answer to E1 here...
- Name:
- Customer ID:
- Address:
- Postcode:
- Do you own or rent?:
- Length of bench top:
- Width of bench top:
- Are you interested in further offers?:
Exercise 2
E2. Evaluate the following
You can type your answer to E2 here...
1. `str(3 + 4) + "cakes"`
2. `int(5/2)`
3. `float("357"+ "."+ "23")`
4. `bool("anything")`
Arithmetic Operators
Discussion 3: Operators
Arithmetic Operators
We'll be learning more about Operators in in Week 4
- Arithmetic Operators (from Highest to Lowest precedence)
()
brackets**
exponent%
modulus/remainder//
floor/integer division/
division*
multiplication-
subtraction (also a unary operator; make number negative)+
addition (also a unary operator; make number positive)
D3. What is an “operator”? Which operators have we learned so far and what do they do?
You can type your answer to D3 here...
Discussion 4: Operator Overloading
Operator Overloading Example
# Example of Operator Overloading
print(1 + 1) # int + int
print("1" + "1") # str + str
# print("1" + 1) # str + int -- what do you think this will do? 🤔
D4. What is “operator overloading”? What is the difference between using + with numerical types and strings/sequences?
You can type your answer to D3 here...
Variables
Discussion 5
D5. What is a “variable”? How do we use variables and why are they helpful?
You can type your answer to D5 here...
Exercise 3
E3. Evaluate the following given the assignments a = 1, b = 2, c = 2.0:
1. `a / a`
2. `b + b`
3. `b + c`
4. `a / b`
5. `a // b`
6. `a % b`
7. `a + b / c`
8. `(a + b) / c`
Exercise 4
E4. What is the output of the following? Why?
You can type your answer to E4 here...
1. `123 + 123`
2. `"123" + "123"`
3. `"123" + 123`
4. `3 * 4`
5. `"3" * 4`
6. `"3" * "4"`
Extra Questions
Extra Question 6: input()
EQ6. How does the input()
function work?
You can type your answer to EQ6 here...
Extra Question 7: Literals
EQ7. What is a literal?
You can type your answer to EQ7 here...
Extra Question 8: Floating Point Error
EQ8. Why does 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1 equal 0.9999999999999999 and not 1.0?
Floating Point Error Code Example
# Discussion 8: Floating Point Error
# print(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)
print(1.2 - 1.0) # what do you expect the answer to be?
# print(((1.2 * 10) - (1.0 * 10)) / 10) # Solution 1: turn the floats to whole numbers
You can type your answer to EQ8 here...
Problems
Problem 1
"""
P1: Write a program which asks the user for their age and calculates the year in
which they were born. There will be two possibilities since you haven’t asked
for their birth date, so print both.
"""
Problem 2
"""
P2: Write a program which asks the user for two integers and multiplies them
together, printing the equation in the form 1 * 2 = 2 for the case of 1 and 2.
"""
Problem 3
"""
P3: Write a program which asks the user for a temperature in degrees Fahrenheit and
prints the corresponding value in Celsius. The conversion formula is below:
C = (F - 32) / 1.8
"""