COMP7004 - Systems Scripting
Lecture 10: Python Basics
Dr. Vincent Emeakaroha
vincent.emeakaroha@mtu.ie
Semester 2, 2026
Lecture Goals
Introduction to Python scripting language
Keywords
Expressions
Operators
Python syntax and basic structures
Data types
First Python program
History
Python was first released in 1991
Although work on it started in 1980s
Created by Guido Van Rossum
Dutch programmer
Name “Python does not come from the dangerous snake
Derived from comedy series “Monty Python's Flying Circus
Latest stable version is 3.14.3
Released in February 2026
https://www.python.org/downloads/
Features
Easy to learn
Simple language with easier syntax to read and write
Portability
Have support across variety of operating systems
Write on one platform and run in another without issues
Extensible
Can be easily combined with other programming languages to extend
capabilities
High-level scripting language
In-built memory management and garbage collection
Large standard library collection
Increases efficiency by avoiding repeating of existing code
Background
By installing the Python interpreter and Python Virtual Machine…
Computer able to understand the Python language and to perform some
computations based on its Python knowledge.
Key Concepts
After installing Python Runtime, what knowledge does
a computer have?
Three knowledge levels:
1. Default knowledge Prelude.
2. Linked knowledge Imported.
3. New knowledge Generated.
Default Knowledge -> Prelude
Let’s suppose a computer speaks Python. Thus, the computer has some
implicit knowledge of Python’s grammar and expressions:
Keywords: It knows what is a for loop, if statement…
Datatypes: It knows that 5 is an integer, that True is a Boolean
Operators: It knows that 5 < 6 = True, that True /\ False = False…
It understands functions and modularity: It has a value/reference
parameter passing policy, strictness/laziness of parameter evaluation…
This is usually referred to as the programming language
prelude. By speaking Python, the computer understands all
these things (it is part of its knowledge) and thus can do
computations using them.
Linked Knowledge -> Imported
A computer can borrow some knowledge generated by others:
E.g., the Python prelude does not include logarithmic operations. Thus, it is
not part of the default knowledge a computer has, so it cannot do
computations including logarithms.
However, this can be fixed by borrowing the external library:
import math;
This extends the Python knowledge the computer has with
logarithms and other maths operations, which can be from now on
part of the operations the computer can do.
New Knowledge -> Generated
We can extend by ourselves (via Python programs) the Python
knowledge a computer can use.
Imagine we want to compute the number of words written in a
text file.
This functionality does not belong to the Python prelude, so
the computer does not know how to do it just by the sake of
speaking Python.
Moreover, let’s suppose that nobody in the Python community
did this before (of course this is not the case, but let’s suppose
it is). Thus, the computer cannot borrow this knowledge by
importing it neither.
New Knowledge -> Generated
We can extend the knowledge of Python by making it to
open a text file and count the words written on it. All we
need is to:
1. Create a new Python program (e.g., myProgram.py).
2. Implement by ourselves within the program a new function (e.g.,
myCountWords) achieving the functionality required:
def myCountWords(file):
#function content (based on knowledge the computer has)
3. Import the program myProgram.py, so that the computer learns all
the new functions defined on it.
4. Now the computer can use myCountWords as part of its Python
knowledge.
Python Programming Basics
Since the three categories of knowledge have been introduced,
let us elaborate a bit more on each of them as follows:
Default knowledge (Prelude):
Keywords, Data Types, Operators
Linked knowledge (Imported):
Imports
New knowledge (Generated):
Variables, Functions
and, for the sake of being used for new generated knowledge:
Control Flow, Exception Handling, User Inputs
Keywords
A keyword, or reserved word, is a word that is reserved by a program
because it has a special meaning.
This keyword and its meaning is known by the computer, as is part of
the Prelude or default knowledge.
The Python language has a small subset of keywords:
Interactive Shell: Entry into Python
Lunching Python interactive shell, e.g.,
Windows: All Programs -> Python 3.13 -> IDLE
Mac OS X: Applications -> Python 3.13 -> IDLE
Linux (Ubuntu): Open terminal window and enter idle or idle3
When Python shell is lunched
A prompt “>>>” will appear
You are ready to let Python do something for you
A simple math with Python
Example: >>> 4+4
Output: 8
Expression
An expression is the most basic programming instruction
Example: 4+4
Expressions consist of
Values such as 4
Operators such as +
They can evaluate down to a single value
Meaning one can use expression anywhere in Python where
single value is required
A single value without operator is also an expression but it
evaluates to itself.
Operators
Operators Operation Example Evaluates to ..
**
Exponent
2 ** 3
8
%
Modulus/Remainder
22 % 8
6
//
Integer division/floored quotient
22 // 8
2
/
Division
22
/ 8
2.75
*
Multiplication
3 * 5
15
-
Subtraction
5
- 2
3
+
Addition
4 + 4
8
Operator Precedence
Order of operator execution
Same to that of Mathematics
**, *, /, //, %, + and -, from left to right priority
Examples on Python shell:
>>> 2 + 3 * 6
Output: 20
>>> (2 + 3) * 6
Output: 30
Expression and Error
The rule for putting value and operator together to form
expression is fundamental to Python programming language.
Just like grammar rules help us to communicate
In case of wrong/bad expression
Python outputs syntax error message
Example:
>>> 5 +
SyntaxError: invalid syntax
One can always check if an expression works by typing it into
the Python interactive shell.
Data Types
Specifies a category for values
Every value belongs to exactly one data type
Three basic data types in Python
Integer or int: indicate values that are whole numbers
Floating-point numbers or floats: indicate numbers with
decimal points
Strings: indicates text values. Always surround strings in a
single or double quotes
Python is a typeless language. This means that you do not
have to declare the variable type. The assigned value
determines the type for the variable.
Common Data Types
Data Type
Example
Integers
-
2, -1, 0, 1, 2, 3, 24, 456
Floating
-point numbers
-
1.25, -1.0, 0.0, 0.5, 1.6, 42.586
Strings
a’, ‘aa’, ‘hello’, ‘22 dogs, ‘big 5
You can have strings with no character
Called blank string
Example: or “
More on strings later in the lecture
Storing Values in Variables
A variable is like a named box in a computer memory where
one can store a single value.
Expression results can be stored in variables for later usage.
Variable names
Can be chosen by the programmer considering language restrictions.
Names should be self descriptive for ease of understanding.
Assignment statement
Enable storing values in variables.
Consist of a variable name, an equal sign and the value to be stored.
Example: counter = 40
Means that the variable “counter” will have the integer value 40 stored in it.
Interactive Shell: Variable Example
>>> counter = 40
>>> counter
40
>>> nums = 7
>>> counter + nums
47
>>> counter + nums + counter
87
>>> counter = counter + 5
>>> counter
45
Variable Naming Convention
Variables can be named anything as long as they obey
these three rules:
1. It can be only one word.
2. It can use only letters, numbers, and the underscore
(_) character.
3. It cannot begin with a number.
Valid and Invalid Variable Names
Valid Variable Names Invalid Variable Names
balance
current
-balance (Hyphens not allowed)
currentBalance
current balance (Spaces not allowed)
current_balance
4account (Cannot begin with a number)
_counter
42 (Cannot begin
with a number)
COUNTER
total_$um
(Special characters like $ are not allowed)
account4
’hello’ (Special characters like ‘ are not allowed)
Variable names are case sensitive
COUNTER, counter, Counter, and couNter are four different variables.
Python has the convention to start variable names with a lowercase letter
Prefer usage of camelcase instead of underscores
Example: lookLikeThis instead of look_like_this. Note that both are valid variable names
Writing First Program
Requires a text editor
Stores the Python code in a file
Run file to execute program
IDLE platform
Select File -> New File
Start typing your code and save afterwards (File -> Save
As)
To run the program
Select Run -> Run Module or press F5
first.py
1) # This program says Hello and ask for my name
2) print('Hello World!')
print('What is your name?')
3) myName = input()
print('It is good to meet you ' + myName)
print('The length of your name is: ')
4) print(len(myName))
print('What is your age?')
myAge = input()
5) print('You will be '+ str(int(myAge)+1) + ' in a year.')
Comments
Python ignores comments
Useful for writing notes and documenting code
Starts with a harsh mark ”#”
1) # This program says Hello and ask for my name
Blank lines are also ignored by Python
Useful for organising codes in paragraphs
Easy reading like in a book
The print() Function
Outputs content on the screen
2) print('Hello World!')
print('What is your name?')
The lineprint(‘Hello World!’)” means print out the text in the
stringHello World!’
The value ‘Hello World! is being passed to the function print().
The value passed to a function is known as “argument”.
More details on functions follows in later lectures.
The input() Function
Reads in user inputs from the keyboard (standard
input)
Waits until user types in some text.
3) myName = input()
Evaluates the user text as string and assigns the value
to the variable myName”.
The len() Function
Accepts a string argument
Returns the number of characters in that string as integer value.
print('The length of your name is: ')
4) print(len(myName))
len(myName)” evaluates to an integer value and this value is
passed to the print function to output it on the screen.
Note that the print function accepts both integer and string
arguments
Implicit concatenation of both leads to an error.
Concatenation of Integer and String
Interactive shell example:
>>> print('I am ' + 26 + years old')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
print('I am ' + 26 + years old')
TypeError: must be str, not int
This error is not caused by the print function but by the
expression
I am + 26 + years old
Addition of string and integer value not allowed
This can be fixed by using a string version of integer instead
The str(), int() and float() Functions
str() converts an integer value to a string version
Example: >>> str(29) will output ‘29
>>> print('I am ' + str(26) + ' years old')
' I am 26 years old '
int() converts a string version of integer to integer
Example: >>> int('26') will output 26
float() converts a string version of floating-point number to
float
Example: >>> float(' 4.16 ') will output 4.16
Concatenation: first.py
The function “input()” always returns a string value:
myAge = input()
5) print('You will be '+ str(int(myAge)+1) + ' in a year.’)
In the above extract, the functions int() and str() are used
to get the appropriate data types to avoid syntax error.
Thank You!