Notes
Lecture 2
Content of the notes (1)
Page 1
Operators and operands
statements
programmes - branching, conditionals, iteration.
Need representation for fundamental data. - Numbers and strings and booleans... fundamentally all are these at atomic level.
Type - strings = strings
Numbers = integers and floats - value & type
Combine in expressions formed of operands and operators.
Interpreter evaluates and interprets. Using script will not print out all data but will store as variable. No print unless we make it explicit.
TYPE CONVERSION If I want to combine two things together in a particular way I need to give it the type of operands it expects. - SEMNATIC if operator expects types.
TYPE CHECKING - Python checked the type before running and flagged the error, not assuming how the operator should work - type checking helps. Different languages have weak to strong type checking. Python is on the strong end of the spectrum.
type discipline - get into the habit of checking what your operators do and be disciplined about making sure you use the right types.
/ = integer division. be careful. % gives remainder
3+4*5 - add or mult first? operator precedence - exp>*/>+- (like in real maths) when in doubt use ( )
Variables assigned with assignment statement eg x=3*5
Binding takes a variable name and links it to the value given. A LINK not a BOX.
Type of variable: inherits from its value. so if x=3 then x is an integer. Variable type is dynamic depending on the current value. Therefore don't change types arbitrarily or this can fuck up your code.
Use a variable ANYWHERE you can use the value.
Things we can do to manipulate: STATEMENTS
legal commands that Python can interpret.
eg Print
assignment
See handout
# identifies comment - to user not to Python. Good programming style says you use comments that will help you to understand reading the code. NEED TO BE UNDERSTAND IN FUTURE THEREFORE NEED TO USE COMMENTS
Choice of variable names - use names that make sense. eg firstname lastname not x y
VARIABLE NAMES CANNOT USE: 28 keywords excluded eg print - command to do something - cannot use as variable name.
Straight line programme - a programme in which we execute a seq of instructions one by one.
Conditional instructions - branching programmes (eg IF needed ADD sugar) - can change the order of instructions based on some test. Usually a value of a variable
IF statements. conditional. in x: the colon says about to begin a string of instructions treated as a block. : start carriage return is end. EG indented commands.
eg
if :
else print something different
== to equal because = is an ASSIGNMENT.
if :
block of instructinos
else:
another block of instructions
You can use this without the ELSE clause if all you want is to find out if something is true. OR just skip to the next command line.
IF can be nested to determine between 3+ variables etc.
when designing test cases need to have a test case for each possible branch to winkle out the bugs.
Elif - check both things - BOOLEAN - use value of AND OR NOT to test something - two values TRUE and FALSE
Branching means you don't do every line - comparative time is the same but different classes of algorithms have different efficiencies.
EG Beat egg whites until stiff - until == test.
ITERATION/LOOPS. use WHILE to give testing value. can use a loop to go round and round until something fits the test.
X Y itersleft
x==3 y==0 itersleft==3
IF you don't change the value of the test variable in the code it will just keep running forever. TEST NEEDS TO INVOLVE LOOP VARIABLE (INITIALISED OUTSIDE LOOP) THAT IS CHANGING.
New Page
Information
- Description
- Introduction to Computer Science and Programming (MIT) Note on Lecture 2, created by Maddy Seb on 08/05/2014.
- Created by
- Maddy Seb
- Views
- 21
- Rating