CISC 101 Chapter 2: Chapter 2 Expressions and Variables

100 views13 pages
2.1 Variables and Assignments
VARIABLES AND ASSIGNMENTS
VARIABLE is a named item that is used to hold a value (i.e x or num_people)
An assignments left side must always be the variable and the right side is the expression
•
ASSIGNMENT STATEMENT assigns a variable with a value (x=5)
In programming, a variable is a place to hold a value.
1.
2.
Based on this order, you can then use this format to compute numerous formulas with the
variables and expressions
3.
If you rewrite the expression value of a variable, the former value will be overwritten
4.
In algebra, an equation means that "the item on the left side equals the item on the right"
5.
Assignment statements look similar, but have VERY different meaning. The left side MUST be
one variable
6.
The "=" isn't "equals", but it s an action that PUTS a value into a variable. Assignment
statements only make sense when executed in sequence
7.
2.1.1: Variables and Assignments
Chapter 2: Expressions and Variables
September 12, 2018
11:10 PM
CISC 101 Elements of Comp Sci Page 1
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in
Increasing a variable's value by 1, as in x = x + 1, is common - this is known as incrementing the
variable
Because in programming = means assignment, a variable may appear on both the left and right as in
x = X + 1
2.2 Objects
Example: executing x = 4 created a new object to represent the value of 4
•
NOTE that a programmer does not explicitly create objects; instead, the interpreter creates
and manipulates objects as needed to run the Python code.
•
Objects are used to represent everything n Python program, including integers, strings,
functions, lists, etc.
•
The animation below shows some objects being created while executing Python code
statements in an interactive Python interpreter. The interpreter assigns an object to a location
somewhere in memory automatically.
•
OBJECT represents a value and it is automatically created by the interpreter when executing a line
of code
The interpreter creates a new object with the value 4. The object is stored somewhere in
memory.
1.
Once 4 is printed, the object is no longer needed and is thrown away.
2.
New object created: 'x' references object stored in address 98.
3.
Objects are retrieved from memory when needed.
4.
2.2.1: Creating New Objects
NAME BINDING is the process of associating names with interpreter objects. An object can have
more than one name bound to it, and every name is always bound to exactly one object. It occurs
whenever an assignment statement is executed
PROPERTIES OF OBJECTS
CISC 101 Elements of Comp Sci Page 2
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in
The value of an object is the data associated with the object.
â—‹
The value of an object can generally be examined by printing that object.
â—‹
Value: A value such as "20", "abcdef", or 55.
1.
The type of an object determines the object's supported behavior - its type never
changes once it is created
â—‹
Integers and strings are imutable, modifying their values with assignments results
in new objects being created and the names bound to the new object
â–Ş
The type of an object also determines the mutability - indicates whether the object's
value is allowed to be changed - of the object
â—‹
Type: The type of the object, such as integer or string.
2.
Identity: A unique identifier that describes the object.
3.
Each Python object has three defining properties: value, type, and identity.
The identity of an object is a unique numeric identifier, such as 1, 500, or 505534. Only one object at
any time may have a particular identifier. The identity normally refers to the memory address where
the object is stored. Python provides a built-in function id() that gives the value of an object's
identity.
2.3 Identifiers
Because python is case sensitive, meaning upper and lower cases will differ (Ex. "Cat" vs "cat")
•
Names that start and end with double underscore are allowed - but they should be avoided
because Python has a special usage for that sequence.
•
Good names should describe the purpose of the variable (Ex. "temperature", "age", etc)
•
Some words can't be used (Ex. "true", "print", etc) because they are reserved words - words
that are part of the language and cannot be used as the programmer defined name. In fact,
many language editors will automatically colour a program's reserved words to indicate that
they cannot be used.
•
A programmer gives names to various items, like variables. These are called identifiers (or names).
They are a sequence of letters, underscores, and digits, and must start with a letter or an
underscore.
CISC 101 Elements of Comp Sci Page 3
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Variable is a named item that is used to hold a value (i. e x or num_people) Assignment statement assigns a variable with a value (x=5) An assignments left side must always be the variable and the right side is the expression. In programming, a variable is a place to hold a value. An assignment statement assigns the left-side variable with the right side expressions. Based on this order, you can then use this format to compute numerous formulas with the variables and expressions. If you rewrite the expression value of a variable, the former value will be overwritten. In algebra, an equation means that "the item on the left side equals the item on the right" Assignment statements look similar, but have very different meaning. The "=" isn"t "equals", but it s an action that puts a value into a variable. Assignment statements only make sense when executed in sequence. Cisc 101 elements of comp sci page 1.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions