Exam 2 Flashcards

1
Q

reference

A

In Python, a variable is a location in memory (on the stack) that holds a reference to an object. The ‘stack’ is static memory set by the bytecode compiler. The stack is also known as ‘local storage’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

values

A

Conversely, values of the variables are stored on the ‘heap’ which is dynamic memory managed by the PVM or JIT compiler depending on whether CPython or PyPy is the implementation. The heap is also known as ‘free storage’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

All variables in Python reference objects.

A

All variables in Python reference objects. Each variable is an address location on the stack and it holds a reference (address) to the actual storage location in memory where the value is stored. This method of variable storage is different than that employed in some other languages. For instance, in C++, the variable location actually stores the value and not a reference to the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Variables (the references to the actual values)

A

Variables (the references to the actual values) and the values to which they point are both stored in RAM (random access memory).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Variables (the references)

A

Variables (the references) are stored on the program stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Values

A

Values are stored on the program heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Variable values

A

Variable values, the data stored at a location in memory, can change while the program is running.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

data type

A

The data type to which a variable references can change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If two variables

A

If two variables contain the same value and are in the same scope they point to the same object in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

out of scope

A

When a variable goes out of scope it is marked as ready for deletion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When a program exits

A

When a program exits, all references to the program’s variable data are lost and the memory is released back to the operating system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

scope

A

Variables created inside a function have scope which is local to that function. This means that those variables are not visible from and cannot be used outside of that function. This is known as ‘function-level scope’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Variables in some languages

A

Variables in some languages like C++ and Java have block-level scope. Python uses function-level scope which means variable visibility is limited to the function containing the variable. If a variable is not in a function, it is visible within the entire module. PHP and Javascript also use function-level scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

module-level or global scope

A

Variables created outside of all functions have module-level or global scope and are visible to all code in that module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Identifiers

A

Identifiers provide a convenient means to identify variables which is much nicer than using a hexadecimal addresses. An identifier is a name for a variable, function, module, class, or any other object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

assignment operator

A

And, each of the variables is initialized by using the ‘=’ and a value. The ‘=’ is known as the assignment operator. A variable is initialized the first time it is assigned a value. Again, the ‘=’ is known as the assignment operator. It assigns values on the right to variables on the left. Variables on the left are also known as ‘lvalues’. The ‘l’ represents a location in memory. To assign something to the right of the ‘=’ to something on the left, the item on the left must have a name and be a storage location.

17
Q

Naming rules

A

Naming rules for Python identifiers must be followed or syntax errors will result.

  • Cannot be a keyword (see table below)
  • Must begin with a letter or underscore: _total_cost, weight_
  • Can contain only letters, numbers, or underscores: num9_bike, route_66
  • Are case sensitive: num1 is different from Num1
18
Q

Naming Conventions

A

Naming Conventions for Python identifiers are recommended but not required.

  • Class names use ‘CapWords’: BuildingLayout, RegionalEquipment
  • Methods and function identifiers use all lowercase and underscores to separate words: calculate_discount
  • Variable identifiers use the same conventions as method and function identifiers
  • Avoid single letter names such as “i”, “x”, “a” except for counters or iterators
  • Avoid using unclear characters such as: lowercase letter “l”, the letter “o”, uppcase letter “I”
19
Q

Numbers

A

Numbers (Integer, Boolean, Real, Complex)

20
Q

Sequences

A

Sequences (Immutable - String, Tuple, Bytes; Mutable - Lists, Byte Arrays)

21
Q

Mappings

A

(Dictionary)

22
Q

Sequences

in

Python Language Reference

A

Sequences -

the Python Language Reference states that sequences “represent finite ordered sets indexed by non-negative numbers.” This means the sets (or groups of items) are placed in order (one after another but not necessarily sorted) and each item can be accessed by an index number. Items are also known as elements. Sequences come in two categories: immutable and mutable.

23
Q

Immutable

A

Immutable - sequences that cannot be changed after creation. These include strings, tuples, and bytes.

  • String - a sequence of Unicode characters such as: ‘abc’, ‘123’, ‘Hello my name is…’.
    • 128 ASCII characters; 1,112,064 Unicode possibilities
  • Tuple - a sequence of arbitrary Python objects such as: (1, ‘a’, ‘Greetings’).
  • Bytes - a sequence of bytes (8-bits) in an immutable array represented by integers in the range of: 0<=x<256. Not covered in this course.
24
Q

Mutable

A

Mutable - sequences that can be changed after creation.

  • Lists - a sequence of arbitrary Python objects such as: [1, ‘a’, ‘Greetings’]. Lists are similar to tuples but lists can be changed and tuples cannot be changed.
  • Byte Arrays - a sequence with the same functionality as the bytes type except that a bytearray is a mutable array. Not covered in this course.
25
Q

Mappings

A

Mappings - the dictionary is currently the only mapping data type in Python.

These are facts about and attributes of Python dictionaries:

  • Is a mapping and not a sequence
  • Are mutable
  • Do not support the sequence operations listed “Data Types Sequences” chapter
  • Do support the mapping operations listed in the table below
  • Keys must be immutable objects
  • Use empty braces ‘{}’ to create an empty dictionary
  • Consist of key:value pairs
  • Duplicate keys are not allowed
  • Duplicate (more than one) values with the same value are allowed
26
Q

dictionary in computer science

A

A dictionary in computer science has multiple names depending on the language:

  • Associative array - PHP
  • Dictionary - Python, Smalltalk, Objective-C, .NET, Swift
  • Hash - Ruby
  • Hash table - Lisp
  • JSON Object - JavaScript
  • Map - C++, Haskell, Java (implemented as Hashmap)
  • Symbol table - Lua
27
Q

Sets

A

Sets - an unordered collection of unique and immutable objects. Even though sets cannot contain mutable objects such as lists, sets themselves are mutable.

  • Use constructor set() to create a new set
  • Use {} to create a new set
  • Use braces with ellipses ‘{…}’ to create empty set
  • Sets are mutable (except frozen sets)
  • Objects in sets are unordered
  • Objects can be of different types
  • Objects in set must be unique
  • Objects in set must be immutable (no lists in sets)
  • Frozen Set - special type of set that is not mutable
28
Q

function id()

A

Now, let’s take a look at the addresses to which the variables point. We use the built-in function id() to return the integer (or long integer if required) value of the address where the variable value is stored. Normally, computer scientists use hexadecimal values to work with addresses. Therefore, the built-in function hex() is used to convert the integral value returned by id() to a hex value. See the images of the calculator that were used to confirm that the integer and hexadecimal values are equivalent.

29
Q

() [] {}

A

Tuples are created with parentheses (), lists are created with square brackets [], dictionaries and sets are both created with braces {}.

30
Q

Keywords

A
31
Q

set data type

A

The set data type is useful for emulating the mathematical operations of set theory. For instance, using the set operations, we can evaluate the mathematical set theory functions of union, intersection, difference, and symmetric difference. See the Venn diagram below. The set operations yield the following:

  • Union of S1 and S2 = abcdefgh
  • Intersection of S1 and S2 = de
  • Difference of S1 - S2 = abc
  • Difference of S2 - S1 = fgh
  • Symmetric Difference of S1 and S2 = abcfgh
32
Q

Sequences (mutable)

A
33
Q

Sequences (mutable and immutable)

A
34
Q
A