Section 03: Variables, Values, and Math Flashcards

1
Q

Define

Variable

A

Variable: Represents memory space that has a name and holds a value.

Modify through assignment statements

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

Define

Assignment Statements:

A
  • Assignment Statements: “Creates a new variable with a given value”
    • Updates → overrides old value
    • Use = for value assignment:
    Operation assigning value (not statement of equality)

What actually happens: (1) Object created ⇒ (2) Value is assigned ⇒ (3) Reference to object is stored in variable

Modify variables through assignment statements

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

Define

Objects

And Object Types

A

Object Types:

  1. Integer: Numbers without fraction components (Z)
  2. Float: Numbers with decimals (R)
  3. String: Characters or lists of characters
  4. Boolean: True/False

After reassignment: old value = garbage collected (removed from memory)

Dont need to declare type with value assignment - as in Java

Life of an Object:

  • Creation
  • Manipulation
  • Stops existing when no more reference to it (garbage collection will reclaim memory space)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of temporary variables?

A

Swapping Variables: Use temporary variable → reference one values dont lose them

Ex: x = y = z = 5

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

Naming Conventions

A
  • Both letters and numbers; not start number
  • Underscore “_” used names

Should be meaningful

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

Code

Input Function

A

Ask the user to provide information

  • input() pauses program execution and waits for user to type in some input from keyboard
  • Once press Enter → all character typed are read & returned as strings
  • New variable, name created with input

Ex: name = input('What is your name?')

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

Define

Casting

A

Casting: Built-in Function to covert a value from one type to another

  1. int(x): converts value x into type of int
  2. float(x): convert value x into type of float
  3. str(x): converts value x into type str
  4. int(s, base): converts string s into type of int (Base specifies which string is)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Arithmetic Operators

A

Arithmetic Operators: Addition (+), subtraction (-), multiplication (*), division (/), and power (**)

Others:
1. Floor Division Operator: // (rounds down to nearest whole numbers)
2. Modulus % (remainder of the floor division operator)

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

String Operator

A

+ operator for string concatenation

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

Define

Operands

A

Operands: values an operator acts on (literal or variables)

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

Define

Expressions

A

Expressions: sequences of operands and operators

Expressions refers to a sequence of operands and Expressions, while statements are instructions express some action to be carried out

Short Circuit Evaluation: when only the first operand (line in an or statement is evaluated) then moves on with the code

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

Code

What are the math functions?

A
  • abs(x): returns absolute value of x
  • pow(x, y): returns x^y (always a float)
  • round(x): rounds x to nearest integer
  • round(x, n-digits): round to given precision in decimal digit
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Math Module

A
  • “File” called math module provides us with additional functions that we can use
  • Access: (1) import module → import math (2) call function want to use math.function_name
    • sin, cos, pi

Documentation: help(math) gives documentation

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

Code

import function:

A

💡 import statement gives access to functions defined in particular module

Ex: math.sqrt(x)
(not a void function)

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