Python Moodle Flashcards
What is an algorithm?
A step by step sequence of instructions that if followed exactly will solve the problem under consideration.
Algorithms are like recipes: they must be followed exactly, they must be clear and unambiguous, and they must end.
What is a program?
A sequence of instructions that specifies how to perform a computation.
The differences between natural and formal languages include:
ambiguity, redundancy, and literalness
Source code is another name for:
the instrucions in a program, written in a high-level language.
What is the difference between a high-level programming language and a low-level programming language?
It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
What is the difference between a compiler and interpreter?
Interpreter reads a high-level program line by line and performs computations.
A compiler reads the high-level program (soucre code) and translates it into object code. The object code is then executed without further translation.
Interpreter: line by line translation
Compiler: translates the whole book
What is debugging?
tracking down programming errors and correcting them
What type of errors extist and explain?
- Syntax Error
syntax = structure of the program
Syntax errors are mistakes in using the language. Examples of syntax errors are missing a comma or a quotation mark, or misspelling a word. This is typically found by the compiler/interpreter. - Runtime Error (=excpection)
Runtime, or execution-time, errors are found when a script or function is executing (=found by the interpreter). Ex. divide by 0. - Semantic Error (=logical error)
semantic = meaning of a program
A semantic error is a mistake in reasoning by the programmer, but it is not a mistake in the programming language (=can only be found by the programmer)
What is a token?
One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.
Python is a
- general-purpose programming language
- scripting language
- interpreted language
What is the order of the program execution process?
- Write the code
- Compile
- Debug
- Execute the compiled and debugged code
In a nutshell what are the key components of a computer program?
Input
Operations
Control Structures
Ouput
What is the truncated division operator?
ex. 9//5 = 1
–> ignores the remainder
What is the modulus operator?
ex. 7 % 3 = 1
1 % 5 = 1 (5*0 + 1)
–> keeps only the remainder
What are keywords?
Keywords define the language’s syntax rules and structure, and they cannot be used as variable names,
e.g. and, def, class, True, try, except, global
What is increment, bumping, and decrement?
increment: adding something to a variable
bumping: increment a variable by 1 ( x+= 1)
decrement: subtracting something to a variable
What to watch out for when using the input function?
Input function returns a string value
What to watch out for when choosing a variable name?
- begin with a letter
- no illigeal characters (+,$,..)
- no keywords
Write the Python code that displays the value of a,b,c in one line with the ‘+’ operator:
a = int(input(“please enter a number”))
b = input(“please enter a sentence”)
c = ‘the input sentence is: ‘
In order to use the + first, we have to make sure that all the variables have the same data type.
The value of b and c is a string but the value of a is an integer so we need to convert it to a string by using str().
print(str(a) + b + c)
Important difference between string and list?
Lists can be changed while strings are immutable.
How do we call a string that contains no characters?
empty string
What is the difference between lists and tuples?
lists:
[10, 20, 30, 40]
[“spam”, “bungee”, “swallow”]
mutable
tuples:
julia = (“Julia”, “Roberts”, 1967, “Duplicity”, 2009, “Actress”, “Atlanta, Georgia”)
immutable
How do you access the last character or middle character of the string fruit = “grape”?
lastchar = fruit[-1]
lastchar = fruit[len(fruit) - 1]
midchar = fruit[len(fruit) // 2]
What is a slice operator?
The slice operator [n:m] returns the part of the string starting with the character at index n and go up to but not including the character at index m.