Lectures 1 to 4 - Revision Flashcards

1
Q

What is a STATEMENT?

A

A STATEMENT is a syntactic unit in a programming language that represents an complete operation.

Syntactic refers to spelling and grammar. The words command and instruction are often (incorrectly) used interchangeably with the term statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a LITERAL?

A

A LITERAL is a fixed value directly typed into code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Data Type?

A

A data type defines how the data is stored in memory, what operations can be performed on the data and how the operations are performed. e.g. numbers can be integer data type or floating point (float) data types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an integer data type?

A

The integer data type represents whole numbers. Whole numbers have no decimal fractional part. The numbers can be positive, negative or zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a float or floating point data type?

A

The floating point (float) data type represents numbers with a decimal fractional part. The numbers can be positive, negative or zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an Operator?

A

An operator is a symbol or a keyword that causes computation to be performed on data so as to produce a result.
An operator performs on one or more operands (values or variables)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List the presedence for some common operators

A

The order of evaluation of operators is determined by precedence rules. Listed below:

Level Operator Descripition (from highest to lowest)
1. () Parenthesis
2. ** Power of
3. * / // % Multiplication, division
4. + - Addition, subtraction
5. < <= == != > >= > Comparison
6. not Logical not
7. and Logical and
8. or Logical or

Parenthesis have the highest priority, i.e. the operators within parenthesis are performed first.

Inner parenthesis are evaluated before outer parenthesis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the // and % Operators and when might you use them?

A

In Python, the % operator is known as the modulus operator.

