ENGG1330
Computer Programming I
49% selected one of the two beginner options
“Excited” was the most common response
A guaranteed B · No work · Nothing learned
86% chose “No”
Approximate figures reported by Kevin
The survey is only one part: remember the coding challenges too
ENGG1330
Computer Programming 1
Terminology
We combine literals and variables with operators
to form expressions
Terminology
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)Terminology
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)Terminology
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)Terminology
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)Terminology
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)Terminology
●Literals
○Raw data in code
●Variables
○Store data in memory
●Operators
○Special symbols that carry out operations
●Expressions
○Combine literals and variables to create new values
ENGG1330
Computer Programming 1
pi = 3.14159diameter = 3area = pi * ( ( diameter / 2) ** 2 )print(area)33.14159'Bruce Lee'TrueENGG1330
Computer Programming 1
Variables Are Used to Store Data in Memory
a = 3b = 3.14159c = 'Bruce Lee'd = TrueVariables Have Unique Names (Identifiers)
a = 3b = 3.14159c = 'Bruce Lee'd = TrueVariables Have a Value
a = 3b = 3.14159c = 'Bruce Lee'd = TrueVariables Have a Type
a = 3b = 3.14159c = 'Bruce Lee'd = Truea = 3b = 3.14159c = 'Bruce Lee'd = Trueprint(type(a))print(type(b))print(type(c))print(type(d))print(type(3))print(type(3.14159))print(type('Bruce Lee'))print(type(True))age = 18print(type(age)) # intage = "18"print(type(age)) # str: int documents the intended type
int(...) converts a value
Python does not enforce type hints at runtime
age: int = 18print(type(age))age = 18print('You are', age)age = age + 1print('You will be', age, 'soon')d = 7
elapsedTimeInDays = 7
Choose meaningful names that describe the value
elapsed_days = 7student_count = 120total_cost = 85.50Start with a letter or underscore
Then use letters, digits or underscores
| Invalid | Reason | Valid Alternative |
|---|---|---|
2nd_score | Starts with a digit | second_score |
class | Reserved keyword | class_name |
total-cost | Contains a hyphen | total_cost |
Names are case sensitiveage and Age are different names
X = 'awesome'print('Python is',x)x = 'awesome'print('Python is',x)| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ | Addition | Adds the operands | 1 + 1 | 2 |
- | Subtraction | Subtracts the right operand | 5 - 4 | 1 |
* | Multiplication | Multiplies the operands | 3 * 7 | 21 |
/ | Division | Divides the left by the right | 3 / 6 | 0.5 |
% | Modulo | Returns the division remainder | 7 % 3 | 1 |
** | Exponent | Raises the left to a power | 7 ** 3 | 343 |
// | Floor Division | Rounds the quotient down | 7 // 3 | 2 |
numCookiesToday = 3numCookiesYesterday = 4print('You had',numCookiesYesterday + numCookiesToday,'total!')a = 42b = 0print(a/b)7 objects, groups of 3
1 left over
7 objects, groups of 3
2 complete groups
print(7 // 3)a = 42b = 0print(a%b)| Numbers | Examples | Test |
|---|---|---|
| Even | 2, 4, 6, 8, … | x % 2 == 0 |
| Odd | 1, 3, 5, 7, … | x % 2 == 1 |
a = 5print(a)a = 5a += 5print(a)L-2 Survey
Complete the full survey during the lecture
x = 42xMod2 = x % 2even = xMod2 == 0print(x,'is even:',even)x = 43xMod2 = x % 2even = xMod2 == 0print(x,'is even:',even)x = 42xMod2 = x % 2even = xMod2 == 0if even: print(x,'is even:',even)x = 43xMod2 = x % 2even = xMod2 == 0if even: print(x,'is even:',even)x = Trueif (x): print('yes')else: print('no')x = Falseif (x): print('yes')else: print('no')x = Trueif (x): print('yes')else: print('no')← A block of code
← Another block of code
x = 42xMod2 = x % 2if (xMod2 == 0): print(x,'is even')else: print(x,'is odd')x = 43xMod2 = x % 2if (xMod2 == 0): print(x,'is even')else: print(x,'is odd')age = 17if (age >= 18): print('You are an adult')else: print('You are under age')age = 36if (age >= 18): print('You are an adult')else: print('You are under age')age = 17if (age >= 18): print('You are an adult')else: print('You are under age')Advanced Example
x = 42xMod2 = x % 2if (xMod2 == 0): print(x,'is even')else: print(x,'is odd')x = 42if ((x % 2) == 0): print(x,'is even')else: print(x,'is odd')Expressions - Example
Expressions - Example
No, Bye bye
height >= 180cm?
True
False
True
False
Yes, I do
age = 25height = 179if (((age >= 18) and (age <= 25)) and height >= 180): print('Yes, I do')else: print('No, Bye bye')print(2 + 3 * 4)print((2 + 3) * 4)Highest to Lowest
*** / // %+ -notandorParentheses make the intended grouping explicit
55age = input('age: ')if (age >= 18): print('You are an adult')else: print('You are under age')age = input('age: ')if (age >= 18): print('You are an adult')else: print('You are under age')| Conversion | Resulting Type |
|---|---|
int(x) | Integer |
str(x) | String |
float(x) | Float |
age = input('age: ')print(type(age))age = int(age)print(type(age))if (age >= 18): print('You are an adult')else: print('You are under age')Python uses zero-based indexing
| Expression | Character |
|---|---|
s[0] | First |
s[1] | Second |
s[-1] | Last |
s[-2] | Second last |
Keep the index within the string’s bounds
name = input('name: ')index = int(input('index: '))print(name[index])Coding Challenge
To Earn Your Participation Mark
Both required · 1 mark · No partial credit