# Applied Programming/Variables

This lesson introduces variables, expressions, and statements.

## Objectives and Skills

Objectives and skills for this lesson include:
* Evaluate an expression to identify the data type assigned to each variable
** Identify str, int, float, and bool data types
* Perform data and data type operations
** Convert from one data type to another type; construct data structures; perform indexing and slicing operations
* Determine the sequence of execution based on operator precedence
** Assignment; Comparison; Logical; Arithmetic; Identity (is); Containment (in)
* Select the appropriate operator to achieve the intended result
** Assignment; Comparison; Logical; Arithmetic; Identity (is); Containment (in)
* Construct and analyze code segments that perform console input and output operations
** Read input from console; print formatted text; use of command line arguments

## Readings

* Wikipedia: Expression (computer science) - includes constants, variables, operators, and functions
* Wikipedia: Data type
* Wikipedia: Statement (computer science)
* Wikipedia: Assignment (computer science)
* Wikipedia: Order of operations
* Wikipedia: Input/output
* Wikipedia: Coding conventions
* Wikipedia: Self-documenting code

## Multimedia

* YouTube: Introduction to Variables
* YouTube: Programming/Scripting Concepts Explained (Variables, Arrays, Strings, & Length)
* YouTube: Programming For Beginners - Variables
* YouTube: Programming For Beginners - Data Types
* YouTube: Duck Typing
* YouTube: Introduction to Programming - Basics
* YouTube: Declaring and using variables and constants
* YouTube: Performing arithmetic operations
* YouTube: CodeTips: What is Self-Documenting Code?
* YouTube: Variables - Coding Basics

## Examples

* Bash
* C#
* Java
* JavaScript
* PHP
* Python3

## Activities
### Programming Languages

* Research different programming languages and select a programming language to use for this course. If unsure, Python3 is currently a popular choice. Based on your selected programming language, use Programming Fundamentals/Introduction#Examples and one of the free online IDE links provided to try running a Hello World program.
* Research free downloadable tools for your selected programming language (interpreter/compiler, IDE, etc.). Consider downloading and installing a development environment on your system. If you set up your own development environment, test the environment using the Hello World program.
* Review Wikipedia: Coding conventions Research style guides or coding standards for your selected programming language. If applicable, discuss coding conventions with your classmates and agree on a standard to follow for this course.

### Coding

Complete one or more of the following coding activities in your selected programming language. Use self-documenting variable and constant names that follow the naming conventions for your selected programming language. Include comments at the top of the source code that describe the program and are consistent with the program or module documentation conventions for your selected programming language.
* Review Wikipedia: Body mass index and MathsIsFun: Metric - US/Imperial Conversion Charts. Create a program that asks users for their weight in pounds and their height in feet and inches. Calculate and display their BMI. Format the output to one decimal place. Include a legend that displays value ranges for underweight, normal, and overweight. Be sure to indicate the source for your BMI range recommendations.

## Lesson Summary

