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.
It is a suitable language for those who are beginning to code due to its clear syntax.
How to Learn Python. Learning programming is synonymous to learning mathematics, it cannot be learnt by simply reading theory and examples. Instead each snippet of code should be run on your own machine (such as an IDE or online compiler). Make sure you can understand the different components of the code by exploring the functions yourself.
It is recommended that you do not move onto the next Python Programming chapter until you feel comfortable after each programming chapter. Writing code yourself is where the majority of your study will come from.
Errors Errors in programming are inevitable. Having a positive approach to errors is the key to improving your programming. They are frustrating by nature but once an error is resolved, it is likely that you won't repeat it. It is recommended you solve errors on your own by reading through your code and searching through online documentation. Generative A.I should be a last resort since it will provide the least amount of benefit to your learning. In some exam questions, you may be asked to explain why an error may occur in a particular line of code.
The first Python programme we write will print a message to the console.
print("Hello World")
Variable & 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 value using the assignment operator, which is single equals (=).
n = 100 # n is assigned to the value 100
print(n) # this will print 100
n = 200 # n is now assigned to the value 200
Note that we are using comments to annotate our code. Comments are messages that we can place without the compiler considering them as code. Every comment should come after a # symbol.
Like most programming languages, Python supports many data types. To get the data type of a specific variable, we can use the type function.
Integers are whole positive number.
integer = 30
print(type(integer)) # <class 'int'>
Floats (short for floating-point number) are used to represent real numbers that have fractional parts, i.e. decimal numbers.
decimal_number = 3.14
print(type(integer)) # <class 'float'>
Strings are sequences of characters.
sentence = "Hello Python!"
name = 'John Doe'
print(type(name)) # <class 'str'>
All string have to be surrounded by either "" or ''.
Booleans are either True or False
is_authenticated = True
print(type(is_authenticated )) # <class 'bool'>
The naming convention Python is snake case meaning that variable names start with a lowercase letter. If a variable is composed of two or more words, they are separated with an underscore. 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 operations and the two integers are the operands.
# arithmetic operations - applicable to ints, floats (numeric types)
a = 5
b = 3
# addition, subtraction, multiplication, division
c = a + b # c is assigned to 8
d = a - b # d is assigned to 2
e = a * b # e is assigned to 15
f = a / b # f is assigned to 1.6666666666666667
# floor division (divides, then rounds down)
g = a // b # g is assigned to 1
# modulus (returns the remainder of division)
h = a % b # h is assigned to 2
# exponentiation
i = a ** b # i is assigned to 125
Python also supports comparison operations. Arithmetic operations operate on numeric values, and return numeric values. Comparison operations operate on many types of values 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 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 data type to another. In most programming languages this is know as type casting.
number_as_string = "3" # I want to convert "3" to 3 (string to int)
number_as_int = int(number_as_string)
b = 4
print(number_as_int + b) # 7
User Input
In some cases 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 code snippet will call the input function, which waits for the user to input something. This input is always a string, you often need to convert it to other data types if the a string is not what you want.
Observe the different method used to print. It first prints "You entered". 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 in between by default. user_input is the variable containing the data entered by the user.
name = input("Enter Your name : ")
age = input("Enter your age: ")
print("Hello " + name + ", you are " + age + " years old")
In this print statement, we are using string concatenation to join several string together as one string. String concatenation uses the + operator.
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 to use the len function, which takes in a string and returns the length of the string.
Question 3
- Write a programme that takes in three integers from the user and prints the average of those numbers
Question 4
- Write a programme that takes the radius and height of a cylinder from the user and print its volume. The volume of a cylinder is .
You may need to use the maths library to get pi. To use a library, use the import keyword at the start of your programming.
import maths
pi = maths.pi
Question 5
- Write a programme that takes the user's birth year as input and calculates their age
Question 6
- Write a programme that takes the user's birth year as input and calculates their age