Python for Problem Solving
Python for Problem Solving
• Introduction to Python
o What is Python?
o History of Python
o Key Features of Python
▪ Python is Easy to Learn and Use
▪ High Level Language
▪ Python is Interpreted
▪ Dynamic Typed
▪ Object Oriented
▪ Extensive Library are Available
▪ Open-Source with Huge community Support
▪ Cross Platform
▪ Good Career Opportunities
o What can you do with Python?
o Why Learn Python?
o Limitation of Python
▪ Performance Limitations
▪ Not preferred for Mobile Apps and Front End
▪ Memory Expensive
▪ Lack of Strong Typing
▪ Not Suitable for Game development
o Python Versions
• Syntax
o Indentation in Python
o Python Variables
o Python Identifiers
▪ Rules
o Python keywords
o Comments in Python
o Multiple Line Statements
o Continuation of Statements in Python
o Taking Input from User in Python
• Comments
o Single-Line Comments
o Multi-Line Comments
▪ Using String Literals as Comment
o Best Practice to Write Comments
• Variables
o Declaration and Initialization of Variables
o Redeclaring variables in Python
o Python Assign Values to Multiple Variables
o Assigning different values to multiple variables
o Can We Use the Same Name for Different Types?
o How does + operator work with variables?
o Can we use + for different Datatypes also?
o Global and Local Python Variables
o Global keyword in Python
▪ Rules of global keyword
o Variable Types in Python
o Object Reference in Python
• Keywords
o List of Keywords in Python
o Getting List of all Python keywords
o Value Keywords: True, False, None Keyword, del
o Operator Keywords: and, or, not, in, is
o Conditional keywords in Python: if, else, elif
o Iteration Keywords: for, while, break, continue, pass in Python
o Exception Handling Keywords
o del Keyword in Python
o Structure Keywords: def, class, return, lambda
o Return Keywords – Return, Yield
o Lambda Keyword in Python
o Context Keywords: With, as Keyword in Python
o as Keyword In Python
o Import and Module: Import, From in Python
o Global and nonlocal keyword uses in Python
o Async Programming: async, await
• Indentation and why is it important
o Why it is and with errors
2. Input/OutputPrint output using print() function
• Data Types
o Strings
▪ Creating a String
• Multi-line Strings
• Accessing characters in Python String
o Access string with Negative Indexing
o String Slicing
▪ String Immutability
▪ Deleting a String
▪ Updating a String
▪ Common String Methods
• len()
• upper() and lower()
• strip() and replace()
▪ Concatenating and Repeating Strings
▪ Formatting Strings
• Using f-strings
• Using format()
▪ Using in for String Membership Testing
o Numbers
▪ Python Integer
▪ Python Float
▪ Python Complex
▪ Type Conversion in Python
▪ Random Numbers
▪ Special Numbers in Python
• NaN (Not a Number)
• Infinity and -Infinity
o Boolean
▪ Python Boolean Type
▪ Evaluate Variables and Expressions
• Python bool() Function
• Integers and Floats as Boolean
▪ Boolean Operators
• Boolean OR Operator
• Boolean And Operator
• Boolean Not Operator
• Python Boolean == (equivalent) and != (not equivalent) Operator
• Python is Operator
• Python in Operator
o Python List
▪ Creating a List
• Using Square Brackets
• Using the list() Constructor
▪ Creating a List with Repeated Elements
▪ Accessing List Elements
▪ Modifying Lists
▪ Adding Elements into List
• append()
• extend()
• insert()
▪ Updating Elements into List
▪ Removing Elements from List
• remove()
• pop()
• del statement
▪ Iterating Over Lists
• Using for Loop
• Using For-Loop and Range() Method
• Using While-Loop
• Using Enumerate()
• Using List Comprehension
• Using Iter and Next Functions
▪ Nested Lists and Multidimensional Lists
o Python Tuples
▪ Creating a Tuple
• Creating a Tuple with Mixed Datatypes.
▪ Python Tuple Operations
• Accessing of Python Tuples
• Concatenation of Tuples
• Slicing of Tuple
• Deleting a Tuple
▪ Built-In Methods
▪ Tuples VS Lists
o Python Sets
▪ Creating a Set in Python
• Creating a Python Set with Another Method
▪ Adding Elements to a Set in Python
• Using add() Method
• Using update() Method
▪ Accessing a Set in Python
▪ Removing Elements from the Set in Python
• Using remove() Method or discard() Method
• Using pop() Method
• Using clear() Method
▪ Frozen Sets in Python
▪ Typecasting Objects into Sets
▪ Advantages of Set in Python
• Unique Elements
• Fast Membership Testing
• Mathematical Set Operations
• Mutable
▪ Disadvantages of Sets in Python
• Unordered
• Limited Functionality
• Memory Usage
• Less Commonly Used
▪ Set Methods in Python
o Python Dictionary
▪ How to Create a Dictionary
▪ Accessing Dictionary Items
▪ Adding and Updating Dictionary Items
▪ Removing Dictionary Items
• del
• pop()
• clear()
• popitem()
▪ Iterating Through a Dictionary
• How to Loop Through a Dictionary in Python
o Iterate through Value
o Iterate through keys
o Iterate through both keys and values
o Iterating Python Dictionary Using map() and dict.get
o Iterate Python Dictionary using zip() Function
o Dictionary iteration in Python by unpacking the dictionary
▪ Nested Dictionaries
o Python Arrays
▪ Create an Array in Python
▪ Adding Elements to an Array
▪ Accessing Elements from Array
▪ Removing Elements from Array
▪ Slicing of an Array
▪ Searching Element in an Array
▪ Updating Elements in an Array
▪ Different Operations on Python Arrays
• Counting Elements in an Array
• Reversing Elements in an Array
• Extend Element from Array
• Type Casting
o There can be two types of Type Casting in Python
▪ Python Implicit Type Conversion
▪ Python Explicit Type Conversion
• Python Convert Int to Float
• Python Convert Float to Int
• Python Convert int to String
• Python Convert String to float
• Python Convert string to int
4. Python Operators
• Arithmetic Operators
o Addition Operator
o Subtraction Operator
o Multiplication Operator
o Division Operator
o Floor Division Operator
o Modulus Operator
o Exponentiation Operator
o Precedence of Arithmetic Operators in Python
o
• Comparison Operators
o Python Equality Operators a == b
o Inequality Operators a != b
o Greater than Sign a > b
o Less than Sign a < b
o Greater than or Equal to Sign x >= y
o Less than or Equal to Sign x <= y
o Chaining Comparison Operators
• Logical Operators
o Logical Operators in Python
▪ AND, Logical OR, and Logical NOT
▪ Order of Precedence of Logical Operators
▪ How do logical operators differ from bitwise operators in Python?
▪ Can logical operators be used with non-boolean values in Python?
• Bitwise Operators
o Bitwise AND Operator
o Bitwise OR Operator
o Bitwise XOR Operator
o Bitwise NOT Operator
o Bitwise Shift
▪ Bitwise Right Shift
▪ Bitwise Left Shift
o Bitwise Operator Overloading
o
• Assignment Operators
o Addition Assignment Operator
o Subtraction Assignment Operator
o Multiplication Assignment Operator
o Division Assignment Operator
o Modulus Assignment Operator
o Floor Division Assignment Operator
o Exponentiation Assignment Operator
o Bitwise AND Assignment Operator
o Bitwise OR Assignment Operator
o Bitwise XOR Assignment Operator
o Bitwise Right Shift Assignment Operator
o Bitwise Left Shift Assignment Operator
o Walrus Operator
• Membership & Identity Operators (in, is)
o Python Membership Operators
▪ Python IN Operator
▪ Python NOT IN Operator
o Python Identity Operators
▪ Python IS Operator
▪ Python IS NOT Operator
o Difference between ‘==’ and ‘is’ Operator
• if-else
o if Statement
o if….else Statement
• Nested if Statements
o if…elif…else Statement
• if-elif-else Ladder
o
• if-else on One Line
o Python Shorthandf If Else
o One Liner if elif else Statements
• Ternary Condition
o Python Ternary If Else
o Ternary Operator in Nested If else
o Ternary Operator using Python Tuple
o Ternary Operator using Python Dictionary
o Ternary Operator using Python Lambda
o Ternary Operator with Print Function
o Limitations of Python Ternary Operator
▪ Readability
▪ Potential for Error
▪ Debugging
▪ Maintenance and Extensibility
▪ Can’t use assignment statements
• Match Case Statement
o Python Match Case Statement Syntax
o Simple Match Case Statement
o Match Case Statement with OR Operator
o Match Case Statement with Python If Condition
o Match Case with the Sequence Pattern
o Match Case Statement with Python Dictionary
o Match Case Statement with Python Class
6. Python Loops
• for Loop
o How to use the for loop in Python
▪ Python For Loop with String
▪ Python for loop with Range
▪ Python for loop Enumerate
▪ Nested For Loops in Python
▪ Python For Loop Over List
▪ Python For Loop with Dictionary
▪ Python For Loop with Tuple
▪ Python For Loop with Zip()
o Control Statements that can be used with For Loop in Python
▪ Continue in Python For Loop
▪ Break in Python For Loop
▪ For Loop in Python with Pass Statement
▪ For Loops in Python with Else Statement
▪
• while Loop
o Syntax of while loop in Python
o Python While Loop
o Infinite while Loop in Python
o Control Statements in Python with Examples
▪ Python while loop with continue statement
▪ Python while loop with a break statement
▪ Python while loop with a pass statement
▪ While loop with else
▪ Sentinel Controlled Statement
▪ Python while loop with user input
▪ While loop with Boolean values
▪ Python while loop with Python list
▪ Single statement while block
• Loop Control Statements (break, continue, pass)
o Break statement
▪ Syntax of Break Statement
▪ Working on Python Break Statement
o Continue statement
▪ Syntax of Continue Statement
▪ Working of Python Continue Statement
o Pass statement
▪ Syntax of Pass Statement
• List Comprehension
o Syntax of list comprehension
o for loop vs. list comprehension
o Conditional statements in list comprehension
• Dictionary Comprehension
7. Python Functions
• Built-in Modules
• Python DSA Libraries
• Machine Learning Libraries
• Python GUI Libraries
• Web Scraping Packages
• Game Development Packages
• Web Frameworks (Django, Flask)
• Image Processing Libraries (OpenCV, Pillow)
• Counters
• Heapq
• Deque
• OrderedDict
• Defaultdict