cmps101 to summarize
Page 1: Introduction
Overview of Python Programming
Quote by Martin Fowler: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
Page 2: Introduction to Programming
Definition of a Computer Program
A computer program details the sequence of steps to perform a task.
Programming is the act of designing and implementing computer programs.
Programming Languages
A programming language allows communication between programmers and computers.
Popular languages:
Python
Javascript
Go
Java
Kotlin
PHP
C#
Swift
R
Ruby
C and C++
Matlab
TypeScript
Scala
SQL
HTML
CSS
NoSQL
Rust
Perl
Python is beginner-friendly and widely used for machine learning and web services.
Page 3: How Python Programs Run
Process of Execution
Compiler reads entire source code.
Translates instructions into byte code.
Virtual machine includes necessary libraries.
Executes byte code.
Example Programs
Exercise 1: Display "Hello World"
print("Hello, World!")Output: Hello, World!
Exercise 2: Display Name
print("Tom Smith")Output: Tom Smith
Exercise 3: Full Name in Two Lines
print("Tom")
print("Smith")Output:
Tom
Smith
Exercise 4: Full Name in One Print
print("Tom\nSmith")Output: Tom Smith
Page 4: Advanced Printing Techniques
Exercise 5: Full Name in Single Line
print("Tom", end=' ')
print("Smith")Output: Tom Smith
Exercise 6: Display Sum
print(6 + 5)Output: 11
Exercise 7: Variable Printing
X = 5
print("The value of X=", X)Output: The value of X= 5
Variable Naming Rules
Names must start with a letter or underscore.
Case sensitive.
Cannot include spaces or certain symbols.
Page 5: Variable Manipulation
Updating Variable Values
Assigning a new value replaces the previous one.
Example:
X = 5
X = 7
X = X + 3Output sequence: X=5, X=7, X=10
Page 6: Types of Numeric Data
Numeric Data Types in Python
Integer (int): Whole numbers without decimal.
Float (float): Real numbers with decimal notation.
Complex (complex): Numbers expressed as (real part) + (imaginary part)j. Example: 2 + 3j.
Strings are sequences of characters in quotes.
Python only has a string type (str) with no separate character type.
Page 7: Type Conversion
Types of Conversion
Implicit Conversion: Automatic type conversion by Python.
Explicit Conversion: Manual type conversion using functions like int(), float().
Example of Type Conversion
Implicit Example
a = 13
b = 2.5
z = a + b
print(z)Output: 15.5
Explicit Example
a = '13'
b = "2"
z = int(a) + int(b)
print(z)Output: 15
Page 8: Arithmetic and Comparison Operators
Arithmetic Operators
Basic operations: +, -, *, /, %, ** for power, // for integer division.
Example Operations
print(7 + 2) # Addition
print(7 - 2) # SubtractionPage 9: Assignment Operators
Operators for assigning values: =, +=, -=, etc.
Example:
# Assignment
a = 7
# Addition Assignment
a += 2 # a = a + 2Page 10: Comparison Operators
Operators and Usage
Comparison Operators: ==, !=, >, <, >=, <=.
Example:
print(a == b)
print(a != b)Page 11: Logical Operators
Used to combine conditional statements: and, or, not.
Example of Logical Condition:
if num < 10 and num > 0:
print("Valid")Page 12: Operator Precedence
Determines the order of operations in expressions.
Highest precedence: arithmetic operators.
Next precedence: relational, then logical operators.
Page 13: Mathematical Functions in Python
Provides access to various mathematical functionality, e.g., math.ceil, math.floor.
Page 14: Mathematical Operations
import math
math.sqrt(16)Output: 4
Page 15: Writing Math Formulas
Using Python for Math
Examples of mathematical expressions using Python syntax.
Page 16: Exercises
Suggested exercises for practice with basic Python operations and formulas.
Page 17: Control Structures
Introduction to Control Structures
if,else,elifstatements for decision making.
Page 18: Using if Statements
Example of if Statement
num = int(input('Enter a number: '))
if num > 10:
print("Greater than 10")Page 19: Logical Operators
Combining conditions using logical operators.
Page 20: else and elif usage
Allows branching based on multiple conditions.
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")Page 21: Loops
Introduction to Loops
Used to repeat a block of code.
Page 22: Tracing Variables
Tracing Variables in Loops
Keeping track of variable changes throughout loops.
num = 0
while num < 5:
print(num)
num += 1Page 23: Using Break and Continue
Control Flow in Loops
breakexits the loop.continueskips to the next iteration.
Page 24: Infinite Loop Warning
Caution against creating infinite loops without exit conditions.
while (1):
print("Infinite Loop")Page 25: Number Guessing Game
Using Random Module
Example of a guessing game implemented in Python.
Page 26: For Loops & Range Function
Using For Loops
Demonstrating the
forloop with therange()function.
for x in range(2, 6):
print(x)Page 27: Output Formatting with Loops
Nested loops for formatted outputs.
for i in range(1, 10):
for j in range(1, 10):
print(i * j, end=' ')
print()Page 28: String Formatting Methods
Different Ways to Format Strings
Percent Formatting.
Using
.format().Using f-strings.
Page 29: Counting Characters in Strings
Processing Strings
Count uppercase and lowercase letters in a string.
Page 30: Validating Phone Numbers
Example: Validating Input
Validating US phone number format.
Page 31: Programming Projects and Exercises
Suggested simple projects and exercises to reinforce learning.
Page 32: Function Definition in Python
Functions
A reusable block of code that performs a specific task. Defined using
defkeyword.
Page 33: Function Parameters and Return
Parameters and Return Values.
Functions can return values and accept parameters.
Page 34: Scope of Variables in Functions
Variable Scope
Understanding local and global variable scopes.
Page 35: Recursive Functions
Recursion Explained
Functions that call themselves with a base case to terminate.
Page 36: Lambda Functions
Anonymous Functions
Defined using
lambdakeyword for single-expression functions.
Page 37: Advanced Function Exercises
Suggested Exercises
Practice with functions and recursion in Python.
Page 38: Working with Lists
List Fundamentals
Lists are dynamic arrays to store multiple items.
Page 39: Operations on Lists
Appending and Inserting
Elements can be added or modified in lists.
Page 40: List Methods
Common List Operations
Methods like
pop(),remove(), and list comprehension.
Page 41: Copying Lists
Copying and Reference
Reference vs values when copying.
Page 42: Tuples
Tuple Fundamentals
Similar to lists but immutable.
Page 43: Working with Tuples
Tuple Behavior
Unpacking and iterating through tuples.
Page 44: File Handling in Python
Reading and Writing Files
Basic file operations using the open() function.
Page 45: Reading CSV Files
CSV Files
Simplified data representation for tabular data.
Page 46: Additional File Operations
Writing Data to Files
Operations for appending to or writing files.
Page 47: Using CSV Module
Best Practices for CSV
Using Python's csv module for file operations.
Page 48: Data Management with CSV
Reading from CSV
Demonstrating read operations on CSV format files.
Page 49: File Content Manipulation
Content Processing
Manipulating and processing file contents for analysis.
Page 50: Population Data Example
Real-world Data Application
Using file reading/writing for population data.
Page 51: Dictionary Basics
Understanding Dictionaries in Python
Key-value data structures.
Page 52: Operations on Dictionaries
Basic Dictionary Operations
Add, edit and retrieve values using key.
Page 53: Nested Dictionaries
Dictionary with Nested Structures.
Example of storing complex data in dictionaries.
Page 54: Exercises on Dictionaries
Suggested Exercises
Tasks for practicing dictionary operations.
Page 55: Summary and Review
Recap of Python Basics
Overview of all topics for quick reference.