Lectures 1 to 4 - Revision Flashcards
What is a STATEMENT?
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.
What is a LITERAL?
A LITERAL is a fixed value directly typed into code
What is a Data Type?
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
What is an integer data type?
The integer data type represents whole numbers. Whole numbers have no decimal fractional part. The numbers can be positive, negative or zero
What is a float or floating point data type?
The floating point (float) data type represents numbers with a decimal fractional part. The numbers can be positive, negative or zero
What is an Operator?
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)
List the presedence for some common operators
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
What are the // and % Operators and when might you use them?
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.
What is a variable?
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 =
What are the naming rules for variable?
- 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
What is a String data type?
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
What doe you need to remember about the + symbol as an operator?
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 do you cast an integer to a string and vice versa?
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 can you repeat Strings?
The string repetition operator is *
e.g. print(‘abc ‘ *5) would be
abc abc abc abc abc
print(‘abc’ *5) would be abcabcabcabcabc
What is the List data type?
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
What is the Dictionary data type?
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
What are Logical operators?
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
List some logical operators with examples.
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
What is a function?
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.
List some common functions built in on python
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
More on Strings - Casting: How do you cast a string to a number (integer, float and long)?
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
More on Strings - Casting: How do you cast a number to a string (necessary for concatenation).?
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)
What is SLICING?
- 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
String Methods: How do you return a copy of a string with it’s first letter capitalized and the rest lowercased?
Once the variable ‘text’ has been created you use:
text.title()