Programming in Python Flashcards
Display data on screen
print ( )
Some examples:
print (“HelloWorld”)
print (“Name: “, name)
print (“5 + 3 = “, 5+3)
print (“Hello”, name, “welcome home”)
Input data from keyboard
input()
Some examples:
name = input()
name = input (“Enter name: “)
age = int(input(“Enter age: “))
cost = float(input(“Enter price: “))
Conditional statement
if <condition>:
<statements></statements></condition>
if <condition>:
<statements>
else:
<statements></statements></statements></condition>
Nested ifs
if <condition 1>:
<statements>
elif <condition 2>:
<statements>
elif <condition 3>:
<statements>
...
else:
<statements></statements></statements></statements></statements>
Fixed Loop
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)
Conditional Loop
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: “))
Creating an Array
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>
Writing to an Array
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, “: “))
Reading from an Array
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])
What are Parallel Arrays?
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
Declaring Parallel Arrays
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
What is modular programming?
Using modules to write code in easily manageable chunks that can be reused. Modules in Python can be either procedures or functions.
What is the difference between a function and a procedure?
A function returns a value back to the main program. A procedure does not.
How do you write a Procedure in Python?
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 do you write a Function in Python?
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 to call a procedure from the main program.
You simply write the name of the procedure…
<procedure> (<any>)
Examples:
displayData (nameArray)
calculateCakeCircumference (size)
inputDetails()
</any></procedure>
How to call a function from the main program.
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()
What is a predefined function?
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.
Which are the four predefined functions covered in Higher computing?
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
What is a record?
A record is a collection of information about one object. It is very similar to a record in a database table.
Declaring a record
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>
Creating an instance of a record (saving details in
a record)
<variable> = <record> (<details>)
Example:
patient1 = Person ("Matthew", 26, "12 Main Street", 184.4, 81.3)
</details></record></variable>
Displaying data from a record
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)
Which are the four steps to declaring an Array of Records
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