Concatenating strings means to join a string to another. For example
wordOne = "Geek"
wordTwo = "Resources"
sentence = wordOne + " " + wordTwo - sentence would now become Geek Resources
A string can be lengthened by adding more characters. For example:
wordOne = "Geek" + " Resources" - wordOne would now become Geek Resources
These examples are in python
The length of a string can be measured using the len statement, this gives the length as an integer.
wordOne = "Geek"
len(wordOne) - this would give the answer 4, as there are 4 characters in the word "Geek"
This example is in python
String manipulation allows us to change all the letters in a string to either uppercase or lowercase. This can be useful when checking inputs. To change to uppercase or lowercase use .lower or .upper, below is an example of both:
name = "Geek Resources"
name = name .lower() - this would make name become "geek resources"
name = name.upper() - this would make name become "GEEK RESOURCES"
This example is in python
String manipulation also allows us to determine which character features at a position within a string. For example:
word = "Resources"
word[5] - this would give the answer "R" as R is the sixth character in the word "Resources"
word[0:8] - this would give the answer "Resource" , as they are the first 8 characters in the string
word[2:8] - this would give the answer "source" , as they are the first 8 characters in the string
This example is in python
File handling operations are a way to interact with files stored on a computer's storage device. In Python, we can perform file handling operations using built-in functions and modules.
There are three main file handling operations: reading, writing, and appending.
To read from a file in Python, we can use the open() function with the mode parameter set to "r" (read). Here's an example:
# Open the file in read mode
file = open("filename.txt", "r")
# Read the contents of the file
contents = file.read()
# Close the file
file.close()
# Print the contents of the file
print(contents)
In the example above, we opened the file named "example.txt" in read mode, read its contents using the read() function, printed them to the console, and then closed the file using the close() function.
To write to a file in Python, we can use the open() function with the mode parameter set to "w" (write). Here's an example:
# Open the file in write mode
file = open("filename.txt", "w")
# Write to the file
file.write("This is some text.")
# Close the file
file.close()
In the example above, we opened the file named "example.txt" in write mode, wrote the text "This is some text." to it using the write() function, and then closed the file using the close() function.
To append to a file in Python, we can use the open() function with the mode parameter set to "a" (append). Here's an example:
# Open the file in append mode
file = open("filename.txt", "a")
# Append to the file
file.write("This is some more text.")
# Close the file
file.close()
In the example above, we opened the file named "example.txt" in append mode, wrote the text "This is some more text." to it using the write() function, and then closed the file using the close() function.
It is important to always close the file after performing file handling operations to avoid any errors or issues with the file. A
An array is a location in memory holds multiple values under a single identifier (similar to a list). Each element is accessible by a number, which is known as an index.
Array elements must be of the same data type
Python example of an array:
computer_Parts = ["CPU", "RAM", "GPU", "SSD"]
print(computer_Parts[1])
output will be "RAM"
A two-dimensional array can hold more than one set of data. This type of array is like a table, with data held in rows and columns.
F1_numbers = [[44,63],[16,55],[14,18],[4,81],[1,11]
print(F1__numbers[1]) output will be 16 and 55
print(F1_numbers[0][0]) output will be 44
A record is a data structure that consists of a number of fields which can all be different types. Data in a database is stored as records, which is then stored in files.
An attribute represents a single piece of data, and records generally include one or more attributes. For example, if we consider a record containing information about a person, it may include the following attributes:
Title
First name
Last name
Email address
SQL is a language used to communicate with databases. It allows you to retrieve data using commands like SELECT, which chooses specific columns, FROM, which specifies the table to select data from, and WHERE, which sets conditions for selecting the data.
SELECT * FROM "Drivers" WHERE "Number" LIKE "44"
Note - * stands for wildcard which means return all records. This would retrieve Mercedes 44 Lewis Hamilton
Subprograms are independent code units within a main program that perform specific tasks. They are designed to break down large and complex programs into smaller, more manageable pieces. There are two main types of subprograms: procedures and functions.
Subprograms are typically small in size, making them easier to write, test, and debug.
Subprograms can be saved as separate modules and reused in other programs, saving time and effort.
Subprograms can be used multiple times in the main program, reducing the amount of code that needs to be written and resulting in shorter programs.
Procedures are subprograms that do not return a value. They are used to perform a set of instructions or actions, and may or may not accept input parameters. Procedures are called using their name and any input parameters, and the instructions within the procedure are executed.
# This is a procedure that prints "Hello, World!"
def say_hello():
print("Hello, World!")
# Call the procedure
say_hello()
Functions, on the other hand, are subprograms that return a value. They are used to perform a calculation or operation on one or more input parameters, and the result is returned to the calling program. Functions are called using their name and any input parameters, and the returned value can be stored in a variable or used in other calculations within the program.
def add_numbers(a, b):
result = a + b
return result
Generating a random number may look different in other programming languages but in python its looks like this:
import random
random_number = random.randint(1,4) - this will generate a random number between 1 and 4
In OCR exam reference language you can either state the first and last possible values
e.g. random_number = randomt(1,4)
Or just the maximum value , if starting from 1
e.g. random_number = random.(4)
Variable scope - The scope of a variable, constant, procedure, or function specifies where in the program it can be recognized and used.
Local scope refers to a variable that is only accessible within a specific block of code. This means that the variable can only be used within the block of code where it was declared, and is not accessible outside of that block. For example, if a variable is declared within a function, it can only be accessed within that function and not outside of it.
Local variables keep a subroutine self-contained, enabling it to be utilized in any program without conflicts with variable names used in the calling program.
Global scope, on the other hand, refers to a variable or function that can be accessed from anywhere within the program. This means that the variable or function can be used in any part of the program, even if it was declared in a different function or block of code. However, it's important to note that using global variables can sometimes lead to errors and make code harder to maintain, so it's generally considered good practice to use local variables whenever possible.