Purpose: Refresh knowledge of basic Python data structures, keywords, control flow operations, and fundamentals.
Target Audience: Intermediate Python programmers aiming for expert level.
Importance of Basics: A deep understanding helps in seeing the larger picture, essential for roles like tech lead or computer science professor.
Professors often possess profound knowledge of the basics to identify research gaps.
Importance: Understanding data structures is fundamental for various programming tasks like machine learning, website management, and algorithm development.
Integer: A whole number (e.g., 3).
Float: A number with floating-point precision (e.g., 3.14).
Python offers various built-in numerical operations, including type conversions.
Examples using numerical data types (Listing 1-1):
x, y = 3, 2
Operations include addition, subtraction, multiplication, division, integer division, modulo, negation, absolute value, casting, and exponentiation.
Integer Division: Uses //
operator which rounds down.
Boolean data type has two values: True and False.
Internally represented using integers (0 for False, 1 for True).
Example: x = 1 > 2
results in False
.
Important keywords for logical operations: and, or, not.
Combinations yield more complex expressions.
Example (Listing 1-3): Various logical expressions using these keywords.
Order of application matters in expressions.
Example interpretations of the expression regarding weather conditions.
Preferred order of operations: not
> and
> or
.
Example: Listing 1-4 illustrates with True/False evaluations.
Definition: Sequences of characters in Python; immutable.
Common ways to create strings: single quotes, double quotes, triple quotes.
Whitespace characters: Newline (\n
), space (\s
), tab (\t
).
Functions such as strip, lower, upper, startswith, endswith, find, replace, join, length, and containment check.
Strings can be manipulated using these methods for various applications.
Represents absence of a value (similar to null in Java).
Example: None is not equal to any numerical values or empty container types (Listing 1-6).
Definition: Sequence of elements, mutable.
Example of list creation and properties.
Adding Elements: Common methods to append, insert, or concatenate.
Removing Elements: Use remove(x)
method to delete items.
Reversing and Sorting: Methods reverse()
and sort()
modify the original list.
Concept: Last-In, First-Out (LIFO) structure; intuitive operation mimicking a stack of papers.
Uses append()
and pop()
methods.
Definition: Unordered collection of unique elements, all elements must be hashable.
Example: Creating a set shows unique properties and hashability checks (Listing 1-8).
Key properties: unordered, unique elements—no duplicates.
Definition: Stores (key, value) pairs.
Example: Retrieve and modify values using keys.
Methods to access keys, values, and items for iteration.
Keywords: if
, else
, elif
for decision making (Listing 1-10).
Types: for
loops and while
loops for repeated execution.
Termination methods: loop conditions or break
statement (Listing 1-12).
Using continue
: Skips to the next iteration without terminating the loop (Listing 1-13).
Definition: Reusable code constructs, defined with def
keyword.