Applied Programming/Variables

From Wikiversity
Jump to navigation Jump to search

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: Introduction to Programming - Basics
  6. YouTube: Declaring and using variables and constants
  7. YouTube: Performing arithmetic operations
  8. YouTube: Python3 Input & Output
  9. YouTube: What Language Should I Choose?
  10. YouTube: Self-Documenting Code

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.
  2. Review How To - Fuel Calculation and Fuel economy. Create a program that asks the user to enter current fuel price at their local gas station (Regular 87), mpg (mile per gallon) of their vehicle or any other vehicle they choose. Calculate how much it would cost to travel 500 miles, and how much fuel would be used. Convert mpg to L/100Km, using the following formula: mpg to l/100km
  3. Create a program that displays a legend of letter grades to their number equivilant on a four point GPA scale(i.e "A = 4"), and then asks the user to enter numbers for four grades. Calculate and display their GPA rounded to two decimal places.
  4. Review the rules for Battleship. Create a program that asks users for two player names, a set board size, set amount of ships, and ship placement inputs. Then have the program format and display the users' inputs as follows:
        Player 1: John White has following ships:
            Destroyer (A4,B4)
            Submarine (G5,G6,G7)
            ...

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]
  • A statement is a syntactic unit that expresses some action to be carried out.[14]
  • Examples of simple statements can include, but are not limited to:[14]
    • Assertion Statements
    • Assignment Statements
    • Goto Statements
    • Return Statement
    • Function/Module Call statements
  • Examples of compound statements can include, but are not limited to:[14]
    • Block Statements
    • Do-Loop Statements
    • For-Statements
    • If-Statements
    • While-Loop Statements
  • Programming languages are characterized by the type of statements they use.[14]
  • Most statement parameters are call-by-name parameters which are evaluated when needed.[14]
  • Statements do not return results and are executed solely for their side effects.[14]
  • 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.[14]
  • 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.[15]
  • 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.[15]
  • Augmented assignment is where the assigned value depends on a previous one like *=, so a=2*a can instead be written as a*=2.[15]
  • A chained assignment is when the value of one variable is assigned to multiple other variables.[15]

Key Terms[edit]

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.[16]
constant
A value that cannot be altered by the program during normal execution.[17]
data type
A classification of data which tells the compiler or interpreter how the programmer intends to use the data.[18]
declaration
A language construct that specifies the properties of a given identifier.[19]
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.[20]
floating point
The formulaic representation that approximates a real number to a fixed amount of significant digits.[21]
integer
A number that can be written without a fractional component.[22]
modulus
The remainder after division of one number by another.[23]
operator
A programming language construct that performs a calculation from zero or more input values to an output value.[24]
order of operations
A collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression.[25]
real number
a value that represents a quantity along a line, including integers, fractions, and irrational numbers.[26]
statement
The smallest standalone element of an imperative programming language that expresses some action to be carried out.[27]
string
A sequence of characters, either as a literal constant or as some kind of variable.[28]
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" (in en). 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" (in en). 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)" (in en). 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)" (in en). Wikipedia. 2018-03-16. https://en.wikipedia.org/w/index.php?title=Python_(programming_language)&oldid=830690084. 
  14. 14.0 14.1 14.2 14.3 14.4 14.5 14.6 Wikipedia: Statement
  15. 15.0 15.1 15.2 15.3 Wikipedia: Assignment
  16. Wikipedia: Boolean data type
  17. Wikipedia: Constant (computer programming)
  18. Wikipedia: Data type
  19. Wikipedia: Declaration (computer programming)
  20. Wikipedia: Expression (computer science)
  21. Wikipedia: Floating point
  22. Wikipedia: Integer
  23. Wikipedia: Modulo operation
  24. Wikipedia: Operation (mathematics)
  25. Wikipedia: Order of operations
  26. Wikipedia: Real number
  27. Wikipedia: Statement (computer science)
  28. Wikipedia: String (computer science)