PYTHON
1. Introduction to Python
High-level Language: Easy to understand and write.
Interpreted: Executed line by line.
Case-sensitive: NUMBER and number are different.
Portable: Runs on various operating systems.
Rich Library: Predefined functions for various tasks.
2. Execution Modes
Interactive Mode: Execute one line at a time.
Script Mode: Write and execute multiple lines in a file.
3. Python Keywords and Identifiers
Keywords: Reserved words with specific meanings (e.g., if, else, for).
Identifiers: Names given to variables, functions, etc. Rules:
o Start with a letter or underscore.
o Can contain letters, digits, or underscores.
o Cannot be a keyword.
4. Variables and Data Types
Variables: Store data values.
Data Types:
o Numbers: int, float, complex.
o Sequences: str, list, tuple.
o Sets: Unordered collection of unique elements.
o Dictionaries: Key-value pairs.
5. Operators
Arithmetic: +, -, *, /, %, //, **.
Relational: ==, !=, >, <, >=, <=.
Logical: and, or, not.
Assignment: =, +=, -=, *=, /=, %=, //=, **=.
Membership: in, not in.
Identity: is, is not.
6. Input and Output
Input: input() function to take user input.
Output: print() function to display output.
7. Type Conversion
Explicit: Manually convert data types using functions like int(), float(), str().
Implicit: Python automatically converts data types during operations.
8. Debugging
Syntax Errors: Mistakes in code structure.
Logical Errors: Code runs but produces incorrect results.
Runtime Errors: Errors during execution (e.g., division by zero).
Functions in Python
1. Introduction to Functions
Definition: A block of code that performs a specific task.
Advantages:
o Increases code readability.
o Reduces code duplication.
o Enhances reusability.
2. User-Defined Functions
Syntax:
def function_name(parameters):
# Function body
return value
Arguments and Parameters: Values passed to a function during a call.
Return Statement: Returns a value from the function.
3. Scope of Variables
Global Variables: Accessible throughout the program.
Local Variables: Accessible only within the function.
4. Built-in Functions
Examples: len(), type(), input(), print(), sum(), min(), max().
5. Modules
Definition: A file containing Python code (functions, variables, etc.).
Importing Modules: Use import module_name to use functions from a module.
Strings, Lists, Tuples, and Dictionaries
1. Strings
Definition: Sequence of characters enclosed in quotes.
Operations: Concatenation (+), repetition (*), slicing ([start:end]), membership (in, not in).
Methods: len(), lower(), upper(), strip(), split(), replace().
2. Lists
Definition: Ordered, mutable sequence of elements.
Operations: Concatenation, repetition, slicing, membership.
Methods: append(), extend(), insert(), remove(), pop(), sort(), reverse().
3. Tuples
Definition: Ordered, immutable sequence of elements.
Operations: Concatenation, repetition, slicing, membership.
Methods: count(), index().
4. Dictionaries
Definition: Unordered collection of key-value pairs.
Operations: Accessing values using keys, adding/updating key-value pairs, deleting keys.
Methods: keys(), values(), items(), get(), update(), pop().