Values and Types
Introduction to CMPUT 175 Python Review
Overview of the review material focusing on basic values and types in Python.
Basic Data Types in Python
Integers (int)
Definition: Integers are whole numbers that can be positive or negative without a decimal point.
Storage: Integers can have as many digits as the memory allows.
Basic Operations on
ints:Addition:
3 + 4Subtraction:
9 - 5Multiplication:
-34 * 3Division (floor division):
7 // 2Exponentiation:
2 ** 128Modulo operation:
32 % 3Explanation of Modulo:
Returns the remainder after integer division.
Results always between
0and one less than the divisor (right value):Example:
1 % 3 = 1,2 % 3 = 2,3 % 3 = 0, etc.
Useful in data structures and algorithms for wrapping behavior.
Floating-Point Numbers (float)
Definition: Floats are finite approximations of real numbers that include decimal points.
Basic Operations on
floats:Addition:
5.0 + -2.4Multiplication:
4.9 * 2Subtraction:
0.2 - 0.5Division:
7.0 / 2.0(note automatic conversion ofintstofloatswhen using/operator).
Type Determination:
Use
type()function to determine data types:type(3)givesinttype(3.0)givesfloatFor division,
type(7 // 2)isintbuttype(7.0 / 2)isfloat.
Text Representation
Strings (str)
Definition: Strings are sequences of characters enclosed in quotes (single or double).
Accessing Characters:
Indexing starts from zero, e.g., for string "Eddie", use:
"Eddie"[0]to access the first letter.
String Length: Use the
len()function:Example:
len("Eddie")returns5.
Indexing: The last character is accessed with index one less than the length, e.g.,
"Eddie"[4].
String Operations
Membership Testing: Check if a substring exists within a string using
inoperator.Example:
"Ed" in "Eddie"isTrue, while"ed" in "Eddie"isFalse.
Case Sensitivity: Strings are case sensitive. Use
.lower()to convert a string to lowercase and.upper()for uppercase.String Concatenation: Use the plus operator to join strings, e.g.,
"CMPUT" + "175".
Special Value: None
Definition:
Nonerepresents the absence of a value.Recognizable by its capitalization.
Returns no output when evaluated in Python's interactive console.
Future Relevance:
Nonewill be useful for representing nothingness in computations as discussed in future lessons.
Summary of Concepts
Integer and floating-point representations in Python using
intandfloattypes.String manipulation using
strtype.Importance of
Nonefor value representation in applications.