Programming in Python Flashcards

1
Q

Display data on screen

A

print ( )

Some examples:
print (“HelloWorld”)
print (“Name: “, name)
print (“5 + 3 = “, 5+3)
print (“Hello”, name, “welcome home”)

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

Input data from keyboard

A

input()

Some examples:
name = input()
name = input (“Enter name: “)
age = int(input(“Enter age: “))
cost = float(input(“Enter price: “))

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

Conditional statement

A

if <condition>:
<statements></statements></condition>

if <condition>:
<statements>
else:
<statements></statements></statements></condition>

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

Nested ifs

A

if <condition 1>:
<statements>
elif <condition 2>:
<statements>
elif <condition 3>:
<statements>
...
else:
<statements></statements></statements></statements></statements>

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

Fixed Loop

A

for <variable> in range (<start>, <end value + 1>):
<statements></statements></start></variable>

Examples:

for x in range (0, 10):
print (“Enter name: “)
name[x] = input()

for count in range (1, 21):
print (count)

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

Conditional Loop

A

while <condition>:
<statements></statements></condition>

Examples:

while password != “1234”:
print (“Incorrect password. Please try again.”)
password = input()

while choice != 3:
print (“Main Menu”)
print (“1. Input Data”)
print (“2. Display Data”)
print (“3. Exit”)
choice = int(input(“Please enter your option: “))

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

Creating an Array

A

Creating / Declaring an array:

<array> = [<data>] * <number>

Examples:

name = [""]*10

score = [0]*25

distanceArray = [0.0]*100

SubscriptionArray = [False]*200

You can also ask the user how many locations they want in the array and use that information to create the array, for example:

numOfLocations = int(input("How many people would you like to register? "))
name = [""] * numOfLocations
</number></data></array>

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

Writing to an Array

A

Normally to be efficient we use a for loop to read and write to and from an array.

To write to an array:

for <variable> in range (0, <number>):
<array> [<variable>] = input ("<message>")</message></variable></array></number></variable>

For example:

for x in range (0, 10):
name[x] = input (“Enter name: “)

for count in range (0, 25):
score[count] = int(input(“Enter score: “))

for c in range (0,100):
distanceArray[c] = float(input(“Enter distance for participant “, c+1, “: “))

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

Reading from an Array

A

Same as when writing to an array, to read efficiently from an array we need to use a for loop so something as follows:

for <variable> in range (0, <number>):
print (<array> [<variable>])</variable></array></number></variable>

Some examples:

for x in range (0,10):
print (name[x])

for count in range (0,25):
print (“Score for student”, count+1, “: “, score[count])

for c in range (0,100):
print (“Participant”, c+1, “: “, distanceArray[c])

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

What are Parallel Arrays?

A

Parallel arrays are just a number of arrays that have the same number of locations and are used together to store different details about the same object/s or people.

For example, to store the name, surname, age and class of 25 students you would have the following arrays that work in parallel:

name = [””]25
surname = [””]
25
age = [0]25
class = [””]
25

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

Declaring Parallel Arrays

A

To declare parallel arrays, it is the same as declaring a single array, but you will have more than one array to declare.

For example:

name = [””]25
surname = [””]
25
age = [0]25
class = [””]
25

You can also ask the user how many locations you need in the arrays, so for example:

numberOfPets = int(input(“How many pets would you like to register?”))
petName = [””] * numberOfPets
type = [””] * numberOfPets
breed = [””] * numberOfPets
age = [0] * numberOfPets
ownerID = [0] * numberOfPets

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

What is modular programming?

A

Using modules to write code in easily manageable chunks that can be reused. Modules in Python can be either procedures or functions.

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

What is the difference between a function and a procedure?

A

A function returns a value back to the main program. A procedure does not.

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

How do you write a Procedure in Python?

A

At the top of the page, above the main program, write:

def <procedure> (<parameters>):
<statements></statements></parameters></procedure>

Examples:

def displayData (array):
for x in (0, len(array)):
print (“Name: “, name[x])

def calculateCakeCircumference (diameter):
pi = 3.142
circumference = pi * diameter
print (“Cake circumference is “, circumference)

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

How do you write a Function in Python?

A

At the top of the page, above the main program, write:

def <function> (<parameters>):
<statements></statements></parameters></function>

Examples:

def calculateCakeCircumference (diameter):
pi = 3.142
circumference = pi * diameter
return circumference

def inputNames ():
for x in range (0, 100):
name[x] = input (“Enter name: “)
return name

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

How to call a procedure from the main program.

A

You simply write the name of the procedure…

<procedure> (<any>)

Examples:

displayData (nameArray)

calculateCakeCircumference (size)

inputDetails()
</any></procedure>

17
Q

How to call a function from the main program.

A

You need to store the value returned by that function into a variable, since functions are returning a value to the main program.

Example:

ribbonLength = calculateCakeCircumference (size)

area = calculateArea (length, breadth)

