String manipulation
Enjoying Flashcards?
Tell us what you think
String manipulation
String manipulation is the use of programming techniques to modify, analyse or extract information from a string.
Case conversion
Case conversion is the ability to change a string from one case to another, e.g. lowercase to uppercase.
Length (string manipulation)
Finding the length refers to the ability to count the number of characters in a string.
Substring
A substring is the ability to extract a sequence of characters from a larger string using slicing.
Concatenation
Concatenation is the ability to join two or more strings together to form a single string using the + operator.
ASCII conversion
ASCII conversion is the ability to return an ASCII character from a numerical value and vice versa.
True or False?
In Python substrings are indexed from 0.
True.
In Python, substrings are indexed starting from 0, not 1. This is called 0-indexing.
name = "sarah"
Write Python code to output the variable name in uppercase.
print(name.upper())
password = "letmein"
Write Python code to output the length of the variable "password".
print(len(password))
FName = "Save"
SName = "MyExams"
Write Python code to output the two variables joined together (concatenation).
print(FName + SName)
File handling
File handling is the use of programming techniques to work with information stored in text files.
Write Python code to open a text file named "fruit.txt" in read only mode.
file = open("fruit.txt", "r")
Write Python code to close a text file.
file.close()
Write Python code to read a line in a text file.
file.readline()
Write Python code to write the value "Oranges" to an open text file
file.write("Oranges")
What does "r" mode do when opening a file?
The "r" mode is used for opening a file in read-only mode.
What does "w" mode do when opening a file?
The "w" mode is used for opening a file in write mode, creating a new file if it doesn't exist, or overwriting an existing file.
What does "a" mode do when opening a file?
The "a" mode is used for opening a file in append mode, writing to the end of an existing file.
True or False?
It's important to make a backup of text files before working with them.
True.
It's important to make a backup of text files before working with them, as mistakes can lead to data loss.
Database
A database is an organised collection of data that allows easy storage, retrieval, and management of information.
Field
A field is one piece of information relating to one person, item or object, represented as a column in a database.
Record
A record is a collection of fields relating to one person, item or object, represented as a row in a database.
Array (storing data)
Useful when working with small amounts of data, stored in main memory (RAM).
What is an advantage of using a database?
Advantages of databases include:
efficient sorting and searching of large amounts of data
multiple user access
better security than text files.
What is a disadvantage of using text files for data storage?
A disadvantage of text files is that it can be difficult to know where one record begins and ends.
What is an advantage of using arrays for data storage?
An advantage of arrays is that they can be more efficient and faster to search than text files.
When are text files useful for data storage?
Text files are useful for storing small amounts of data on secondary storage when the application is closed.
SQL
SQL (Structured Query Language) is a programming language used to interact with a Database Management System (DBMS).
SELECT (SQL)
The SELECT command in SQL is used to retrieve data (fields) from a database table. E.g. SELECT name, age
FROM (SQL)
The FROM command in SQL specifies the table(s) to retrieve data from.
WHERE (SQL)
The WHERE command in SQL filters the data based on a specified condition.
True or False?
The '*' symbol is a wildcard in SQL.
True.
The * symbol is called a wildcard in SQL and selects all fields in a table when used with SELECT.
SELECT * FROM Customers;
What does this SQL command do?
This command selects all fields from the Customers table.
SELECT name, age FROM Customers WHERE age > 30;
What does this SQL command do?
This command selects the name and age fields from the Customers table where the age is greater than 30.
True or False?
To select all fields, you must list each field name after SELECT
False.
Using SELECT * will select all fields in the table.
Give an example SQL command to select specific fields (country, population) from a table (world) with a condition (only countries that have a population less than 100,000.
Example:
SELECT country, population FROM world WHERE population < 100000;
What does the FROM clause specify in an SQL SELECT statement?
The FROM clause specifies the table(s) to retrieve data from.
1D array
A 1D array is an ordered, static set of elements that can only store one data type. For example, a row in a table
2D array
A 2D array is an ordered, static set of elements that can only store one data type BUT adds another dimension. It can be visualised as a table with rows and columns.
Write Python code to create a 1D array named 'temp' with numbers 1-5.
temp = [1, 2, 3, 4, 5]
Write Python code to output element 2 of a 1D array named 'countries'.
print(countries[1])
Write Python code to modify element 3 in a 1D array named 'students' with the value "Rebecca".
students[2] = "Rebecca"
Write Python code to create a 2D array named 'scores'. Use values 1-9 to create 3 rows and 3 columns.
scores = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
True or False?
2D arrays can store different data types in the same array.
False.
Like 1D arrays, 2D arrays can only store one data type.
What does zero-indexing mean for arrays?
Zero-indexing means that the indexes of an array start from 0, not 1.
Function
A function is a type of sub-program that performs a specific task and returns a value.
Procedure
A procedure is a type of sub-program that performs a specific task but does not return a value.
Parameter
A parameter is a value that is passed into a sub-program (function or procedure).
True or False?
To use a function/procedure it must be 'called'.
True.
To use a function or procedure, you 'call' it from the main program.
Global variable
A global variable is a variable declared at the outermost level of a program and can be accessed from any part of the program.
Local variable
A local variable is a variable declared within a specific scope, such as a function or code block, and can only be accessed within that scope.
What is the key difference between a function and a procedure?
A function returns a value, whereas a procedure does not return a value.
Write Python code to define a function named 'area' taking 2 parameters (length, width).
def area(length, width):
Write Python code to call a procedure named 'ageCheck', taking 1 parameter (21).
ageCheck(21)
Why are global variables generally discouraged in programming?
Global variables are generally discouraged because they can be accessed and modified from anywhere in the program, leading to potential bugs and hard-to-maintain code.
Why is random number generation used in programming?
Random number generation adds an element of unpredictability to a program.
True of False?
Simulating the roll of a dice is an example of random number generation.
True.
Examples include simulating dice rolls, selecting random questions, national lottery, cryptography.
How do you import the random module in Python?
To import the random module in Python, use:
import random
random.randint(a, b)
The random.randint(a, b) function in Python generates a random integer between a and b, inclusive.
Write Python code to generate a random colour from 'red, 'blue', 'green' and 'yellow. Output the randomly chosen value.
import random
colours = ["Red", "Green", "Blue", "Yellow"]
random_colour = random.choice(colours)
print(random_colour)