There are many tasks that can be achieved with programming language;
most have built in core features that allow programmers to create bigger and
better programs. Some of the features that are included in programming
languages are:
·
Sequence
·
Selection
·
Iteration
·
Variables
Sequence
A sequence tends to be standard program statements that are
executed one after then other this will run in the sequence and will not
differentiate the order. As python reads the program in will read it in the
order in which it is written this will ensure the program loads step by step.

Selection
Selection statements are in essence what they say to do;
they give the user a choice or a selection of answers so they can produce their
own outcome. This best example of this would be a very basic calculator; in
this instance you ask the user for two numbers, you then ask the user to make a
selection from the pre-programmed selections, the code below shows this in a
bit more detail.
‘ menu =
"Welcome to the Python Calculator\n\ #Store menu text in a string
What calculation
would you like to perform?\n\
1. Add\n\
2. Subtract \n\
3. Multiply \n\
4. Divide \n\
5. Square first
number \n\
6. Square second
number \n\
7. Calculate the
power\n"
num1 =
int(input("Enter the first number: ")) #important to cast as int
num2 =
int(input("Enter the second number: "))
print(menu)
choice =
int(input("Enter your choice: ")) #get user choice
if
choice == 1:
print(num1 + num2)
elif
choice == 2:
print(num1 - num2)
elif
choice == 3:
print(num1 * num2)
elif
choice == 4:
print(num1 / num2)
elif
choice == 5:
print(num1**2)
elif
choice == 6:
print(num2**2)
elif
choice == 7:
print(num1**num2)
’
Iteration
Another word for iteration could be a loop; there are two
types of loops that can be used to repeat certain sections of a programmer’s
code. A fixed look would be continuously executed and would only be able to
stop if there was an option to use “break;” within the code otherwise the
program could continue to run until manually terminated. The other type of loop
that can be used are conditional loops these loops will run until a condition
has been met this could be either before or maybe ever after the loop has
completed at least once.
Pre-Check Loops
A pre-check loop would have the condition placed at the
beginning of the loop this would check that the condition is true before the code
or loop is executed, if the condition equals false straight from the beginning
this would cause the loop not to execute event once. In addition to this if the
execution of the loop causes the condition to change it will again have broken
the loop and will no longer run. An example of this is shown below:
for loopCounter in
range(2,41,2):
print(loopCounter)
The above snippet of code tells the computer to print all
numbers in starting from 2 and ending in 41, the third digit tells the program
to only print every second number hence the program counts up in twos like the
two times table. The code above will result in the following output:

Post-Check Loops
These types of conditional loops check the condition at the
end of the loop this means that the loop will always run at least once to see
what the condition is that needs to be met, this would then determine whether
or not the program should loop around again, with this being automatically
looped there are risks that the program may go into an infinite loop.
height =
int(input("Please enter your height (In Inches): "))
while height > 0:
if height <= 48:
print("You are the correct height
for this ride")
else:
print("You are too big for this
ride, Sorry!")
height = int(input("Please enter your
height: "))
The code above will ask the user their height in inches this
will then allow the user to enter their height, and will tell them whether they
are able to or are too big to ride the ride. The following screenshot is the
result of the code:

Variable
A variable within a program can be classes as many things,
for example both inputs and outputs will be stored as variables even if the
variable gets changed at a later date. It is stored within the program for a
certain period of time depending on which type of variable it is.
The data that can be stored within the variables are made up
of numbers and words, when a programmer is coding a program they will have to
declare which variable they should use
and the developer should also name the variable in relation to its
value.
For example when dealing with numbers an “int” should be
used this stands for integer and is a whole number. A string should be used
declare words and writing, numbers can be put in this but just as a character
and not to be able to do mathematical equations.
Local and Global
Variables
Programs are usually made up from a collection of functions.
A variable can be declared within a function and it can only be called upon in
that function. On the other hand there are global variables these need to be
declared at the top of the program this way they are able to be recalled all
throughout the program and will save a developer from having to repeat them
over and over again.
Input and Output
The input allows the
user to interact with the program and input information to provide a different
result. An output is the result after the user is has input the information and
executed the program both of these will be shown in the screenshot below.
Logical Operators
A logical operator is used when an output or action is
dependent on one to several conditions being met. The three operands that are
to be used are the following:
·
OR
·
AND
·
NOT
This will only be able to be used to tell the user if the
statement is true or false , this will then determine which path the program
would then have to go down.
OR = ||
The program would choose a response if either on OR the
other condition meets the correct criteria, if neither of them meet the
criteria then the program would return false and thus sending the program down
the false path. If one of the conditions are true then it would send the
program down the true path.
AND &&
The program would again choose a response but only if both of the conditions are met,
for example if both of the conditions are met then the outcome would be true
and it would send it on that pathway whereas even if one condition is true but
the other is not then the overall outcome would be false and will send it down
the false pathway.
NOT !=
This would execute a command or return a value when the
condition is false.
Selection
Operators
Selection operators tend to be used when if statements are
used, when something is dependent on a statement. Depending on the answer will
depend on how the program continues, for example:

The selection operator is “<” in this instance is means
that if the answer is less than two it will select the first option whereas if
the answer is above two it would then select the “else” option. The selection
operators are show below:
Operator
|
Symbol
|
Purpose
|
Greater Than
|
>
|
This would check to sell if the value on the left
was bigger than the value on the right.
|
Less Than
|
<
|
This checks to see that the value on the left is less than the value
on the right.
|
Equal To
|
==
|
Checks to see if the two values are equal, if they
are the condition would be true.
|
Greater Than
Or Equal To
|
>=
|
Checks to see if the value on the left is equal to or greater than
the value on the right.
|
Less Than Or
Equal To
|
<=
|
Checks to see if the value on the left is equal to
or less than the value on the right.
|
Not equal to
|
!=
|
Checks if the value is equal of not if they aren’t then the condition
is true.
|
List / Array
A list or array is a variable with multiple of the same
elements stored within is this allows a developer to add delete and recall the
list throughout the program whenever they like, it also makes the code more
light weight and will ensure the developer doesn’t have to declare every
variable every time then want to recall them all instead they will only have to
recall the one list variable.
Reading and
Writing to and from a text file
Reading and writing allows a user to write and receive information
to a text file, a program will need to have the ability to write to a file this
allows the program to save and store variables for a later recall. This will allow for tasks such as creating a
log in to recall information from a username and would be able to compare the
password to ensure the user is able to log on.
The code below will open a specified text file, anything
that has been specified will then be written to the text file it will then be
saved.

No comments:
Post a Comment