nameArray = inputNames()

18
Q

What is a predefined function?

A

A function that has been created and stored in a library and can be used in your program to carry out a specific process that you do not need to code yourself.

19
Q

Which are the four predefined functions covered in Higher computing?

A

split() - used to Create Substrings (i.e. to divide strings into smaller strings)
chr() - used to convert an ASCII value into a character
ord() - used to convert a character into its ASCII value
int() - used to convert floating point numbers to integer
% - modulus - returns the remainder part of a division

20
Q

What is a record?

A

A record is a collection of information about one object. It is very similar to a record in a database table.

21
Q

Declaring a record

A

from collections import namedtuple

<record> = namedtuple ("<record>", "<field name 1> <field name 2> <field name 3> ... ")

Example:

from collections import namedtuple

Person = namedtuple ("Person", "name surname age address height weight")
</record></record>

22
Q

Creating an instance of a record (saving details in
a record)

A

<variable> = <record> (<details>)

Example:

patient1 = Person ("Matthew", 26, "12 Main Street", 184.4, 81.3)
</details></record></variable>

23
Q

Displaying data from a record

A

print (<variable>.<field>)</field></variable>

Examples:
print (patient1.name)

print (“Patient name: “, patient1.name, patient1.surname, “\n”, “Age: “, patient1.age, “\n”, “Height: “, patient1.height, “\n”, “Weight: “, patient1.weight)

24
Q

Which are the four steps to declaring an Array of Records

A

This is a combination of arrays and records

Three steps:
1. Import namedtuple
2. Create a record
3. Create an empty instance of the record
4. Create an array with the empty instance of the record as its data type

Example:
from collections import namedtuple

Person = namedtuple (“Person”, “name surname age address height weight”)
emptyPerson = Person (“”, “”, 0, “”, 0.0, 0.0)
arrayOfPerson = [emptyPerson]*100

25
Inserting data into an array of records
Use a fixed loop to input as many records as needed. This is very similar to inserting data into a parallel array with only slight changes.
26
How to insert data into an array of records
for in range (0, ): = input() ... [] = (, , ...) Example: for count in range (0, 100): name = input ("Enter name: ") surname = input ("Enter surname: ") age = int (input ("Enter age: ")) address = input ("Enter address: ") height = float (input ("Enter height: ")) weight = float (input ("Enter weight: ")) arrayOfPerson [count] = (name, surname, age, address, height, weight)
27
What are the three steps to follow when reading from or writing to a file?
Open file Read / Write data Close file
28
How do you open a text or csv file when you are writing to it?
If you want to replace anything that already exists in the file and start over, you use "w" for write: = open (", "w") Examples: file = open ("fileName.csv", "w") textFile = open ("fileName.txt", "w") If you want to add the data to what already exists in the file without replacing it, you use "a" for append: = open (", "a") Examples: file = open ("fileName.csv", "a") textFile = open ("fileName.txt", "a")
29
How do you open a file to read from it?
= open (", "r") Examples: file = open ("fileName.csv", "r") textFile = open ("fileName.txt", "r")
30
How do you close a file?
.close() Examples: file.close() textFile.close()
31
What are the steps to write data to a text file?
Step 1: This is optional and only necessary if you have data that needs to be concatenated. Concatenate all data into one variable. Step 2: Write the data into the file.
32
How do you write data into a text file?
= + ... .write () Example: fileData = name + " " + surname + "\n" file.write (fileData)
33
How do you write data into a csv file?
When writing into a csv file, you need to make sure that you separate each cell content by a comma (,) and each record by a new line (\n). Same two steps as when writing into a text file are needed, i.e.: Step 1: concatenate all data into one variable Step 2: write the contents of the variable into the file = + "," + + ... + "\n" .write (variable) Example: for x in range (0,100): fileData = arrayOfPerson[x].name + "," + arrayOfPerson[x].surname + "," + arrayOfPerson[x].age + "," + arrayOfPerson[x].address + "," + arrayOfPerson[x].height + "," + arrayOfPerson[x].weight + "\n" file.write(fileData)
34
How do you read data from a text file?
= file.read() Example: fileData = file.read() You might need to split the data depending on how it is stored in the file.
35
How do you read data from a csv file?
= file.read() = .split ("\n") for in range (0, len(-1): = [].split(",") = [0] = [0] ... Example - storing data into parallel arrays: fileData = file.read() separateRecords = fileData.split("\n") for x in range (0, len(recordData)-1): recordData = separateRecords[x].split(",") name[x] = recordData[0] surname[x] = recordData[1] age[x] = int(recordData[2]) ... Example - storing data into an array of records: fileData = file.read() separateRecords = fileData.split("\n") for x in range (0, len(recordData)-1): recordData = separateRecords[x].split(",") name = recordData[0] surname = recordData[1] age = int(recordData[2]) ... arrayOfPeople[x] = Person (name, surname, age, ...)