2.2.3 Additional programming techniques Flashcards
What is File Handling?
File Handling is a way of storing information
What is the code to open a file?
file = open (“document.txt” , “w”)
file - keyword, assign to operation
open - reserve word to open a document
document - document name
w - operation
What does w, a and r mean?
w - write
a - append
r - read
What is the code so it will write your name and age into the file?
file.write (name)
file.write (age)
What is the code to close the document?
file.close()
What can you not write into a text file?
int
Ask 10 students to enter their preferred meal for parents evening
for i in range (10):
name = str(input(“What is your name?”))
meal = str(input(“Do you want rice, chicken, pasta or meatballs?”))
file = open(“meal.txt” , “a”)
file.write (name + “,”)
file.write (meal)
file.write (“\n”)
file.close ()
What does file.write (“\n”) mean?
It means new line and you have to apply it to the end
What is an array?
- A set of values that are all stored under one identifier (name)
- eg. values = [10,15,30,40,42,35]
0 1 2 3 4 5
What does index mean?
The position where values sit in the array (0 index array)
What is an example of an array?
Values = [10,15,30,40,42,35]
print (values[0]) = 10
print(values[3]) = 40
print(values[0:3]) = 10,15,30,40
How do you print all the numbers in an array?
for i in value:
print(i)
or:
for i in range (len(value)):
print(value[i])
What is Libraries/Modules?
Built in function that can be controlled to do a task
How do you do a random generator?
import random (need to import random)
rdn =random.randint(1,10) -> between the 2 values
can call ‘rdn’ whatever you want
randint = random integer
What is a subroutine?
A small block of code that is part of a big code and can be called to perform a task
What is a function?
A subroutine that will perform a value back to the program
What is a procedure?
A subroutine that will not return a value back to the program
What is a parameter?
Value being passed into a subroutine
How do you code a function?
Function(def) add(num1,num2):
ans = num1+num2
return(ans)
output = add(15,6)
print(output)
How do you code an area of a rectangle as a function?
Function area_rec (length,width):
area=(leangth*width)
return(area)
output=area_rec(5,6)
print(output)
How do you code a procedure?
Procedure(def) add(num1,num2):
ans=num1+num2
print(ans)
What is an SQL?
- Structured Query Language
- Used with databases - > question and return answers from a database
What is Field?
- A single piece data to be collected in a database
What is Record?
- A collection of fields