Programming Flashcards
asking the user to input into a variable
age=input (how old are you?”)
print(“You are”,age)
age is the variabe
using IF to test a variable
if age < 18: print("You are a child.") else: print("You are an adult>") if - is the number in age less than 18 else - otherwise
sub-routines
the collective term for both procedures and functions
procedure
a piece of programming code which has a name and can be used/ called as often as needed. it should perform only ONE task.
function
similar but always returns an answer to the program. (procedures do not return an answer)
procedure example:
def hello(): print("Hello world")
hello()
def - all procedures start with ‘def’…
hello - followed by the name of the procedure followed by ():
all code inside the procedure must be indented
hello() - type the procedures name to run it. make sure to include brackets
when might functions be used?
if there is some sort of calculation is to be carried out, or some other piece of information is needed by the main program (e.g. a persons name, age, answer to a question)
function example
def getname(): name= .................... return name
name= getname()
…………….
return name - the function has a return statement which sends information back to the main program
name=getname () - when the function is called in the main program, you must save the information being returned into a variable otherwise it will be lost
what is a list?
a collection of data items grouped under the same variable.
the items in a list are numbered from zero
how to create a list
lessons = [“maths”,”PE”,”history”,”RE”]
use square brackets to create a list
how to print a list
print(lessons)
prints the whole list
print (lessons[3])
prints the fourth item(because it starts counting from 0
how to add to the list
lessons.append(“computer science”)
adds computer science to the end of the list
what is a while loop?
it can be used to repeat a section of code until something changes. this is useful if you don’t know how long you need the code to repeat for
while loop example
carryon=”Y”
while carryon ==”Y”:
print (“keep going”)
carryon=input(“Do you want to carry on (Y/N)
create a variable and put the letter Y in it
while carryon is equal to Y, keep priting”keep going”
you have to cose an oppurtunity for the code to change otherwise it will go forever
variable
a named location within the computers memory to store a single value.