Level 3 Computing Year 2 Lecturer: Phil Smith
Previously We learnt what a computer program does. What a procedural program does. What a procedure is. We had a first look at IDLE.
Now Learning Outcomes 1. Understand the features of procedural programming. Procedures we need to add a bit more on to the definition from last lesson. Functions and pre defined functions. Variables and variable scope. Parameters. Modularity. Programming libraries. Control structures (iteration). Conditional commands (selection).
LO1 Features We start by learning about variables. 1. Principles 2. Design 3. Syntax 4. Implementation 5. Scope
Variables In order to process input the information must first be stored in memory. For this all program languages use variables.
Variables Input variables In memory (RAM) Could be the same Output variables
What is a variable? Variables In Python we define a variable like this myvariable Instance variable names should be all lower case. Words in an instance variable name should be separated by an underscore It should have a meaningful name and be as short as possible. It should have a datatype.
Variables Watch out for this little gotcha Python is case sensitive?? So myvar is a different variable to myvar This will catch at least one of you out. (It has me)
Variable Data types All variables have a type e.g. String, Integer, Decimal, Float etc These data types determine the type of data that can be placed into the variable and also defines the amount (size) of memory needed to store the data. Python provides a wide range of data types. https://docs.python.org/2/library/datatypes.html
Variable Data types All values have a data type (a catogory of values) and every value belongs to a single data type. In Python, data types are inferred from the type of data assigned to the variable. E.g myvar = 42 is a string data type Myvar = 42 is an int data type so "42" (str) is not the same as 42 (int)
Variable String Data type Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from 1 at the end.
Variable Data types Activity numbers and strings Start IDLE on your PC
Variable List Data types Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type. Example list = [1, 2, 3, 4, 5, 6, a, b, c ] or Small_list = [123, Phil ]
Activity on lists Variable Data types Start IDLE on your PC
Variable assignment If you type 22 + 10 in idle the result displayed will be 32. This is an example of a code statement. If you type answer = 22 + 10 in IDLE then 32 is stored inside the variable answer, its data type will be int. When there is an assignment in the statement then this is called an expression. In IDLE you can then enter the variable name and the result is displayed.
Variable assignment So how can we enter a number of statements/expressions to be executed all together rather than one statement at a time (as in IDLE) Let s explore this option and also write a code sequence to show input, process and output.
Start IDLE on your PC Activity Then follow my lead.
Summary What have we learned today?