PythonOneLiners
PYTHON REFRESHER
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.
Basic Data Structures
Importance: Understanding data structures is fundamental for various programming tasks like machine learning, website management, and algorithm development.
Numerical Data Types and Structures
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.
Arithmetic Operations
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.
Booleans
Boolean data type has two values: True and False.
Internally represented using integers (0 for False, 1 for True).
Example:
x = 1 > 2
results inFalse
.
Important keywords for logical operations: and, or, not.
Combinations yield more complex expressions.
Example (Listing 1-3): Various logical expressions using these keywords.
Boolean Operator Precedence
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.
Strings
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
).
Important String Methods (Listing 1-5)
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.
The Keyword None
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).
Container Data Structures
Lists
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()
andsort()
modify the original list.
Stacks
Concept: Last-In, First-Out (LIFO) structure; intuitive operation mimicking a stack of papers.
Uses
append()
andpop()
methods.
Sets
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.
Dictionaries
Definition: Stores (key, value) pairs.
Example: Retrieve and modify values using keys.
Methods to access keys, values, and items for iteration.
Control Flow
Conditional Execution
Keywords:
if
,else
,elif
for decision making (Listing 1-10).
Loops
Types:
for
loops andwhile
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).
Functions
Definition: Reusable code constructs, defined with
def
keyword.