Lecture Notes Flashcards
(484 cards)
Define programming.
Programming means giving a computer a list of tasks, which it then runs in order to solve a problem.
What are some advantages of computer programming?
- Computers don’t get bored - automate repetitive tasks
- Computers don’t get tired
- Computers are calculators
- Computer code is reproducible
What can’t computers do?
- Computers are not creative
- Computers are not ethical
- Computers only know what you tell them
What are some advantages of python?
- High-level language
- Emphasises readability, making use of white space and indentation
- Dynamically typed
- Interpreted language
- Assigns memory automatically
- Supports multiple approaches to programming
- Extensive functionality
- Portable
- Open source
- Very popular
What are some disadvantages of python?
- Slower than compiled languages
- Can be memory-intensive
What are the different types of cells in a Jupyter notebook?
- Code cells - interpreted as Python code
- Markdown cells - for adding formatted text
How do you add a comment to a Jupyter notebook?
#
Why are comments important?
- Allow you to keep track of what your code does
- Avoids repetition and mistakes
- Easy for other people to follow
What steps should you take for debugging?
- Always read error messages carefully
- Comment your code thoroughly
- Tell your code to print outputs for intermediate steps
- Use the internet
How do you print in python?
print()
Prints whatever is in the brackets.
Useful for displaying results and testing purposes.
What does a variable have?
A name and a value.
The name is fixed, the value can change.
What are the different types of variables in Python?
- Numeric: integers, floats or complex numbers
- Text: string, always marked by quotation marks
- Boolean: True or False
- Sequences: lists or arrays of numbers/letters
How do you change the string x = ‘33.3’ to a float?
float(x)
How do you check the type of a variable?
type(x)
How do you change the a float to an integer?
int(x) - this roads it to a whole number
How do you get an input from the user?
variable = input(“Enter your name: “)
What is an expression?
Any group of variables or constants that together result in a value.
What are the common symbols used in basic math expressions?
*
/
% (remainder)
** (raise to the power of)
How do you concatenate two strings together?
String1 + String2
= String1String2
String1 * 3
String1String1String1
How is python indexed?
Zero-based indexing
What is string slicing?
Extracting certain characters from a string.
How do you access specific parts of a string?
Using the index with square bracket notation
- string[0]
Can we change a part of string in place?
We can access parts of a string to see their value, but we cannot change them in place - strings are immutable.
How do we access a sequence (sub-string) of any length?
By specifying a range to slice. Ranges use a : notation eg [1:10]
The slice occurs before each index (eg between 0 and 1 and 9 and 10)- returning characters 1-9.