2.2 String manipulation + BASIC FILE HANDLING Flashcards

1
Q

Define string manipulation

A

String Manipulation: “The act of manipulating, extracting or changing the characters in a string variable”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

State the string manipulation commands

A

string manipulation commands

.length
.upper
.lower
.substring (x, i)
ASC(…)
CHR(…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does .length mean

A

.length is used to get the length of a string
Returns the length of a string

length = string.length - if the string was “Hello” then length would be 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does .upper and .lower mean

A

.upper and .lower
Used for converting the cases of a string
Returns the string in uppercase and lowercase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What deos .substring(x, i) mean

what will output

chars=”Hello”
chars = string.substring (3,1)
print(chars)

A

substring(x, i)
to get a substring

Returns part of a string, starting at the character of the first parameter and up to (not including) the number of characters in the second parameter

stringname.subString(startingPosition, numberofCharacters)

chars = string.substring (3,1) - index starts at 1

If string was “Hello” then chars would be “I” (lowercase L)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what does ASC(…) mean

ascii=ASC(“A”)
print(ascii)
what would output

A

Ascii conversion
ASC(…)
Returns the ASCII value of a character
ASC(character)
e.g.
ascii=ASC(“A”)
ascii would be 65

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does CHR(…) mean

Char = CHR(65)
print(Char)
what would output

A

Ascii conversion
CHR(…)
Returns the character from the ASCII number

CHR (asciinumber)
Char = CHR(65)
char would be “A”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will this code display

someText=”Computer Science”
print(someText.length
print(someText.substring(3,3)).upper)

A

Will display:
16 - includes spaces
PUT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

State the string manipulation commands in python

A

string manipulation commands in python

.len
.upper()
.lower()
.substring (x, i)
ord(…)
chr(…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does .len mean

write a program to work out the length of this list
“Hello”

what would be the output

In a high level programming language

A

Returns the length of a string

string=”Hello”
length = len (string)
print(length)
- if the string was “Hello” then length would be 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what does .upper and .lower mean

write a program which displays the word “Hello” in uppercase and lowercase

In a high level programming language

A

Returns the string in uppercase or lowercase

ustring = string.upper() -

if the string was “Hello” then ustring would be “HELLO”

lstring = string.lower() -

If the string was “Hello” then lstring would be “hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does .substring (x, i) do

write a program which splits “Hello” into a sublist to become “l” - 1st L

In a high level programming language

A
  • substring - Returns part of a string, starting at the character of the first parameter and the ending position in the second parameter

stringname.subString(startingPosition, endingPosition)

string=”Hello”
chars = string[2:3] - index starts at 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does ord(…) do

A

ord (…)
Returns the ASCII value of a character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a program which returns the ASCII value of the character “A” - in a high level programming language
what would the output be

A

ascii = ord(“A”)
print(ascii)

ord(character)

outuput - 65

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does chr(…) do

A

chr (…) Returns the character from the ASCII number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

write a program which returns the character from the ascii value “65” - IN A HIGH LEVEL PROGRAMMING LANGUAGE
what would the output be

A

char = chr(65)
print(char)

output
A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what will be the output of this program

someText=”Computer Science”
print(len(someText))
print((someText[3:7].upper())

A

Program will display

16
PUTE

18
Q

How to write data to a file

A

Open the file for creating/overwriting or appending to a file

Write data to a file

Close the file

19
Q

What are the stages of reading data from a file

A

Open the file for reading data

Assign a boolean variable to “false” to indicate the end of the file is not reached

While the end of the file is false and the search item is not found:
Read the file from data
If the data matches what is being searched for, assign the data to variable or output
Check if the end of the file has been reached and assign the Boolean variable to “True”

Close the file

20
Q

In python, what do you write to open a file, with the intention to read from the file

A

f = open(“characters.txt, “r”)

f - is a variable
The open command takes two parameters:

“characters” - the name of the file to open

“r” - what we want to do with the file e.g. read

21
Q

In python how do you indicate that you have not reached the end of the file

A

end_of_file = False
or
while not end_of_file:

  • end_of_file is a variable
22
Q

In python, what do you write to read a file

A

f = open(“characters.txt, “r”)

….

name=f.readline().strip()
health=f.readline().strip()

name and health are variables

f - was assigned earlier as being a pointer to characters.txt

.strip() - this strips the new line character - so we just get the name/ the thing we want. the new line character is there, as when writing the file - we were pressing enter - the enter is stored as a character - to signify a new line

23
Q

In python how do you close the file

A

f.close()

f is just a variable
f is just pointing to characters.txt

f is the thing connecting our program to our text file

24
Q

In python how do you open a file, with the intention to write

A

f = open(“characters.txt”, “a”)

The open command takes two parameters

“characters.txt” the name of the file we want to connect to
“a” - means we want to append (add) to the end of the file

“w” - this would create a new file or overwrite contents of an existing file

25
How do we show in python that we want to start a new line
e.g. "hello" \n - means start a new line
26
In python, what do you do to write to a file
f.write(name+"\n") f.write(health+"\n") name and health are variables
27
In OCR Exam Reference language how do you open a file
myFile = open("sample.txt") myFile is a variable
28
In OCR Exam Reference language how do you close a file
myFile.close() myFile is a variable
29
In OCR Exam Reference language how do you read a line from the text file
x = myFile.readLine() x - program assigns x to be the first line of sample.txt .readLine - is the read line command
30
In OCR Exam Reference language how do you write to a file
myFile.writeLine("Hello World"
31
In OCR Exam Reference language how do you determine the end of the file
myFile.endOfFile() endOfFile() is used to determine the end of the line/
32
In OCR Exam Reference language how do you determine you are not at the end of the file
NOT myFile.endOfFile() myFile - is a variable
33
What is data dependency
Data dependency is when a program requires the items from the text file to be in a very certain order.
34
what is sql used for
SQL is used to create, delete, modify and manipulate records in a database
35
basic commands of sql
SELECT – (tells us) which fields (are) to be returned. (An asterisk * can be used to indicate all fields) FROM – (tells us which table of the database do we want to extract information from) which table. Databases can have more than one table, each with their own unique name WHERE – records meet a condition. The keyword LIKE can be used as a wildcard.
36
what is an array
An array is a static number of related data items that are stored together in the same memory space Each data item has the same data type
37
HOW IS A PARTICULAR ITEM IN A LIST FOUND
The particular data item (element) is found using its index
38
TWO TYPES OF SUB PROGRAMS
Procedures Carry out a task Provide structure to code Functions Carry out a task AND return a value Create reusable program components
39
ADV. OF USING SUB PROGRAMS
he program is easier to write The program is easier to debug We are creating reusable components Functions could be gathered together into a library for easy reuse across multiple programs An example is import random This imports the "random" library of functions into our program so we can use them. The program is easier to test (if we block our program into sub programs, - this is because we can test each procedure or function individually on its own and when we know it works, we can put it into a larger overall program)
40
what are subroutines
Larger programs are developed as a set of sub-programs called subroutines
41
Examples of situations when you might want to generate a random number
Simulating the roll of a dice Generating a random set of co-ordinates Gambling simulations National lottery program Quiz programs (selection a random question from a list of questions) Cryptography