Section 03: Variables, Values, and Math Flashcards
Define
Variable
Variable: Represents memory space that has a name and holds a value.
Modify through assignment statements
Define
Assignment Statements:
-
Assignment Statements: “Creates a new variable with a given value”
- Updates → overrides old value
- Use
=
for value assignment:
What actually happens: (1) Object created ⇒ (2) Value is assigned ⇒ (3) Reference to object is stored in variable
Modify variables through assignment statements
Define
Objects
And Object Types
Object Types:
- Integer: Numbers without fraction components (Z)
- Float: Numbers with decimals (R)
- String: Characters or lists of characters
- 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)
What is the purpose of temporary variables?
Swapping Variables: Use temporary variable → reference one values dont lose them
Ex: x = y = z = 5
Naming Conventions
- Both letters and numbers; not start number
- Underscore “_” used names
Should be meaningful
Code
Input Function
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?')
Define
Casting
Casting: Built-in Function to covert a value from one type to another
-
int(x)
: converts value x into type of int -
float(x)
: convert value x into type of float -
str(x)
: converts value x into type str -
int(s, base)
: converts string s into type of int (Base specifies which string is)
Arithmetic Operators
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)
String Operator
+
operator for string concatenation
Define
Operands
Operands: values an operator acts on (literal or variables)
Define
Expressions
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
Code
What are the math functions?
-
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
Math Module
- “File” called math module provides us with additional functions that we can use
- Access: (1) import module →
import math
(2) call function want to usemath.function_name
sin, cos, pi
Documentation: help(math)
gives documentation
Code
import
function:
💡 import
statement gives access to functions defined in particular module
Ex: math.sqrt(x)
(not a void function)