Applied Programming/Variables

This lesson introduces variables, expressions, and statements.

Objectives and Skills

edit

Objectives and skills for this lesson include:[1]

  • 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

edit
  1. Wikipedia: Expression (computer science) - includes constants, variables, operators, and functions
  2. Wikipedia: Data type
  3. Wikipedia: Statement (computer science)
  4. Wikipedia: Assignment (computer science)
  5. Wikipedia: Order of operations
  6. Wikipedia: Input/output
  7. Wikipedia: Coding conventions
  8. Wikipedia: Self-documenting code

Multimedia

edit
  1. YouTube: Introduction to Variables
  2. YouTube: Programming/Scripting Concepts Explained (Variables, Arrays, Strings, & Length)
  3. YouTube: Programming For Beginners - Variables
  4. YouTube: Programming For Beginners - Data Types
  5. YouTube: Duck Typing
  6. YouTube: Introduction to Programming - Basics
  7. YouTube: Declaring and using variables and constants
  8. YouTube: Performing arithmetic operations
  9. YouTube: CodeTips: What is Self-Documenting Code?
  10. YouTube: Variables - Coding Basics

Examples

edit

Activities

edit

Programming Languages

edit
  1. 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.
  2. 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.
  3. 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

edit

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.

  1. 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

edit
  • A variable is a named memory address paired with an identifier.[2]
  • The value contained inside a variable is mutable and can be overwritten during the execution of the program.[2]
  • In strongly typed languages, the variable is permanently associated with a specified data type upon declaration.[3]
  • 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.[4]
  • 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.[4] Sticking to your language’s coding conventions will also help other programmers understand your work more easily.[5]
  • An expression uses values, constants, variables, operators and functions to produce another value.[6]
  • The returned value computed by an expression can be of primitive data types as well as complex data types.[6]
  • Expressions statements evaluate an expression when terminated, typically by a semicolon.[6]
  • Order of operations (Also referred to as Precedence Rules[7]) is a set of rules that dictate which order to perform procedures when evaluating an equation.[8]
    • PEMDAS (parenthesis, exponents, multiplication, division, addition, subtraction) is the default order.[8]
    • These rules allow for the use of parenthesis to force a desired order of operations.[8]
    • An exception is the minus (-) symbol where it has highest precedence in certain applications like Excel.[8]
  • Data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.[9]
    • Data types include: integers, booleans, characters, floating-point (or real) numbers and alphanumeric strings.[9]
  • A programmer might create a new data type named "complex number" that would include real and imaginary parts.[9]
  • Inputs are the signals or data received by the system and outputs are the signals or data sent from it.[10]
  • I/O devices are the pieces of hardware used by a human (or other system) to communicate with a computer.[10]
  • An I/O interface is required whenever the I/O device is driven by the processor.[10]
  • Channel I/O requires the use of instructions that are specifically designed to perform I/O operations.[10]
  • 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.[10]
  • A constant is a value that cannot be altered by the program.[4] To visually differentiate them from variables, your language’s style guide may recommend different casing for both.[11]
  • Constants are useful for self-documenting code and for allowing correctness.[4]
  • There are three ways to express a data value that cannot be altered: [4]
    1. literal
    2. macro
    3. 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.[4]
  • It is common for some programming languages such as Ruby and Python to use capitals and underscores for constants.[4]
  • Coding conventions are common practices and guidelines aimed to unofficially standardize the structure and coding style for the specific programming language.[12]
    • Consistency in indentation, comments, declarations, line length, statements, white space, naming conventions will help improve readability, maintenance and reduce time.[12]
    • Some guides are published by the creators of the language. For example, PEP8 for Python.[13]
    • 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.[14]
    • 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.[15]
    • 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. [16]
  • A statement is a syntactic unit that expresses some action to be carried out.[17]
  • Examples of simple statements can include, but are not limited to:[17]
    • Assertion Statements
    • Assignment Statements
    • Goto Statements
    • Return Statement
    • Function/Module Call statements
  • Examples of compound statements can include, but are not limited to:[17]
    • Block Statements
    • Do-Loop Statements
    • For-Statements
    • If-Statements
    • While-Loop Statements
  • Programming languages are characterized by the type of statements they use.[17]
  • Most statement parameters are call-by-name parameters which are evaluated when needed.[17]
  • Statements do not return results and are executed solely for their side effects.[17]
  • 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.[17]
  • 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.[18]
  • 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.[18]
  • Augmented assignment is where the assigned value depends on a previous one like *=, so a=2*a can instead be written as a*=2.[18]
  • A chained assignment is when the value of one variable is assigned to multiple other variables.[18]

