# Python Programming/Variables

This lesson introduces variables, expressions, and statements.

## Objectives and Skills

Objectives and skills for this lesson include:
* Language Basics
** Language elements (constants, numbers and strings)
** Strings types (single quotes, double quotes and triple quotes)
** Escape Sequence, string concatenation and format method
** Variables naming, types and objects
** Indentation, logical and physical lines
* Operators and Expressions
** Operators and Expressions
** Evaluation Order and Associativity
* Input Output
** User input

## Readings

* Wikipedia: Variable (computer science)
* Wikipedia: Data type
* Wikipedia: Expression (computer science)
* Wikipedia: Statement (computer science)
* Wikipedia: Order of operations
* Python for Everyone: Variables, expressions, and statements

## Multimedia

* YouTube: Python for Informatics: Chapter 2 - Expressions
* YouTube: Python Numbers
* YouTube: Python3 Input & Output
* Youtube: Python 3 Programming Tutorial: Variables
* Youtube: Setting up Pycharm and getting started

## Examples
### Data Types

Built-in Python data types include integer (int), floating point (float), string (str), and Boolean (bool) data types.

### Type Conversion

An object’s type is accessed by the built-in function type.

### Quotes

String literals are written in a variety of ways, including single quotes, double quotes, and triple quotes. Triple quoted strings may span multiple lines. The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

### Numeric Operations

All numeric types (except complex) support the following operations, sorted by ascending priority.

### Assignment Operations

An assignment statement evaluates the expression and assigns the result to the target. Augmented assignment is the combination, in a single statement, of an operation and an assignment statement.

### Input Function

Python 2: If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), which is then parsed and evaluated as a Python expression.
Python 3: If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** Variables and Types
*** Basic Operators
** TutorialsPoint
*** Basic Syntax
*** Variable Types
*** Basic Operators
** Codecademy
*** Strings and Console Output
** SoloLearn
*** Basic Concepts
** Wikiversity
*** Python/Numbers
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Variables, Scripts

### Practice

* Experiment with different numeric operations to ensure you understand how they work. Then review either MathsIsFun: Order of Operations or Teachoo: What is BODMAS?. Create a Python program that demonstrates the order of operations.
* Create a Python program to prompt the user for hours and rate per hour to compute gross pay (hours * rate).
* Review MathsIsFun: Conversion of Temperature. Create a Python program that asks the user for a Fahrenheit temperature and then calculate and display the corresponding Celsius temperature or ask the user for a Celsius temperature and then calculate and display the corresponding Fahrenheit temperature.
* Create a Python program that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds.
* Review MathsIsFun: Area of Plane Shapes. Create a Python program that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes.

### Games

* Play CodeCombat.

## Lesson Summary

* Built-in Python data types include integer (int), floating point (float), string (str), and Boolean (bool).
* An object’s type is accessed by the built-in function type.
* String literals are written in a variety of ways, including single quotes, double quotes, and triple quotes. Triple quoted strings may span multiple lines.
* The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
* Numeric operators include +, -, *, /, //, %, and **.
* Assignment operators include +=, -=, *=, /=, //=, %=, and **=.
* The Python 2 input function reads a line from input, converts it to a string (stripping a trailing newline), which is then parsed and evaluated as a Python expression.
* The Python 3 input function reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

## Key Terms
### assignment

    A statement that assigns a value to a variable.

### concatenate

    To join two operands end to end.

### comment

    Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.

### escape character

    A character which invokes an alternative interpretation on subsequent characters in a character sequence.

### evaluate

    To simplify an expression by performing the operations in order to yield a single value.

### expression

    A combination of variables, operators, and values that represents a single result value.

### floating point

    A type that represents numbers with fractional parts.

### floor division

    The operation that divides two numbers and chops off the fractional part.

### integer

    A type that represents whole numbers.

### keyword

    A reserved word that is used by the compiler to parse a program; you cannot use keywords like if, def, and while as variable names.

### mnemonic

    A memory aid. We often give variables mnemonic names to help us remember what is stored in the variable.

### modulus operator

    An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by another.

### operand

    One of the values on which an operator operates.

### operator

    A special symbol that represents a simple computation like addition, multiplication, or string concatenation.

### rules of precedence

    The set of rules governing the order in which expressions involving multiple operators and operands are evaluated.

### statement

    A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.

### string

    A type that represents sequences of characters.

### type

    A category of values. The types we have seen so far are integers (type int), floating-point numbers (type float), and strings (type str).

### value

    One of the basic units of data, like a number or string, that a program manipulates.

### variable

    A name that refers to a value.

## Review Questions

* Built-in Python data types include _____, _____, _____, and _____. Built-in Python data types include integer (int), floating point (float), string (str), and Boolean (bool).
* An object’s type is accessed by the built-in function _____. An object’s type is accessed by the built-in function type.
* String literals are written in a variety of ways, including _____, _____, and _____. _____ may span multiple lines. String literals are written in a variety of ways, including single quotes, double quotes, and triple quotes. Triple quoted strings may span multiple lines.
* The _____ character is used to escape characters that otherwise have a special meaning, such as _____. The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
* Numeric operators include _____. Numeric operators include +, -, *, /, //, %, and **.
* Assignment operators include _____. {{{2}}}
* The Python 2 input function _____. The Python 2 input function reads a line from input, converts it to a string (stripping a trailing newline), which is then parsed and evaluated as a Python expression.
* The Python 3 input function _____. The Python 3 input function reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

## Assessments

* Flashcards: Quizlet: Python Variables
* Flashcards: Quizlet: Python Variable Types
* Flashcards: Quizlet: Python Variables, Expressions, and Statements
* Quiz: Quizlet: Python Variables
* Quiz: Quizlet: Python Variable Types
* Quiz: Quizlet: Python Variable, Expressions, and Statements

## See Also

* Python.org: An Informal Introduction to Python
* Python.org: Built-in Types
* Python.org: Expressions
* Python.org: Data model
* Python.org: Simple statements
* SoloLearn: Python
* Open Book Project: Python 3 - Variables, expressions and statements
* Digital Ocean: How to use Variables in Python 3
* PythonBasics: variables