<style> .markdown-body h1:first-of-type { margin-top: 24px; } .markdown-body h1 { margin-top: 64px; } .markdown-body h1 + h2 { margin-top: 32px; } .markdown-body h2 { margin-top: 48px; } .markdown-body h2.topics { font-size: 1.8em; border-bottom: none; } .markdown-body h2 { color: #5a57b8; } .markdown-body h3 { color: cornflowerblue; } .markdown-body h4 { color: cornflowerblue; } .exercise { font-size: 150%; font-weight: bold; color: rgb(227,112,183); } .note { color: red; } .markdown-body em { color: #e000e0; } .markdown-body strong { color: #e000e0; font-weight: bold; } .markdown-body p a, .markdown-body td a { border-bottom: 1px solid; } .markdown-body a:hover { border-bottom: none; text-decoration: none; } </style> # Python Language Quick Reference ## Terminology **Identifier**: a single letter or underscore, or word that begins with a letter or underscore. **Keyword**: an identifier that is part of the Python language. Keywords cannot be used as user-defined identifiers (e.g. as variable, function, or class names) ## Python Symbols and Usage The table below is designed to help you identify and understand some of the fundamental rules and operations that occur in Python code. | Symbol | Use | | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | = | - *Creates* or *updates* a [Variable](#Variables) | | == | - *Tests* equality between two values, e.g. `x == 10` | | '' or "" | - *Creates* a [String](#Strings) | | [ ] | - *Creates* a [List](#Lists) when appearing on the right-hand side of `=`<br>- *Accesses* [Sequences](#Sequences) or [Dictionaries](#Dictionaries) when preceded immediately by an identifier | | ( ) | - *Creates* a [Tuple](#Tuples) when not on the left-hand side of `=` and not preceded immediately by an identifier<br>- *Runs* a [Function](#Functions) when not on the left-hand side of `=` and preceded immediately by an identifier<br>- *Defines* a [Function](#Functions) when preceded immediately by an identifier and the keyword `def` | | { } | - *Creates* an empty [Dictionary](#Dictionaries) when not on the left-hand side of `=` and containing no values<br>- *Creates* a [Dictionary](#Dictionaries) when not on the left-hand side of `=` and containing only `key:value` pairs<br>- *Creates* a [Set](#Sets) when not on the left-hand side of `=` and containing values that are not `key:value` pairs | | . | - *Indicates* an [Object](#Objects) is being used when not inside of single or double-quotes. The value on the left-hand side of the dot is the object itself, and the value on the right-hand side of the dot is a property or method | ## Variables Any time a single `=` (the 'assignment operator') appears, a variable is being created or updated. If the identifier (letter or word) to the left of the `=` is appearing in the script for the first time, a variable is **created**. If the identifier to the left of the `=` has already appeared in the script, the variable is **updated**. Example: ```python x = 10 # first appearance of 'x', so a variable is created, storing the value 10 x = 20 # x is updated and now stores the value 20 y = 30 # first appearance of 'y', a variable is created, storing the value 30 x = 40 # x is updated and now stores the value 40 ``` ### Variable Naming Rules A variable name must start with a letter or underscore, and cannot be a 'reserved word' - a word that has a special meaning in Python. You can see a list of all reserved words by typing `help('keywords')` in the Python REPL. You should also avoid using non-reserved words that are already in use, for example: ```python """ 'list' is the name of a class *and* function in Python and should not be used as a variable name """ list = [1, 2, 3, 4] ``` ## Sequences ### Lists Lists are data structures that can contain *many* values (of any data type), separated by commas ### Tuples Tuples are similar to lists but *immutable*, meaning that once values are assigned they cannot be changed ### Strings Strings are *immutable* sequences of characters (letters, numbers, symbols, emojis, etc.) in between single or double quotes ## Functions Functions are reusable chunks of code - one or more Python statements that we need to run multiple times in a script. Any time your script is performing the same (or similar) actions multiple times, a function should probably be defined. This helps to keep code D.R.Y. (Don't Repeat Yourself), i.e. cleaner and more organized. We can create our own functions using the `def` keyword, or use pre-defined Python functions like `print()` and `input()` ## Sets Sets are data structures that can contain *unique* values, and support most of the set operations you may be familiar with from arithmetic: unions, intersections, etc. ## Dictionaries Dictionaries are data structures that can contain *key/value pairs*. Each piece of data stored in a dictionary has to have a name (the key) and an associated value. ## Objects Objects (covered extensively in second-term ACIT2515) are data structures that can contain both named variables (object *properties*) and functions (object *methods*) that can operate on those variables. *Classes* are like templates that define a set of properties and methods, from which we can create many objects. Every object created from a class will have the same structure and behavior, but may have different values. Think of classes as a generic description of something: a student, for example. All students have names, email addresses, and id numbers. All students can take classes and write exams. These properties (name, email address, id) and methods (take class, write exam) define a theoretical class from which we can create objects: the actual students, whose names, email addresses, and id numbers are unique. Objects and classes can be identified by the use of 'dot notation': any time you see a dot (that is not inside of quotes) in a Python statement, an object or class is being used. The identifier on the left of the dot is always an object or class, and the identifier on the right of the dot is either a property or method.