Week 1: Variables, Operators, Strings Flashcards
(100 cards)
a) Define Variable
b) In python everything is an ______
c) Variables _______objects stored in a computer’s memory
d) Python does not require variables to be _____ ______ (ie. you can create a variables by simply assigning it a value)
a) Variable: Named area in a computer’s memory that can store a value
b) In python, everything is an object
c) Variables point to (reference) objects stored in the computer’s memory
d) Python does not require variables to be explicitly stated (you can create a variable by simply assigning it a value)
Define identifier
What are the rules for variable or function identifiers?
An identifier is a name we give to variables or functions
- In python there are certain rules we must follow when naming variables or functions
- Case sensitive
- Must start with a letter or underscore
- Can only contain letters, digits, or underscores
- Can not be identical to a built-in keyword
Some common naming conventions for identifiers
Some common naming conventions:
- snake_case
- camelCase
- SCREAMIG_SNAKE_CASE
- SCREAMINGCASE
- UpperCamelCase (aka. Pascal Case)
- flatcase
Style guidelines for variable identifiers
- should always have a meaningful name
- try not to make it too long
- should follow a consistent style
What is PEP8
Naming conventions under PEP8
PEP8 is python’s recommended style guide
PEP8 Naming Conventions:
- snake_case should be used for variable and function identifiers
- Never use the characters “I” “l” or “O” as single character variable names
- Shoud only use English words and letters whenever feasible
- SCREAMING_SNAKE_CASE should be used for constants
Define Constants
Constants are variables whose value should never change after its initial value set
- python does not enforce this, but you should let other programs know a value is a constant by using SCREAMING_SNAKE_CASE
Variables in python are ____ typed
This means ___?
Variables in python are dynamically typed
This means one variable can be assigned values of different types
Some of the built-in data types
-
int (integer)
used for whole numbers, negative or positive -
float (floating-point)
used for real numbers with decimal points, can be positive or negative -
bool (boolean)
stores a true or false value, often used in conditions and logical operations -
str (string)
used for text, allowing you to work with strings of characters. can include most numbers, letters, and symbols -
list (list)
used to store a collection of items of any type. lists are often ordered and changeable, meaning you can add, remove or modify elements after creating them -
tuple (tuple)
similar to a list but immutable, meaning once its created, its elements can not be changed, added, or removed -
set (set)
unordered collection of unique elements, meaning it does not allow duplicate values and does not maintain any specific order. supports set operations like unions and intersections -
dict (dictionary)
collection of key-value pairs where each key is unique and maps to a corresponding value
What is the type() function
- We can find the type of an object using the type function
- The type function takes an object and returns its type
I.e.
Input:
w = true
type(w)
Output:
< class ‘bool’>
Define casting
What is implicit casting vs. explicit casting
Casting: converting values from one type to another is called casting
- Casting in python can either be implicit or explicit
- Implicit casting occurs automatically when some operation is performed by an object
- Explicit casting requires the programmer to manually use one of the built-in data type functions
Some built-in data type functions
int()
- Creates an integer value by converting a float or string (when possible)
- trunicates the decimal point
float()
- Creates a floating-point value by converting an integer or string (when possible)
str()
- Creates a string value by converting a value of another type (supports most other types)
2 common cases where you need explicit casting
- You wish to input a numerical value from the user, but the input() function returns a string
- You wish to concatenate (add) a numerical value to a string, but strings can not be added to numerical types
Define operators
How are they represented?
- Operators are used to perform operations on objects and values
- Operators are represented as special symbols or keywords that designate some type of computation
Categories of operators
- Assignment operators
- Arithmetic operators
- Comparison operators
- Booelean or logical operators
- Identity operators
- Membership operators
- Concatenation and repetition operators
- Bitwise Operators
What is the Assisgnment Operator and how it works?
The Assignment Operator: used to assign values to variables
- The value of the expression on the right is evaluated and the result is stored in the variable on the left
Assignment Operator Example:
Input:
x = 10
y = 5
z = x + y
x = 3
y = 2
z = z +1
print(x, y , z)
What is the output, and explain why?
3 2 16
- x is assigned to 10 then reassigned to 3 so x is 3
- y is assigned to 5 then reassigned to 2 so y is 2
- z is assigned to x+y. At the time x was 10 and y was 5 so z is 15, and 15 is stored in z even after x+y change.
- then z is reassigned to z+1 so z now stores the value 16
Define arithmetic operators
arithmetic operators allow you to perform arithmetic operations on numeric values
What are some common arithmetic operators
More common arithmetic operators
What is the order of operations for arithmetic operators?
The order of operations from highest to lowest priority is:
1. Parentheses
2. Exponents
3. Multiplication, Division, and Modulo
4. Addition and Subtraction
What are compound operators.
Examples of compound operators.
additional Assignment Operators called compound operators provide shorthand for some common assignments we tend to see with variables.
What are some built-in math functions?
What is a module
A python module is a file containing Python code, such as functions, classes, and variables, that can be imported and used in other Python programs
The Python standard library comes with many premade modules that include helpful functions and classes we can use in our programs.
How do we import modules? (what keywords do we use)
To use a module, we first need to import it into our program using the from and import keywords.
Example:
- from math import sqrt