W3: Data Types, Variables, Arithmetic Operators

Data Types







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

Discussion 1

D1. What is a "data type"? Can the data type of an object change?

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?

TypeExampleWhat does it store?What can we do with it (functions, operations...)?How do we convert to it?
"Hello"
123
3.1415
True

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.

Exercise 2

E2. Evaluate the following

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?

Discussion 4: Operator Overloading

Operator Overloading Example

D4. What is “operator overloading”? What is the difference between using + with numerical types and strings/sequences?

Variables

Discussion 5

D5. What is a “variable”? How do we use variables and why are they helpful?

Exercise 3

E3. Evaluate the following given the assignments a = 1, b = 2, c = 2.0:

Exercise 4

E4. What is the output of the following? Why?

Extra Questions

Extra Question 6: input()

EQ6. How does the input() function work?

Extra Question 7: Literals

EQ7. What is a literal?

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

Problems

Problem 1

Problem 2

Problem 3