Compsci exam notes Flashcards
Variables
A variable is a named storage location in a computer program.
Different types of variables are used to store different data types.
To define a variable, you need to specify its name and initial value.
Assignment Statement
Use the assignment statement ‘=’ to place a new value into a variable.
Assignment Statement Example
cansPerPack = 6
Note: ‘=’ is not used for comparison; it copies the value on the right into the variable on the left.
Variable Types
There are three types of data in Python: integers (int), floating-point numbers (float), and strings.
Variable Types Examples
cansPerPack = 6 # int, canVolume = 12.0 # float
Updating a Variable
If an existing variable is assigned a new value, that value replaces the previous contents.
Updated Variable Example
cansPerPack = 6 and then cansPerPack = 8
Computed update: cansPerPack = cansPerPack + 2
Variable Naming
Variable names should describe the purpose of the variable.
Follow rules: start with a letter or underscore, continue with letters, digits, or underscore.
Use ‘camelCase’ notation, avoid reserved words.
Constants
In Python, a constant is a variable whose value should not change after the initial assignment.
Constants are conventionally named in ALL_CAPS.
Arithmetic Operations
Python supports basic arithmetic operations: addition ‘+’, subtraction ‘-‘, multiplication ‘*’, division ‘/’.
Precedence follows PEMDAS (Parenthesis, Exponent, Multiply/Divide, Add/Subtract).
Mixing numeric types results in a floating-point value.
Arithmetic Operations (Contd.)
Use ‘**’ for exponentiation.
Floor division (//) discards the fractional part.
Remainder calculation uses the ‘%’ operator.
Arithmetic Operations (Contd.) Example
Convert pennies to dollars and cents
pennies = 1729
dollars = pennies // 100 # Calculates the number of dollars
cents = pennies % 100 # Calculates the number of pennies
print(“I have”, dollars, “and”, cents, “cents”)
Math Module
Python’s standard library includes the math module for advanced mathematical functions.
Import functions using from math import sqrt.
Strings
Strings are sequences of characters in Python.
Use single or double quotes to define string literals.
String length is obtained using len().
String Operations
Concatenate strings using ‘+’. Example: firstName + lastName
Repetition using ‘*’. Example: “-“ * 50
Convert numbers to strings using str().
String Methods
strings have built-in methods like upper(), lower(), etc.
String Methods Example
Example: name.upper() converts all characters to uppercase.
Input and Output
Use input() to read from the console.
Format output using % format specifiers.
Commenting Code
Use comments for explanation and documentation.
Two common styles include starting each line with # or using “”” “”” for multiline comments.
The if Statement
Allows a program to execute different actions based on data.
Keywords: if and else.
The if Statement example
floor = int(input(“Enter floor number: “))
if floor > 13:
actualFloor = floor - 1
else:
actualFloor = floor
print(“The elevator will travel to the actual floor %d” % actualFloor)
Relational Operators
Used in if statements for comparisons.
Floating Point Considerations
Floating-point numbers may have precision issues.
Relational Operators Examples
if floor > 13:
if floor >= 13:
if floor < 13:
if floor <= 13:
if floor == 13:
if floor != 13: