Statements: Declarations and Assignments

Introduction

We are now at a point where we can start to write more complex instructions. To summarize what we are currently able to do:

  • Create Variables
  • Get user input from the keyboard
  • Investigate memory addresses of variables
  • Choose the best data type for the job
  • Create and work with arrays
  • Create and use functions
  • Perform Binary Arithmetic / Logic operations
  • Construct Expressions with a variety of operators

Now, we will round out your foundational knowledge with learning about Statements, and finally looping and control structures. In this section, we will start off by considering two types of structures: Declarations, and Assignments.


Statements

As has already been reiterated, programming involves creating sequential instructions for the computer to process. In each language, the individual instructions are delimited in various ways. In C-style languages like C++, C#, Java and PHP, the instructions are delimited by semi-colons (;). In Python, however, we delimit instructions by placing one per line. These instructions, what we will be referring to as statements, are executed sequentially, and asynchronously.

Statements can be broken into a number of categories:

  1. Declarations. Creation of variables.
  2. Assignments. Define a data-type value, defined by an expression, with a variable.
  3. Conditionals. Selectively execute blocks of statements depending on the evaluation of a binary arithmetic expression.
  4. Looping Structures. Repetitively execute a block of statements while a binary arithmetic expression returns true.
  5. Function Calls and Returns. Jump to a set of statements at another location in the program by calling a function, then jump back by the return statement.

We will begin by considering Declarations and Assignments.


Declarations and Assignments

Declarations

Recall from a previous section on variables, that a variable is made up of an identifier, pointer, data type and value. A declaration is a statement which associates a data type and identifier to a variable.

In many programming languages, it is necessary to precede the assignment of a value to a variable with a declaration statement, which would prepare (or initialize) the variable to be ready to receive data. In Python, however, variables are automatically initialized when first used.

Assignments

An assignment statement takes an initialized variable, and associates a value of a data type to the variable of the same data type. This is done using an expression with the assignment operator "=".

Note: Please be aware of the difference between the assignment operator "=" and the equivalence comparison operator "==". The equivalence operator "==" is used in binary arithmetic to determine if the two terms on either side of the operator are equivalent. Whereas the assignment character "=" is used to write the value on the right-hand side of the operator into the memory block which the variable on the left-hand side points to. This distinction is important, as if you use the assignment operator where you mean to use the equivalence operator, the expression will result in true, as the assignment operation runs and returns true once complete.

Use in Python

x = 4
my_string = "Hello, World!"
price = 40.99

In the above examples, you will notice that they are all expressions, meaning they are comprised of an operator with a term on either side which can be either a variable or a literal. However, what defines these expressions as assignment statements is the fact that:

  1. The first term is a variable,
  2. The operator is the assignment operator "=", and
  3. the second term is either a literal or a function call

In Python, we do not have to first initialize the variable with a declaration statement before using the variable in an assignment. When we first assign a value to the variable, Python initializes the variable with the identifier used and the data type of the value being assigned.


Exercises

Write an assignment statement for the following data types:

  1. Boolean
  2. Array
  3. Integer

Continue to next section


Comments

comments powered by Disqus