After using integer division (//), which gives you the quotient of the division, you can use the modulus operator % to get the remainder of the division

e.g to calculate mintes in a number of seconds, first // by 60
then use % to get the remaining seconds.

remember the quotiant of the division is the whole number ignoring any remainder.
e.g, when you divide 10 by 3:
The integer division 10 // 3 gives you the quotient 3.
The modulus 10 % 3 gives you the remainder 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a variable?

A

A variable is data that is stored and can be used again later.
A variable is declared when it is created. A variable stores:
- the name of the variable.
- the data type
- it’s assigned value

The operator to assign a variable is =

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the naming rules for variable?

A
  • must start with a letter
  • can include numbers
  • can not include spaces
  • is case sensitive

Coding conventions (not python rules but standard convention)
- use underscores between words
- use lowercase letters for variables whose data changes
- use upper case for variables whose data doesn’t change (constants)

Avoid using similar names in a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a String data type?

A

The string type represents a sequence of characters. Strings are used for storing messages, names, etc. A literal string is denoted by quotation mark.

Single and double quotation marks are exchangeable.
The delimiting quotation marks are not stored as characters in the string. They simply indicate the start and end of the string.

Programmers use the following coding convention to improve readability (but Python understands both):
Single quotation marks are used for one word strings.
Double quotation marks are used for longer strings

To use quotation marks inside a srtring use the other type of quotation marks e.g print(“She says ‘Hello’”)

Strings can be stored as variables for later use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What doe you need to remember about the + symbol as an operator?

A

It has 2 meanings:

If the operands are strings then it concatenates them.

If the operands are integers it adds them

If one operand is a string and one an integer then you get a TypeError

There is a solution though - you can convert (cast) integers to strings and you can cast strings to integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you cast an integer to a string and vice versa?

A

To cast an integer to a string: use string function str()
e.g address + str(street_number)

To cast a string to an integer: int()

Then you can concatenate the strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you repeat Strings?

A

The string repetition operator is *

e.g. print(‘abc ‘ *5) would be
abc abc abc abc abc

print(‘abc’ *5) would be abcabcabcabcabc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the List data type?

A

The list type represents an ordered collection of elements.
The elements can be of any type.
For example a list of integers and a list of strings.
Can store mixed types of elements (e.g digits and names).
Lists can be stored in variables
Individual elements in a list can be accessed by assigning the list to a variable, then referencing the index in the variable (indices start at 0 at the left most end of the list).
Similar to arrays in other programming languages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Dictionary data type?

A

The dictionary type represents maps between keys and values.
Uses curly brackets to encase whole dictionary to show it is held in a dictionary not a list. e.g employees names and staff numbers
d = {‘Andy’: 478520, ‘Barbara’: 837402}

The key is the name and the value is the staff number.

To get Andy’s staff number you can look up his name:
d[‘Andy’] (square brackets)

The keys (what you serach on) must be unique within the dictionary.
The values do not need to be unique

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are Logical operators?

A

A logical / Boolean operator compares two numbers.
The result of the comparison is a True or False value.

The logical or boolean (bool) data type represents the True or False value.

The not operator inverts the input.

The and operator combines two logical expressions, the output is True if both inputs are True

The or operator combines two logical expressions, the output is: True if both or either of the inputs is True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

List some logical operators with examples.

A

3 == 4
Output will be True if 3 equals 4

3 != 4

Output will be True if 3 is NOT equal to 4

3 < 4
Output will be True if 3 is LESS than 4

3 > 4
Output will be True if 3 is GREATER than 4

3 <= 4
Output will be True if 3 is LESS than or EQUAL to 4

3 >= 4
Output will be True if 3 is greater than or equal to 4

19
Q

What is a function?

A

A function is a reusable block of code that performs a specific task.

A function:
has a name
can have inputs
can have outputs

To run it, call it’s name followed by parenthesis where you enclose the input values, seperated by commas, then close the parenthesis.

e.g max (1,5,3)
The output would be 5

Python includes a set of built-in functions.
Third-party functions are available.
You can also write your own functions.

20
Q

List some common functions built in on python

A

print() Displays the inputs to screen (inputs can be literal values or variables).

type() Displays the type of a literal value or variable (good for bug fixing)

str() Casts an integer to a string

int() Casts a string to an integer

max () Returns the largest input

21
Q

More on Strings - Casting: How do you cast a string to a number (integer, float and long)?

A

if you have a string that is
text = “1234”

int(text)
»>1234

int(text) * 2
»>2468

float(text)
»>1234.0

long(text)
»>1234L

You cannot cast a string of text that has a decimal point in to a integer. It needs to be cast to a float

22
Q

More on Strings - Casting: How do you cast a number to a string (necessary for concatenation).?

A

str()

can be an input value e.g

> > > yearsleft = 65 - age

str(yearsleft)

casting to a string allows concatenations (which you cannot do with integers)

23
Q

What is SLICING?

A
  • Slicing involves extracting sections from a String
  • Each character of a String has an index – the
    first is zero
    e.g. ‘PYTHON’
    P = index 0
    Y = index 1 etc

To extract single characters:
You create the text variable e.g
text = “PYTHON”
text[0] will output P
text[1] will output Y
text[2:5] will output index 2 onwards up to (but not including index 5). THO
text[:4] will output from the beginning up to (but not including index 4). PYTH
text[4:] will output from index 4 to the end. N

24
Q

String Methods: How do you return a copy of a string with it’s first letter capitalized and the rest lowercased?

A

Once the variable ‘text’ has been created you use:

text.title()

25
String Methods: How do you return a copy of a string with all letters in lower case?
Once the variable 'text' has been created you use: text.lower()
26
String Methods: How do you return a copy of a string with the first letter of each word capitalised?
Once the variable 'text' has been created you use: text.title() Remember that with these 3 methods the letter after any punctuation is treated as a new word e.g if text="you're welcome' text.title() would output You'Re Welcome
27
Boolean String Methods: Once the variable 'text' has been created, what will text.isdigit() return?
text.isdigit will return TRUE if all the characters in the string are digits AND there is at least one character (otherwise will return FALSE)
28
Boolean String Methods: Once the variable 'text' has been created, what will text.isalpha() return?
text.isalpha() will return TRUE if all the characters in the string are alphabetic AND there is at least one character (otherwise will return FALSE)
29
Boolean String Methods: Once the variable 'text' has been created, what will text.isalnum() return?
text.isalnum() will return TRUE if all the characters in the string are alphanumeric AND there is at least one character (otherwise will return FALSE)
30
Boolean String Methods: text = 'Hello123' what will text[2:].isdigit() return? and why?
FALSE because llo123 contains both digits and alpabetic characters.
31
Boolean String Methods: text = 'Hello123' what will text[5:7].isdigit() return? and why?
TRUE because 12 are both digits
32
Creating & Accessing Lists: What does creating a list do?
Creating a list creates a structure where elements are indexed starting at zero.
33
Creating & Accessing Lists: what is the statement to create a list. Use an example.
list = [10, 45, Carly, 12, Lee] in this example index zero has the value 10 index 1 has the value 45 index 2 has the value Carly index 3 has the value 12 index 4 has the value Lee
34
Creating & Accessing Lists: How do you access elements of a list?
list[0] will return the element at index 0 . In previous example this would be 10
35
Creating & Accessing Lists: How do you find out how many elements are in a list?
len(list) in the previous example it would be 5.
36
Creating & Accessing Lists: How do you add one element to then end of a list?
list.append() e.g. list.append(Blossom) This will add the element Blossom to the end of the list that was previously declared. e.g list = [10, 45, Carly, 12, Lee, Blossom]
37
Creating & Accessing Lists: How do you concatenate lists and what happens when you do?
Concatenation creates a new list which needs to be stored as a new variable if you want to acccess it again. example: e.g list = [10, 45, Carly, 12, Lee] list + [Alfie, 100] will create a new list which = [10, 45, Carly, 12, Lee, Alfie, 100]. However if you display your list using >>>list then your original list that was declared will be displayed [10, 45, Carly, 12, Lee] if you want to keep this new list then store as a new variable e.g nlist = [10, 45, Carly, 12, Lee, Alfie, 100]
38
Creating & Accessing Lists: How do you add multiple elements to a list?
list.extend([]) example: list.extend([Alfie, 100]) >>>list will output [10, 45, Carly, 12, Lee, Alfie, 100]
39
What is a 2-D list?
In a 2-D list, each item in the list is itself a list. i.e a list of lists. It is called a matrix matrix[0] is the first item (list) in the list They are stored in rows and collumns (rows accross and collumns down) The first row is row 0, the first collum is 0. A 2-D list can be called anything you wanrt (as long as it conforms to variable naming convention)
40
How do you create a 2-D list?
matrix = [ [ 1, 2, 3] [ 4, 5, 6] [ 7, 8, 9] ] so in this example the first item is list 1, 2, 3. Value of 1 is in row 0 collumn 0. the second item is list 4, 5, 6 etc Value of 4 is in row 2, collum 0.
41
How do I access elements within a 2-D list?
matrix[row][collumn] Using the above example. matrix = [ [ 1, 2, 3] [ 4, 5, 6] [ 7, 8, 9] ] matrix[1][0] would be value 4 matrix [2][1] would be value 8
42
Describe the key features of dictionaries
Dictionaries: * Similar to lists but not sequential. *They consist of a set of KEY VALUE PAIRS * Elements are accessed by KEYS. * A key can be a number or a string. * VALUES can be basic types (such as strings, ints, etc.) or can be complex types such as lists and dictionaries. * Dictionaries are similar to associative arrays or hashtables in other programming languages
43
How do I create a dictionary? Use an example
This example will have three sets of KEY / VALUE pairs person = {'Name': "Fergus", 'Age': 34 'Skills': ["Python", "perl", "SQL"]} The first key / value pair has a key of 'Name' and a value of "Fergus". The second key / value pair has a key of 'Age' and a value of '34'. The third key / value pair has a key of 'Skills' and the value is a list ["Python", "Perl", "SQL"] so "Python" is index 0 in the list, "Perl" is index 1, and "SQL" is index 2.
44
How do I access elements of a dictionary?
Using the same example of a dictionary created as person = {'Name': "Fergus", 'Age': 34 'Skills': ["Python", "perl", "SQL"]} To access a simple element give the dictionary name with the key in [ ] e.g. person['Age'] This will give the value of 34 To access an element of a complex structure use the key in [ ] followed by the index in [ ] e.g person['Skills'][0] returns "Python" To access the full list for that key use person["Skills"] i.e use the dictionary name followed by the key in square brackets