Python Basics
Introduction to Python
● A program is a set of instructions to perform a task.
● A programming language is used to write programs.
● Machine language uses 0s and 1s (hard for humans).
● High-level languages (e.g., Python, Java) are easier to write and understand.
● Python uses an interpreter, not a compiler.
● The interpreter executes code line by line.
Features of Python
● High-level, free and open-source.
● Interpreted language – executes one line at a time.
● Easy syntax, case-sensitive.
● Portable and platform-independent.
● Supports web development, has rich libraries.
● Uses indentation to define blocks of code.
Working with Python
● Requires Python interpreter (installed or online).
● >>> is the Python prompt.
Execution Modes
● Interactive Mode: run one command at a time.
● Script Mode: write, save, and run programs from .py files.
Keywords
● Reserved words with special meaning (e.g., if, for, return).
● Cannot be used as variable names.
● Case-sensitive.
Identifiers
● Names used for variables, functions, etc.
● Must begin with a letter or _; cannot start with digits.
● No special characters allowed (e.g., @, $).
● Cannot be a keyword.
Variables
● Hold values (e.g., strings, numbers).
● Created using assignment (e.g., x = 5).
● No need for explicit declaration in Python.
● Variables must be assigned before use.
Comments
● Start with #, not executed.
● Help document the code for clarity.
Note: All data values in Python are objects.
● Use id() to find the object’s identity.
Data Types
1 Numbers:
● int: 1, -2, 0
● float: 3.14, -5.0
● complex: 2+3j
● bool: True or False
2 Sequence Types:
● String: "Hello", '123'
● List: [1, 2.5, "Text"]
● Tuple: (10, 20, "Yes") – immutable
3 Set:
● {1, 2, 3} – unordered, no duplicates
4 None:
● Represents absence of value.
● Type: NoneType
5 Mapping:
● Dictionary: {key: value} – e.g., {"name": "Amit"}
Mutable & Immutable:
● Mutable: list, set, dictionary
● Immutable: int, float, string, tuple, bool
When to Use:
● List: changeable collection
● Tuple: fixed data
● Set: unique values
● Dictionary: key-value pairs
Operators
1 Arithmetic:
● +, -, *, /, //, %, **
2 Relational:
● ==, !=, >, <, >=, <=
3 Assignment:
● =, +=, -=, *=, /=, //=, %=, **=
4 Logical:
● and, or, not
5 Identity:
● is, is not
6 Membership:
● in, not in
Expressions
● Combination of variables, constants, operators.
● Always evaluates to a value.
Operator Precedence (Highest to Lowest):
1. **
2. ~, unary +, -
3. *, /, %, //
4. +, -
5. Relational (<, >, etc.)
6. Assignment (=, +=, etc.)
7. is, is not
8. in, not in
9. not
10. and
11. or
Statements
● Executable lines in a program.
● Types: assignment, print, input.
Input and Output
● Input: input() – always returns string.
● Output: print() – displays data.
print() Parameters:
● sep: separator between values.
● end: what to print at the end of the line (default is newline).
Type Conversion
Explicit:
● int(), float(), str(), chr(), ord()
● Done by the programmer
Implicit:
● Done automatically by Python.
● E.g., int + float → result is float.
Debugging
Types of Errors:
1. Syntax Errors – violates Python rules.
2. Logical Errors – wrong logic, wrong output.
3. Runtime Errors – crashes during execution (e.g., divide by zero).