2 Programming Flashcards
What is declaration?
Declaration is telling the computer what the identifier (name) should be and what type of data should be stored for a variable.
What is assignment?
The allocation of data values to variables, constants, arrays and
other data structures so that the values can be stored.
what is a variable
Value that can change during the running of a program. By
convention we use lower case to identify variables (eg a=12)
what is a constant?
Value that remains unchanged for the duration of the program. By
convention we use upper case letters to identify constants. (e.g. PI=3.141)
what are data structures?
a data structure refers to a way of organizing and storing data in a computer so that it can be accessed and used efficiently. e.g. arrays, 2-D arrays, data types.
what do we use to show indices in python? e.g. if someone wants to find out the answer to 2^3
2 ** 3, which equals 8
What is input?
Input is collecting data, usually through the keyboard
it is data sent to a computer to be processed
What is input in pseudocode and in python?
psuedocode:
name <— USERINPUT
python:
name = str(input(“Enter your name: “))
psuedocode: variable = USERINPUT
What is output?
Output is putting data onto the screen, usually as text
it is processed info that is sent out from a computer
What is output in pseudocode and in python?
psuedocode:
OUTPUT “text”
python:
print(“text”)
What are the data types?
Character Real String Integer Boolean
What is Character data?
Character data is a single letter of text data
Eg. ‘a’
What is Real data?
Real data is decimal numbers.
Eg. ‘0.55’
What is String data?
String data is text data (multiple characters).
Eg. “Hello”
What is Integer data?
Integer data is whole numbers.
Eg. ‘12’
What is Boolean data?
Boolean data is a true or false value.
Eg. ‘True’ or ‘False’
What does the type of data determine?
The type of the data determines how it is stored and what you can do with the data.
What is casting?
Casting is the process of converting data from one type to another.
What are the reasons for casting?
One of the most common reasons for casting is output.
Output must be formatted as a string, and so we may need to convert a certain piece of data to a string.
All input also comes as a string, and must then be converted to other data types.
How do you cast to a string?
Casting to a string can be done by using the str function
Eg. str(3) gives “3”
How do you cast to an integer?
Casting to an integer can be done using the int function. Eg. int(3.4) gives 3
How do you cast to a real?
Casting to a real can be done using the real function.
Eg. real(“3.4”) gives 3.4
What are operators?
Operators are symbols that represent a specific function within a program
What are the Arithmetic operators?
Integer division
Modulus/modulo operator
Basic Operators
What is the integer division operator? what do we use to show integer divsion?
The integer division operator returns the quotient (whole part) of a division.
Eg. 5 DIV 2 would give 2. the word DIV is used in pseudocode.
the symbol for integer division in python coding is:
5 // 2 = 2
What is the Modulus/Modulo operator? what symbol do we use for modulus/modulo in python coding and in psuedocode?
The modulus/modulo operator gives the remainder of the division of two numbers.
Eg. 5 MOD 2 would be equal to 1. The word MOD is used in psuedocode.
The symbol in python coding is: 5 % 2 = 1
how do we do integer division, including remainders?
we would use both DIV and MOD. see pic
What are the basic operators?
Addition is done using a + sign. Subtraction is done using a - sign. Division is done using a / sign. Multiplication is done using a * sign. Exponentiation is done using a ** sign.
What is the relational operator?
Relational operators compare two values, and produce a True or False value.
What are the Equality operators?
We can test if two values are equal using the equality operator.
Eg. 4 == 4 is True
We can also test if two values are not equal using the not-equal-to operator
Eg. 4 ≠ 4 would evaluate to False. that symbol is used in psuedocode to show not equal to. <> is also used in psuedocode to show not equal to.
in python coding, the symbol for not equal to is !=
symbol for less than, greater than, less than or equal to and greater than or equal to in pseudocode and in python coding?
less than (in python and psuedocode): <
greater than (in python and psuedocode): >
less than or equal to (in python and psuedocode): <=
greater than or equal to (in python and psuedocode): >=
What are the Boolean operators?
AND
OR
NOT
What is the AND Boolean operator?
The AND boolean operator evaluates if both operands are True Eg. True AND True = True True AND False = False False AND True = False False AND False = False
What is the OR boolean operator?
The OR boolean operator evaluates to True if only one of the operands are True. Eg. True OR True = True True OR False = True False OR False = False False OR True = True
What is the NOT boolean operator?
NOT negates a logical value
Eg.
NOT True = False
NOT False = True
what do these evaluate to?
7 < 2 and 1 < 2
7 < 2 or 1 < 2
not 7 < 2
-> False
-> True
-> True
What is Sequence?
Sequence is when the computer follows a set of steps in the same order every time it is run.
Each line of code will have some
operation and these operations will be carried out in order line-by-line
in python and in psuedocode, how do you generate a random integer?
This also includes 1 and 10
python: import random #Generate a random integer between 1 and 10 random_integer = random.randint(1, 10) # 1 and 10 can be generated print("Random Integer:", random_integer) psuedocode: random_integer -> RANDOM_INT(0,9) OUTPUT random_integer
in python, how do you generate a random item from a list?
import random #Given a list, pick a random value my_list = [2, 5, 8, 10] random_choice = random.choice(my_list) print("Random Choice:", random_choice)
What is selection?
Selection allows us to have decisions made in our program
Selection represents a decision in the code according to some condition. The
condition is met then the block of code is executed otherwise it is not. Often
alternative blocks of code are executed according to some condition
Selection allows us to execute a section of code depending on whether a condition is met or not.
What is an If-statement?
If-statements is an easy way of checking if a condition is true
convert this to psuedocode:
~~~
if i > 2:
j=10
else:
j=3
~~~
IF i > 2 THEN j <--- 10 ELSE j <--- 3 ENDIF
convert this to python:
IF i ==2 THEN j <--10 ELSE IF i==3 j <-- 3 ELSE j <--1 ENDIF
if i ==2: j=10 elif i==3: j=3 else: j=1
Which shape is used to show a selection on a flow diagram?
Diamond symbol
What is Iteration?
Iteration allows a group of statements to be repeated multiple times. Iteration statements are often called loops.
What is definite iteration? give an example
Definite iteration repeats a block of code for a known number of time. For loops are an example. For loops are used when we know before hand the number of iterations we wish
to make.
convert to psuedocode:
for a in range(3): print(a)
FOR a ← 0 TO 3 OUTPUT a ENDFOR
What is indefinite iteration? give three examples.
Indefinite iteration is a block of code that will repeat while a specified condition is true. so we don’t know beforehand the number of iterations that will occur. three examples are: while loop, do…while and repeat…until. these loops are used when we do not know beforehand the number of iterations needed and the block of code will repeat while a specified condition is true.
convert to python and what would be the output?
a <-- 0 WHILE a < 4 OUTPUT a a <-- a + 3 ENDWHILE
a=0 while a<4: print(a) a=a+3
output:
0
3
use a nested for loop to print out a grid of x’s
see pic
Use a nested while and if
to print out only even
numbers until and including 50
see pic
Which shape is used in a flow diagram for iteration?
Diamond shape
What is a subroutine?
A subroutine is a names block of code within your program
What are the advantages of a subroutine?
- subroutines can be developed in isolation/independently/separately;
- easier to discover errors // testing is more effective (than without a
subroutine); - subroutines make program code easier to understand;
- subroutines make it easier for a team of programmers to work together on
a large project;
- easier to discover errors // testing is more effective (than without a
- subroutines make it easier to reuse code
How does a subroutine make code easy to read?
There are fewer long blocks of code to understand
How does a subroutine make the program more efficient?
Blocks of code only have to be created once but can be reused multiple times
How do subroutines make the program more reliable?
If there are bugs in the program, each subroutine can be tested individually to make sure it works
What are parameters?
Parameters are variables listed inside the parentheses in the function definition (see pic in key points in onenote)
parameters are used to pass data into functions
What are arguments?
Arguments are the actual values that are sent to the function when it is called (see pic in key points in onenote)
What are the two types of subroutine?
- Function
- procedure
What is a function?
A function is a subroutine which returns a value
A function is a named block of code that takes arguments, manipulates data and returns a value
functions have both input(s) and an output(s)
What is a procedure?
A procedure is a subroutine which does not return a value
a procedure is a named block of code that performs a set of instructions without returning a value.
a procedure either has an input or an output
what is the similarity and difference in the way procedures and functions are set out in Python
they are both declared using the def keyword, but a function has a return statement and a procedure does not. Examples:
# A function that returns the square of a number def square(x): return x * x # A procedure that prints "Hello, world!" def greet(): print("Hello, world!")
What is the scope of a variable?
The scope of a variable determines which parts of a program can access and use
that variable
What will be the scope of a variable defined within a subroutine?
Local scope
describe variables with a local scope
A variable with a local scope can only be accessed within the subroutine that it is defined in.
Local variables are not recognized outside a subroutine unless they are returned. There is no way of modifying or changing the behavior of a local variable outside its scope.
local variables only exist while the subroutine is executing
Once the execution of the subroutine ends, the local variable is destroyed, and its memory is released
What is a global variable?
A global variable is a variable that can be accessed by any part of the whole program
A global variable is a variable that can be used anywhere in a program.
they are defined outside of subroutines.
How can a variable inside a subroutine be made to have a global scope?
A variable can be made global by adding the ‘global’ keyword in front of it the first time it is used
see pic