Python Programming: Basics (Leaving Cert Computer Science): Revision Notes
Python Programming: Basics
Python Overview
Python is a high-level, interpreted programming language known for its simplicity and readability. Its clear syntax makes it an excellent language for people who are beginning to code, and it is the language assessed in the Leaving Certificate written examination.
How to learn Python
Learning programming is like learning mathematics — it cannot be picked up by simply reading theory and examples. Each snippet of code should be run on your own machine (using an IDE or an online compiler). Make sure you understand every component of the code by experimenting with the functions yourself.
It is recommended that you do not move on to the next Python chapter until you feel comfortable with the current one. Writing code yourself is where the majority of your learning will come from.
A word on errors
Errors in programming are inevitable, and having a positive approach to errors is key to improving. They are frustrating by nature, but once you resolve an error you are unlikely to repeat it. Try to solve errors yourself by reading through your code and searching the online documentation. Generative AI should be a last resort, since leaning on it provides the least benefit to your learning. In some exam questions you may be asked to explain why an error occurs on a particular line of code.
The first Python program we write will print a message to the console.
print("Hello World")
Variables & Data Types
Variables are used to store values such as integers. Think of a variable as a name attached to a particular object. To create a variable, we assign it a value using the assignment operator, which is a single equals sign (=).
n = 100 # n is assigned the value 100
print(n) # this will print 100
n = 200 # n is now assigned the value 200
Note that we are using comments to annotate our code. Comments are messages we can leave in the code that the interpreter ignores. Every comment comes after a # symbol.
Like most programming languages, Python supports many data types. To find the data type of a variable, we can use the type function.
Integers are whole numbers (positive, negative or zero).
integer = 30
print(type(integer)) # <class 'int'>
Floats (short for floating-point numbers) represent real numbers that have fractional parts, i.e. decimal numbers.
decimal_number = 3.14
print(type(decimal_number)) # <class 'float'>
Strings are sequences of characters.
sentence = "Hello Python!"
name = 'John Doe'
print(type(name)) # <class 'str'>
All strings must be surrounded by either "" or ''.
Booleans are either True or False.
is_authenticated = True
print(type(is_authenticated)) # <class 'bool'>
Python's naming convention is snake case: variable names start with a lowercase letter, and if a name is made up of two or more words, they are separated with an underscore (for example user_input). Different languages have different naming conventions.
Operations
In Python, different data types support various operations. In mathematics it is well known that we can add two integers: addition is the operation, and the two integers are the operands.
# arithmetic operations - applicable to ints and floats (numeric types)
a = 5
b = 3
# addition, subtraction, multiplication, division
c = a + b # c is assigned 8
d = a - b # d is assigned 2
e = a * b # e is assigned 15
f = a / b # f is assigned 1.6666666666666667
# floor division (divides, then rounds down)
g = a // b # g is assigned 1
# modulus (returns the remainder of division)
h = a % b # h is assigned 2
# exponentiation
i = a ** b # i is assigned 125
Python also supports comparison operations. Arithmetic operations act on numeric values and return numeric values; comparison operations act on many types of value and return Boolean values.
# comparison operations
a = 10
b = 20
c = 30
d = 10
# equal to
print(a == d) # True
print(a == b) # False
# not equal to
print(a != d) # False
print(a != b) # True
# greater than
print(c > d) # True
# less than
print(d < c) # True
# greater than or equal to
print(c >= b) # True
print(a >= d) # True
# less than or equal to
print(c <= b) # False
print(a <= d) # True
Variables in Python can be converted from one data type to another. In most programming languages this is known as type casting.
number_as_string = "3" # convert "3" to 3 (string to int)
number_as_int = int(number_as_string)
b = 4
print(number_as_int + b) # 7
User Input
Sometimes we would like to get input from the user. This is typically done using the input function.
user_input = input("Enter something: ")
print("You entered:", user_input)
This snippet calls the input function, which waits for the user to type something. This input is always a string, so you often need to convert it to another data type if a string is not what you want.
Observe the different method used to print here. It first prints "You entered:", then the comma separates the string from the variable. In this context the comma acts as a separator that tells Python to print multiple items with a space between them by default. user_input is the variable containing the data the user entered.
name = input("Enter your name: ")
age = input("Enter your age: ")
print("Hello " + name + ", you are " + age + " years old")
In this print statement we use string concatenation to join several strings into one. String concatenation uses the + operator. Note that concatenation only works between strings — this is why age works here (input returns a string), but you would get an error if you tried to concatenate a string with an integer directly.
Questions
Question 1
- Define two variables, length and width, to store the dimensions of a rectangle. Calculate and print the area of the rectangle.
Question 2
- Create a variable text containing the string "Python Programming". Print the number of characters in text.
You may need the len function, which takes in a string and returns its length.
Question 3
- Write a program that takes in three integers from the user and prints the average of those numbers.
Question 4
- Write a program that takes the radius and height of a cylinder from the user and prints its volume. The volume of a cylinder is .
You may need the math library to get pi. To use a library, use the import keyword at the start of your program.
import math
pi = math.pi
Question 5
- Write a program that takes the user's birth year as input and calculates their age.
Question 6
- Write a program that asks the user for two numbers and prints whether the first is divisible by the second (hint: use the % modulus operator).
Remember!
- Python is a high-level, interpreted language valued for readable syntax; the best way to learn it is to write and run code yourself.
- Variables store values and are assigned with a single =. Core data types: int, float, str, bool — check any value's type with type().
- Arithmetic operators (+ - * / // % **) return numbers; comparison operators (== != > < >= <=) return True/False.
- type casting converts between types, e.g. int("3").
- input() reads from the user and always returns a string, so cast it when you need a number.
- Join strings with concatenation (+) — but only string with string.
- Python uses snake_case and # for comments.