* A variable is a named memory address paired with an identifier.
* The value contained inside a variable is mutable and can be overwritten during the execution of the program.
* In strongly typed languages, the variable is permanently associated with a specified data type upon declaration.
* In order to write self-documenting code, identifiers must be comprehensible and describe the purpose or function of a given object. For example, instead of variables x and y, you might have variables feet and inches.
* Self-documenting code needs to be readily understood and maintained by other and future programmers, even those without intimate knowledge of the project at hand. Sticking to your language’s coding conventions will also help other programmers understand your work more easily.
* An expression uses values, constants, variables, operators and functions to produce another value.
* The returned value computed by an expression can be of primitive data types as well as complex data types.
* Expressions statements evaluate an expression when terminated, typically by a semicolon.
* Order of operations (Also referred to as Precedence Rules) is a set of rules that dictate which order to perform procedures when evaluating an equation.
** PEMDAS (parenthesis, exponents, multiplication, division, addition, subtraction) is the default order.
** These rules allow for the use of parenthesis to force a desired order of operations.
** An exception is the minus (-) symbol where it has highest precedence in certain applications like Excel.
* Data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.
** Data types include: integers, booleans, characters, floating-point (or real) numbers and alphanumeric strings.
* A programmer might create a new data type named "complex number" that would include real and imaginary parts.
* Inputs are the signals or data received by the system and outputs are the signals or data sent from it.
* I/O devices are the pieces of hardware used by a human (or other system) to communicate with a computer.
* An I/O interface is required whenever the I/O device is driven by the processor.
* Channel I/O requires the use of instructions that are specifically designed to perform I/O operations.
* Memory-mapped I/O (MMIO) and port-mapped I/O (PMIO) (which is also called isolated I/O) are two complementary methods of performing input/output (I/O) between the central processing unit (CPU) and peripheral devices in a computer.
* A constant is a value that cannot be altered by the program. To visually differentiate them from variables, your language’s style guide may recommend different casing for both.
* Constants are useful for self-documenting code and for allowing correctness.
* There are three ways to express a data value that cannot be altered:
** literal
** macro
** constant
* We can define constants in a variety of programming languages with the following qualifiers const, final and read-only for uses in C/C++, Java and C# respectfully.
* It is common for some programming languages such as Ruby and Python to use capitals and underscores for constants.
* Coding conventions are common practices and guidelines aimed to unofficially standardize the structure and coding style for the specific programming language.
** Consistency in indentation, comments, declarations, line length, statements, white space, naming conventions will help improve readability, maintenance and reduce time.
** Some guides are published by the creators of the language. For example, PEP8 for Python.
** According to PEP 8 formatting, each line of code in Python should never exceed 79 characters. This is done to avoid wrapping lines and to make more code readable at once. Docstrings and comments should be limited to 72 characters. If a team unanimously agrees, the line limit can be extended to 99 characters as long as docstrings and comments are still capped at 72 characters.
** PEP 8 recommends using 4 spaces per indentation level. Spaces have to be chosen over tabs. Mixing tabs and spaces is not allowed in Python 3.
** Some libraries have their own style, but in general it is recommended to have a descriptive naming style using variations of snake case (words separated by underscores). More specific variations can be found on the PEP8 page.
* A statement is a syntactic unit that expresses some action to be carried out.
* Examples of simple statements can include, but are not limited to:
** Assertion Statements
** Assignment Statements
** Goto Statements
** Return Statement
** Function/Module Call statements
* Examples of compound statements can include, but are not limited to:
** Block Statements
** Do-Loop Statements
** For-Statements
** If-Statements
** While-Loop Statements
* Programming languages are characterized by the type of statements they use.
* Most statement parameters are call-by-name parameters which are evaluated when needed.
* Statements do not return results and are executed solely for their side effects.
* Most languages have a fixed set of statements defined by the language, but there have been experiments with extensible languages that allow the programmer to define new statements.
* An assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies a value into the variable.
* In an assignment, the expression is evaluated in the current state of the program and the variable is assigned the computed value, replacing the prior value of that variable.
* Augmented assignment is where the assigned value depends on a previous one like *=, so a=2*a can instead be written as a*=2.
* A chained assignment is when the value of one variable is assigned to multiple other variables.

## Key Terms
### array

    A list or group of similar types of data values that are grouped.

### assignment

    Sets the value saved in the storage location denoted by a given variable name.

### Boolean

    A data type having two values, typically denoted true and false.

### constant

    A value that cannot be altered by the program during normal execution.

### data type

    A classification of data which tells the compiler or interpreter how the programmer intends to use the data.

### declaration

    A language construct that specifies the properties of a given identifier.

### expression

    A combination of one or more explicit values, constants, variables, operators, and functions that a programming language interprets and computes to produce another value.

### floating point

    The formulaic representation that approximates a real number to a fixed amount of significant digits.

### integer

    A number that can be written without a fractional component.

### modulus

    The remainder after division of one number by another.

### objects

    A combination of related variables, constants and other data structures which can be selected and manipulated together.

### operator

    A programming language construct that performs a calculation from zero or more input values to an output value.

### order of operations

    A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.

### pointer

    A variable that contains the address of a location in the memory. The location is the commencing point of an object, such as an element of the array or an integer.

### program

    As an organized collection of instructions, which when executed perform a specific task or function. It is processed by the central processing unit (CPU) of the computer before it is executed.

### real number

    A value that represents a quantity along a line, including integers, fractions, and irrational numbers.

### statement

    The smallest standalone element of an imperative programming language that expresses some action to be carried out.

### string

    A sequence of characters, either as a literal constant or as some kind of variable.

### variable

    A storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

## See Also

* Computer Programming/Variables
* PlanetGeek: Comprehensive Guide to Self-Documenting Programs
* SlideShare: Examples of Primitive Data Types
* FreeCodeCamp: How to Think Like a Programmer
* FreeCodeCamp: How the let, const, and var Keywords Work in JavaScript
* LearnPython: Python Variables and Types
* Tutorials Point: Computer Programming - Variables