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