Here you will be learning about the following relating to python:
- File handling
- Logic operators
- Variables, local and global variables, constants
- Command words
- Statements
- Sequences
- Subroutines, procedures, functions
- Arrays(list), 2 dimensional arrays
- File handling, read, write, close and database
- Data structures
Constructs and techniques and their implementation in different languages
1. C O N S T R U C T S A N D T E C H N I Q U E S A N D
T H E I R I M P L E M E N TAT I O N I N
D I F F E R E N T L A N G U A G E S 🐍
B Y O L I V E R Y O U N G
2. C O M M A N D W O R D S
• While
• If
• For
• Print()
• Input()
• Most languages
have the
following
keywords such
as Python,
however it
maybe worded
differently to
other
languages. For
example in Java
it maybe slightly
different.
• While ()
• If ()
• For ()
• System.out.
println()
• Input()
• Java• Python
3. C O N S TA N T S A N D VA R I A B L E S , L O C A L
A N D G L O B A L VA R I A B L E S
• Variable - is where to assign a value to; those values can change and be
replaced later on.
• Constants are variables that stay the same every time a program is executed.
• Constants are not expected to change.
• Python does not have any constants instead just make sure you don’t change
the value of the variable. For example: by = 50
• Java however does, for example final by = 50 which means that no matter
what, you can’t later on change that value. For example, I then later on do by
= ‘any word’ it will give an error message.
•
4. L O C A L VA R I A B L E S
• Local variables are just variables that
you can access within a particular
region. For example, for 🐍
qq = [1, ‘bring’, 2.2]
for pos in qq:
print pos
pos is a local variable to the for loop
and cannot be accessed outside of
that for loop block.
Another example:
def jump(x):
x = ‘’
y = ‘minger’
return x + y
I can’t access the variable x and y outside of
that function.
5. G L O B A L VA R I A B L E
• Global variable is a variable that can be accessed
either inside and or outside the function.
For example:
def zzzz (a):
a = 0
y = 20
return a + y
I can’t access the variable x
and y outside of that function.
But if I did this:
def zzzz (a):
global y
y = 20
a = 0
return a
zzzz()
Q = y+50
Print(Q)
6. S TAT E M E N T
• Assignment: is the assigning of a value to a variable. For example f = 20,
means you are assigning 20 to f, if I do f = ‘word’ later on, f becomes ‘word'.
You can also do q += 1, which means assign that addition of 1 into q, whilst
w -= 3, means assign that subtraction of 3 to w
• Input is a statement that is used to tell the computer that the user must enter
something in order continue with the program. For example in python, gg =
str(input())
• Output is a statement (in python is print) that displays or shows something
on the screen. For example, print(755)
• Sequence can contain any instructions (i.e. print, inputs etc. ) that are run in
the order they are presented. No instructions are skipped unless you specify
it to. for example:
7. S E Q U E N C E
raffle = [4, 5, 6, 1, 2, 5]
for numbers in raffle:
If numbers != 5:
print (numbers)
x = ‘hello’
for x in range(2,5):
print(x)
• For example, the sequences
skips 5 because of the if
statement
9. D E F I N E S U B R O U T I N E S
• A procedure is a function which does not return
anything.
• A function is a block that usually returns something
• A subroutine is either a function or a procedure, i.e.,
a generic term for a collected code
10. P R O C E D U R E
def my_function():
x = 13
print (x)
my_function()
def my_function(y):
x = y + 1
return x
my_function(7)
F U N C T I O N
11. L O G I C A L O P E R AT O R S
A = ‘q’
B = 3
If A == ‘q’ and B == 3:
print(True)
AND ^
Uses of these: if you want something to be
both true before something happens
12. A = ‘q’
B = 3
If A == ‘q’ or B == 3:
print(True)
OR v
Uses of these: if you want only need one to
be true before something happens
L O G I C A L O P E R AT O R S
13. A = ‘q’
B = 3
If not(A == ‘q’ and B == 3):
print(True)
NOT ⌝
Uses of these: if you want something to be
not the case, before something happens
L O G I C A L O P E R AT O R S
14. A = 'q'
B = 3
if (A == 'q' and B == 3) or not(A == 'q'
and B == 3):
print(True)
Bi-conditional
<->
Because
L O G I C A L O P E R AT O R S
15. A R R AY S ( N A M E D I N P Y T H O N A S L I S T )
• Is a collection of elements, whether it be int, string or
float. It can be used for many things even calling out a
function in sequence.
• 2D arrays is list inside of a list
16. E X A M P L E S O F A R R AY A N D 2
D I M E N S I O N A L A R R AY
Q = [11, 12, 5, 2]
print(Q[3])
Q = [[11, 12, 5, 2], [15, 6,10]]
print(Q[2][0])
This will output 15This will output 2
17. F I L E H A N D L I N G
• You can connect this
to a database
I M P O RT M Y S Q L . C O N N E C T O R
M Y _ D ATA B A S E =
M Y S Q L . C O N N E C T O R . C O N N E
C T (
H O S T = " L O C A L H O S T " ,
U S E R = " M Y U S E R N A M E " ,
PA S S W O R D = " M Y PA S S W O R D "
)
P R I N T ( M Y _ D ATA B A S E )
V = O P E N ( " D E M O F I L E 2 . T X T " , " A " )
V. W R I T E ( “ I C A N S E E T H E T R E E S . ” )
V. C L O S E ( )
# O P E N A N D R E A D T H E F I L E B Y D O I N G
T H E C O D E B E L O W
V = O P E N ( " D E M O F I L E 2 . T X T " , " R " )
P R I N T ( V. R E A D ( ) )
• You can create a .txt file.
• Write on it
• And close the file
• You can use the read function to
what is read inside of the file
18. F I L E H A N D L I N G
• We can use file handling when we want to write a
program that can manipulate a text file. For example,
when we want to manipulate data in it, i.e. anti-virus
program that detects a malicious code, or you just want
to remove all of the commas on the file using python
etc.
• You can also connect it to a database and transfer the
password and username to a database, which is usually
safer. So we can do some matching, if the user is in the
database already, then give an error etc.
19. D ATA S T R U C T U R E
• It is a way we
organise data, in a
structured way so that
we can efficiently
manipulate it later on.
• For example,
dictionary and list are
a form of data
structures in python
Q = [11, 12, 5, 2]
print(Q[3])
diction =
{ “1": "Bobby",
"2": “Sussy",
"3": “Sam” }
print(diction)
20. E V E N T H A N D L I N G
from tkinter import *
def singles(event):
print("Single clicks, recognised ")
def doubles(event):
print("Stopping, because double click")
import sys; sys.exit()
widget = Button(None, text='Mouse Clicks')
widget.pack()
widget.bind('<Button-1>', singles)
widget.bind('<Double-1>', doubles)
widget.mainloop()
Event handling is when
a particular function,
subroutine or method
(function in a class) in
response to an event
such as a click or
keyboard inputs.
Example, of event
handling in python.