Knowlet

Unit I: Introduction to Python


1. Basic Elements of Python

Python is a high-level, interpreted programming language known for its readability and simple syntax. The basic elements include character sets and the use of comments to explain code.

  • Comments: Used to add notes within the code. Python uses the # symbol for single-line comments.

2. Python Character Set & Tokens

A token is the smallest individual unit in a python program.

  • Keywords: Reserved words that have special meaning to the interpreter (e.g., if, else, while).
  • Identifiers: Names given to entities like variables, functions, and classes.
  • Literals: Constant values stored in variables (e.g., 10, "Hello").
  • Operators: Symbols that perform operations on variables and values.
  • Punctuators: Symbols used to organize sentence structures and expressions (e.g., , : [ ] { }).

3. Variables, L-values, and R-values

Variables are reserved memory locations to store values.

  • L-value: Expressions that refer to a memory location (e.g., a variable on the left side of an assignment).
  • R-value: The data value stored at some address in memory (e.g., the value/expression on the right side of an assignment).

4. Knowledge of Data Types

Python provides several built-in data types to handle different kinds of data:

Category Data Types
Number Integer, Floating point, Complex
Boolean True, False
Sequence String, List, Tuple
Mapping Dictionary
None Represents the absence of a value

5. Mutable vs Immutable Types

Understanding mutability is critical for managing data in Python.

  • Mutable Types: Values can be changed after they are created (e.g., Lists, Dictionaries, Sets).
  • Immutable Types: Values cannot be changed once created (e.g., Numbers, Strings, Tuples).

6. Operators & Expressions

Operators are symbols used to perform computations.

  • Arithmetic: +, -, *, /, %, **, //.
  • Relational: ==, !=, >, <, >=, <=.
  • Logical: and, or, not.
  • Assignment: =, +=, -=, *=, /=.
  • Identity: is, is not.
  • Membership: in, not in.
Precedence of Operators: Determines the order in which operators are evaluated in an expression (e.g., multiplication before addition).

7. Input/Output & Type Conversion

  • Input: The input() function is used to accept data from the console.
  • Output: The print() function displays output to the console.
  • Implicit Conversion: Python automatically converts one data type to another without user involvement.
  • Explicit Conversion (Type Casting): The user manually converts the data type using functions like int(), float(), or str().

Exam Tips & Warnings

  • Division Tip: In Python 3, / always returns a float, while // (floor division) returns an integer.
  • Identifier Rules: Identifiers cannot start with a digit and cannot be a keyword.
  • Case Sensitivity: Python is case-sensitive; Variable and variable are different.
Mnemonic for Tokens: KILOP (Keywords, Identifiers, Literals, Operators, Punctuators).

Frequently Asked Questions

Q: What is the difference between a list and a tuple?
A list is mutable (can be changed), while a tuple is immutable (cannot be changed).

Q: What is a complex number in Python?
It is a number with a real and imaginary part, represented as a + bj.

Did this resource help you study?

Share feedback or report issues to help improve this resource.