INF1-IP: Week 2
Lecture 2 - Strings and numbers
- Lecture slides
- Self-guided notebook: see "Files" at the bottom of this page.
Learning outcomes
- Distinguishing
int,float, andstrvalues and inspecting them withtype() - Converting user input with
int(),float(), andstr() - Using floor division, remainder, exponentiation, and compound assignment
- Combining strings and producing readable output with f-strings
- Diagnosing common type and conversion errors
Key terms
Click to expand.
Type
The label assigned to values denoting what kind of values they are. Different types have different functionalities attached to them.
Type conversion
Changing a value from one type to another.
Integer
A type of value that defines a whole number.
Float
A type of value that defines a number with fractional parts.
String
A type of value that defines text.
Operator
A symbol that performs an action on one or more values (its operands). May perform different actions depending on its operands' types.
Operand
The values that an operator acts on (e.g. the '2' and '3' in '2 + 3').
Compound assignment
A single operation that changes a value and updates the name that pointed to the old value to the new one.
String concatenation
Merging two strings together.
String interpolation
Evaluating variables and expressions directly inside of a formatted string.
New syntax
Click to expand.
type()Returns what type a value is in the format
<class 'type'>.int(),float(), andstr()Functions to convert a value to an integer, float, and string, respectively. Only work if their arguments actually can be converted to the new type.
+(on strings)Concatenates strings.
*(on a string and an integer n)Repeats the string n times.
//Floor division: divides and rounds down to the nearest integer.
%Remainder: returns an integer of how much is 'left over' after floor division.
**Exponentiation: raises a number to a power.
+=,-=,*=,/=,//=,%=,**=Compound assignment operators: shorthands that change the value by a set amount and update the name to point to the new value in one go.
.upper()and.lower()Transform a string to uppercase or lowercase characters, respectively.
.replace(old, new)Searches a string and replaces the substring 'old' with 'new'.
f'...'andf"..."Denotes a formatted string (or f-string). Inside the f-string, variables may be interpolated using
{...}and formatted using modifiers.TypeErrorA type of error that highlights when a function expected a value of a particular type, but received the wrong one.
ValueErrorA type of error that highlights when a value mismatches its assigned type - for example, when attempting to convert a value to an incompatible type.
Further practice
TBA.
Lecture 3 - Conditions and logic
- Lecture slides: TBA.
- Self-guided notebook: see "Files" at the bottom of this page.
Learning outcomes
- Constructing comparisons with
==,!=,<,<=,>, and>= - Combining Boolean values with
and,or, andnot - Selecting one or more paths with
if,elif, andelse - Using indentation to show which statements belong to a branch
- Recognising reserved keywords in Python
Key terms
Click to expand.
Boolean
A type of value that is either true or false.
Indentation
Whitespace at the beginning of lines of code that, in Python, groups pieces of code together.
Boundary test
When writing an if statement using numbers, a test that checks the statement by using values that are on the boundaries of the conditions.
Character encoding
The numerical representation of characters that computers use to process them, for example when comparing strings.
Reserved keyword
A word with special functionality in Python that cannot be used as a variable name.
New syntax
Click to expand.
TrueandFalseBoolean values in Python. Remember to capitalise them!
boolandbool()The Boolean type in Python and the function to convert a value to the
booltype in Python, respectively.==and!=Equal to and not equal to comparison operators. Works across types.
<,<=,>,>=Less than; less than or equal to; greater than; and greater than or equal to comparison operators. Only work when their operands are of similar types.
and,or,notLogic operators that combine Boolean expressions.
if,elif,elseKeywords that define if statements and their branches. Remember that for each branch, the head ends with a colon and the body is indented.
SyntaxErrorA type of error that highlights when you break the rules of Python, such as naming a variable after a reserved keyword or attempting to assign a variable as part of a condition (by using
=instead of==, for example).IndentationErrorA type of error that highlights when you misuse indentation.
Further practice
TBA.
Tutorial
Tutorial 1: The Tools of the Trade has you install Python and VS Code on your own laptop so you can start writing Python files outside of Jupyter notebooks.
See "Files" at the bottom of this page for the interactive notebook.
Key terms
Click to expand.
Python interpreter
The program that reads and executes Python code.
Python source file
A text file containing Python code, normally with a
.pyfilename extension.Code editor
A program for creating and editing source files, such as IDLE or VS Code.
Extension
An add-on that gives an editor extra features, such as Python support in VS Code.
Terminal
A window or panel for text input and output; it commonly hosts a command shell, and can be both its own app or a part of another app (such as VS Code).
Command shell
A program that interprets commands to the operating system, such as starting other programs. Examples include PowerShell (on Windows), zsh (on macOS), or bash (on Linux).
Python interactive shell
An interface, recognised by the
>>>prompt, for sending commands directly to the Python interpreter. Opened by runningpython(on Windows) orpython3in a command shell, or directly by opening the Python interpreter program or the IDLE Shell.REPL
The Read–Evaluate–Print Loop followed by an interactive shell.
Assessment
No assessment this week.