Python for Absolute Beginners
9781501521980
This book eases you into the foundational aspects of Python 3.x with an extensive range of code samples that illustrate
138
1
4MB
English
Pages 252
Year 2023
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
Cover
Title Page
Copyright Page
Dedication
Contents
Preface
Chapter 1: Introduction to Python
Tools for Python
easy_install and pip
virtualenv
Python Installation
Setting the PATH Environment Variable (Windows Only)
Launching Python on Your Machine
The Python Interactive Interpreter
Python Identifiers
Lines, Indentations, and Multi-Lines
Quotation Marks and Comments
Saving Your Code in a Module
Some Standard Modules
The help() and dir() Functions
Compile Time and Runtime Code Checking
Formatting Techniques
Working with f-strings
Working with Strings
Comparing Strings
Formatting Strings
Summary
Chapter 2: String Operations
Working with Strings
Formatting Strings
Slicing and Splicing Strings
Testing for Digits and Alphabetic Characters
Search For and Replace a String in Other Strings
Remove Leading and Trailing Characters
Printing Text without NewLine Characters
Working with Dates
Converting Strings to Dates
Exception Handling
Handling User Input
Command-Line Arguments (optional)
Summary
Chapter 3: Working with Loops
Precedence of Operators
Reserved Words
Working with Lists
Lists and Basic Operations
Reversing and Sorting a List
Working with Loops
Python for Loops
A for Loop with try/except
Flatten a List of Lists
Numeric Exponents in Python
Nested Loops
while Loops
Count the Number of Digits in Numbers
Count the Number of Digits in a Positive Integer
Using a while Loop to Find the Divisors of a Number
Using a while Loop to Find Prime Numbers
Assigning Values to Multiple Variables
The break/continue/pass Statements
Basic List Comprehensions
Working with List Comprehensions
Lists and Filter-related Operations
Summary
Chapter 4: Conditional Logic
Conditional Logic
Checking for Leap Years
Comparison and Boolean Operators
The in/not in/is/is not Comparison Operators
The and, or, and not Boolean Operators
Local and Global Variables
Uninitialized Variables and the Value None
Scope of Variables
Passing by Reference versus by Value
Arguments and Parameters
Summary
Chapter 5: Built-in Functions
Functions and Methods
Built-in Functions
The split(), join(), and range() Functions
The join() Function
The range() Function
The char Class
User-defined Functions
The return Statement in Python Functions
Specifying Default Values in a Function
Returning Multiple Values from a Function
Functions with a Variable Number of Arguments
Importing Custom Modules
Summary
Chapter 6: Tasks with Strings and Arrays
Task: Count Word Frequencies
Task: Check If a String Contains Unique Characters
Task: Insert Characters in a String
Task: String Permutations
Task: Check for Palindromes
Working with 1D Arrays
Rotate an Array
Task: Sort Array In-place Without a Sort Function
Task: Invert Adjacent Array Elements
Working with 2D Arrays
The Transpose of a Matrix
Summary
Chapter 7: Tasks with Numbers
Time and Space Complexity
Task: FizzBuzz
Task: Sum of Even and Odd Numbers in a List
Task: Maximum and Minimum Powers of an Integer
Task: Calculate the Number of Digits
Task: Determine If a Positive Integer is Prime
Task: Find the Prime Factorization of a Positive Integer
Task: Goldbach’s Conjecture
Task: Sum of Prime and Composite Numbers
Summary
Chapter 8: Working with Bit Values
Working with Bit Values
Task: Binary Substrings of a Number
Task: Common 1 Values of Two Binary Numbers
Task: Invert Bits in Even and Odd Positions
Task: Invert Pairs of Adjacent Bits
Task: Find Common Bits in Two Binary Numbers
Task: Check for Adjacent Set Bits in a Binary Number
Task: Count Bits in a Range of Numbers
Task: Find the Right-most Set Bit in a Number
Task: The Number of Operations to Make All Characters Equal
Task: Compute XOR without XOR for Two Binary Numbers
Summary
Chapter 9: Python Data Structures
Queues
Tuples (Immutable Lists)
Sets
Dictionaries
Creating a Dictionary
Displaying the Contents of a Dictionary
Checking for Keys in a Dictionary
Deleting Keys from a Dictionary
Iterating Through a Dictionary
Interpolating Data from a Dictionary
Dictionary Functions and Methods
Dictionary Formatting
Ordered Dictionaries
Sorting Dictionaries
Python Multi-Dictionaries
List Comprehensions
Lists and Filter-related Operations
Expressions in Lists
Sorting Lists of Numbers and Strings
Lists and Arithmetic Operations
Concatenating a List of Words
Arrays and the append() Function
Working with Lists and the split() Function
Counting Words in a List
Iterating Through Pairs of Lists
Other List-Related Functions
Summary
Chapter 10: Introduction to Recursion
What is Recursion?
Arithmetic Series
Calculating Arithmetic Series (Iterative)
Calculating Arithmetic Series (Recursive)
Geometric Series
Calculating a Geometric Series (Iterative)
Calculating Geometric Series (Recursive)
Factorial Values
Calculating Factorial Values (Iterative)
Calculating Factorial Values (Recursive)
Calculating Factorial Values (Tail Recursion)
Fibonacci Numbers
Calculating Fibonacci Numbers (Recursive)
Calculating Fibonacci Numbers (Iterative)
Task: Reverse a String via Recursion
Task: Check for Balanced Parentheses
Task: Determine if a Positive Integer is Prime
Summary
Chapter 11: Miscellaneous Topics
Functionally-Oriented Programming
The filter() Function
The map() Function
The reduce() Function
The Lambda Operator and Lambda Expressions
Lambda Expressions
Dunders and Magic Methods
The Iterator Protocol
The iter() Function and the __iter__() Method
Dictionaries and Iterators
Examples of Iterators
Range Versus a List
What is a Pipe?
Working with Generators
The yield Keyword
Generators and Comprehensions
A Generator Without a Loop
Miscellaneous Examples of Generators
Generate Squares of Numbers
Generate an Infinite List of Integers
Find Prime Numbers
Summary
Appendix A: Introduction to NumPy
What is NumPy?
Useful NumPy Features
What are NumPy Arrays?
Working with Loops
Appending Elements to Arrays (1)
Appending Elements to Arrays (2)
Multiplying Lists and Arrays
Doubling the Elements in a List
Lists and Exponents
Arrays and Exponents
Math Operations and Arrays
Working with “−1” Sub-ranges with Vectors
Working with “−1” Sub-ranges with Arrays
Other Useful NumPy Methods
Arrays and Vector Operations
NumPy and Dot Products (1)
NumPy and Dot Products (2)
NumPy and the Length of Vectors
NumPy and Other Operations
NumPy and the reshape() Method
Calculating the Mean and Standard Deviation
Code Sample with Mean and Standard Deviation
Trimmed Mean and Weighted Mean
Summary
Appendix B: Introduction to Pandas
What is Pandas?
Options and Settings
Data Frames
Data Frames and Data Cleaning Tasks
Alternatives to Pandas
A Data Frame with a NumPy Example
Describing a Data Frame
Boolean Data Frames
Transposing a Data Frame
Data Frames and Random Numbers
Reading CSV Files
Specifying a Separator and Column Sets in Text Files
Specifying an Index in Text Files
The loc() and iloc() Methods
Converting Categorical Data to Numeric Data
Matching and Splitting Strings
Converting Strings to Dates
Working with Date Ranges
Detecting Missing Dates
Interpolating Missing Dates
Other Operations with Dates
Merging and Splitting Columns
Reading HTML Web Pages
Saving a Data Frame as an HTML Web Page
Summary
Index