Key Terms

edit
array
A list or group of similar types of data values that are grouped.[19]
assignment
Sets the value saved in the storage location denoted by a given variable name.[3]
Boolean
A data type having two values, typically denoted true and false.[20]
constant
A value that cannot be altered by the program during normal execution.[21]
data type
A classification of data which tells the compiler or interpreter how the programmer intends to use the data.[22]
declaration
A language construct that specifies the properties of a given identifier.[23]
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.[24]
floating point
The formulaic representation that approximates a real number to a fixed amount of significant digits.[25]
integer
A number that can be written without a fractional component.[26]
modulus
The remainder after division of one number by another.[27]
objects
A combination of related variables, constants and other data structures which can be selected and manipulated together.[28]
operator
A programming language construct that performs a calculation from zero or more input values to an output value.[29]
order of operations
A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[30]
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.[31]
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.[32]
real number
A value that represents a quantity along a line, including integers, fractions, and irrational numbers.[33]
statement
The smallest standalone element of an imperative programming language that expresses some action to be carried out.[34]
string
A sequence of characters, either as a literal constant or as some kind of variable.[35]
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.[2]

See Also

edit

References

edit
  1. Microsoft: Exam 98-381 Introduction to Programming Using Python
  2. 2.0 2.1 2.2 Wikipedia: Variable (computer science)
  3. 3.0 3.1 Wikipedia: Type safety
  4. 4.0 4.1 4.2 4.3 4.4 4.5 4.6 Wikipedia: Self-documenting code
  5. "Coding conventions". Wikipedia. 2018-03-16. https://en.wikipedia.org/w/index.php?title=Coding_conventions&oldid=830687922. 
  6. 6.0 6.1 6.2 Wikipedia: Expression (computer science)
  7. "Order of operations". Wikipedia. 2018-03-14. https://en.wikipedia.org/w/index.php?title=Order_of_operations&oldid=830361255. 
  8. 8.0 8.1 8.2 8.3 Wikipedia: Order of operations
  9. 9.0 9.1 9.2 Wikipedia: Data type
  10. 10.0 10.1 10.2 10.3 10.4 Wikipedia: Input/output
  11. "Naming convention (programming)". Wikipedia. 2018-02-19. https://en.wikipedia.org/w/index.php?title=Naming_convention_(programming)&oldid=826478696. 
  12. 12.0 12.1 Wikipedia: Coding conventions
  13. "Python (programming language)". Wikipedia. 2018-03-16. https://en.wikipedia.org/w/index.php?title=Python_(programming_language)&oldid=830690084. 
  14. PEP 8 - Style Guide for Python Code
  15. PEP 8 - Style Guide for Python Code
  16. [1]
  17. 17.0 17.1 17.2 17.3 17.4 17.5 17.6 Wikipedia: Statement
  18. 18.0 18.1 18.2 18.3 Wikipedia: Assignment
  19. "Top Programming Terms and Definitions for Beginners [Updated]". Hackr.io. Retrieved 2021-01-22.
  20. Wikipedia: Boolean data type
  21. Wikipedia: Constant (computer programming)
  22. Wikipedia: Data type
  23. Wikipedia: Declaration (computer programming)
  24. Wikipedia: Expression (computer science)
  25. Wikipedia: Floating point
  26. Wikipedia: Integer
  27. Wikipedia: Modulo operation
  28. "Top Programming Terms and Definitions for Beginners [Updated]". Hackr.io. Retrieved 2021-01-22.
  29. Wikipedia: Operation (mathematics)
  30. Wikipedia: Order of operations
  31. "Top Programming Terms and Definitions for Beginners [Updated]". Hackr.io. Retrieved 2021-01-22.
  32. "Top Programming Terms and Definitions for Beginners [Updated]". Hackr.io. Retrieved 2021-01-22.
  33. Wikipedia: Real number
  34. Wikipedia: Statement (computer science)
  35. Wikipedia: String (computer science)