(For Python 3.X)

Python is a high-level object-oriented programming language. Python stores files with the extension .py

Variables

A variable is a data-store which stores a specific data-type.

The value being stored can be replaced, changed, removed etc.

#This is an example of using variables

var1 = 5

print(var1)

var1 = 6

print(var1)

var1 = var1 +1

print(var1)

Mathematical Operators

#This is an example of mathematic operators being used.

#Addition

var1 = 5

var2 = var1 + 5

print(var2)

 

#Subtraction

var1 = 5

var2 = var1 – 5

print(var2)

 

#Multiplication

var1 = 5

var2 = var1 * 5

print(var2)

 

#Division

var1 = 5

var2 = var1 / 5

print(var2)

Comparison/Logic Operators

Comparisons evaluate as True or False. Comparisons are often used with conditional statements and loops.

#This is an example of comparison operators being used. 

#Greater than

print(“5 > 5 = “, str(5>5)) 

 

#Greater than or equal to

print(“5 >= 5 = “, str(5>=5)) 

 

#Less than

print(“5 < 5 = “, str(5<5))  

 

#Less than or equal to

print(“5 <= 5 = “, str(5<=5)) 

 

#Equal to

print(“5 == 5 = “, str(5==5)) 

 

#Not Equal to

print(“5 != 5 = “, str(5!=5)) 

print(“5 != 4 = “, str(5!=4)) 

Assignment

Assigning a value stores a value 

#This is an example of assigning a value to a variable and then displaying it as a message (printing to the screen) to the user. (== means equals, = means assignment).

var1 = 5

print(var1)

User Input

User Input allows the program to interact with data from the user.

#This code is an example of user input being stored into a variable and then concatenated (joined) with other text and displayed to the user as a message.

user_input = input(“Please type your age as an integer (whole number)”)

print(“Your age is ” + user_input)

Data Types

Data types preserve the format and integrity of data to ensure data isn’t lost.

#This is an example of storing data as specific data types. 

#Stores data as string

var1 = “5”

print(var1)

 

#Stores data as integer (whole number only)

var2 = 5

print(var2)

 

#Stores data as real number (also known as float, stores integer and decimal)

var3 = 5.3

print(var3)

 

#Stores data as Boolean (true or false)

var4 = True

import datetime

 

#Stores data as a date & time

var5 = datetime.datetime(2020, 5, 17)

print(var5)

For Loops

A FOR loop is a count controlled loop that runs a set number of times.

#This is an example of a FOR loop. 

for i in range(0,10):

     print(i)

While Loops

A WHILE loop is a condition controlled loop.

#This is an example of a WHILE loop. 

#This loop will run as the condition has been met.

flag = True

while (flag == True):

     print(“Hello”)

 

#This loop will not run as the condition has not been met.

flag = False

while (flag == True):

     print(“Hello”)

Conditional Statements (IF,ELIF,ELSE)

It is a conditional statement that allows code to run only if the condition is met. The statement always begins with an IF, following conditions are always ELIF. If no conditions have been met ELSE can be used to run code.

#This is an example of an IF/ELIF/ELSE statement. 

var1 = 5

var2 = 10

if var1 > var2:

     print(“var1 is greater than var2”)

elif var1 < var2:

     print(“var1 is less than var2”)

elif var1 == var2:

     print(“var1 is the same as var2”)

else:

     print(“Error”)

Random Number Generation

#This is an example of code which generates a random integer between 1 & 100.

from random import randint

r = randint(0,100)

print(r)

Lists

A list stores a sequence of independent values. Each value is indexed.

A LIST can return a specific value (chosen by index) or all values.

#This is an example of a LIST storing letters from the alphabet. 

alphabet = [“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”W”,”X”,”Y”,”Z”]

print(alphabet[0])

print(alphabet)

String Manipulation

String manipulation includes joining, splitting, adding, removing, searching etc.

#This is an example of string manipulation. 

#This is an example of string concatenation (joining).

string1 = “Hello”

string2 = “World”

print(string1, string2)

print(string1, “what day is it?”)

Casting

Data is cast so that it can be used in a different way. E.g. using strings “2” + “2” = “22”, using integers 2 + 2 = 4

#This is an example of data being cast from one data type to another. 

#Float being cast as integer

var1 = 5.123

print(int(var1))

 

#String being cast as integer

var1 = “5”

print(int(var1))

 

#String being cast as float

var1 = “5.123”

print(float(var1))

 

#Float being cast as string

var1 = 5.123

print(str(var1))

 

#integer being cast as string

var1 = 5

print(str(var1))


Functions

A function is a sub-program. A function can be called to launch it with values and can return a value. Sub-programs are useful as they allow smaller programs to run independently from the main program and focus on a specific task.

#This is an example of a function. 

def menu(var1):

     print(“Hello”, var1)

     return True

 

menu(“World”)