INF1-IP: Week 2
Lecture 2 - Strings and numbers
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
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 represents a whole number.
Float
A type of value that represents numbers that can have a fractional part.
String
A type of value that represents 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 the quotient down. With two integer operands the result is an integer; with one or more float operands the result is a float.
%Remainder: returns what is left over after floor division. With two integer operands the result is an integer; with one or more float operands the result is a float.
**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 raised when an operation or function receives a value of an unsuitable type, such as adding a string and an integer together.
ValueErrorA type of error raised when an operation accepts the argument's type but its particular value is unsuitable. For example,
int("4.5")accepts a string argument, so there is no reason for aTypeError, but as the string does not represent a whole number, we run into aValueErrorinstead.
Further practice
- Official Python Tutorial: Numbers and Text.
- W3Schools: Numbers and Strings.
- Helsinki Python MOOC: Arithmetic operations and Print statement formatting.
Lecture 3 - Conditions and logic
- Lecture slides
- Self-guided notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
Learning outcomes
- Constructing comparisons with
==,!=,<,<=,>, and>= - Combining conditions 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.
Lexicographic comparison
Comparing strings from left to right using their code points.
Code point
A number assigned to a character so computers can process it.
Unicode
A common standard for code points, used by almost all computers.
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. Work 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.
andandorLogical operators that combine conditions. Return a Boolean when their operands are Booleans; return one of their operands when they are not.
notLogical operator that reverses a condition. Always returns a Boolean.
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
- Official Python tutorial:
ifstatements. - W3Schools: Booleans and If...Else.
- Helsinki Python MOOC: Conditional statements, More conditionals, and Combining conditions.
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.
- Interactive notebook (right-click → Save As... then remove the .txt part so it is saved as an .ipynb)
- Note that this notebook was created to be used in Notable; its interactive checker cells might not work in every environment.
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.