Skip to content
AL-306 · Computer Workshop/Introduction to Python-I/Quick Revision Short Notes

Computer Workshop/Introduction to Python-I (AL-306) - Unit 1 Short Notes

How unit 1 is examined

This unit covers Python's basics: syntax, literals, numbers, variables, strings, operators, indentation, input, output, functions and comments. No past questions are tagged, so every topic is a short, complete definition with key points.

Introduction to python language

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Python is a high-level, interpreted, general-purpose, dynamically typed programming language created by Guido van Rossum and released in 1991.</mark>

Key points.

  1. Python is interpreted, so code runs line by line without a separate compile step.
  2. It is dynamically typed, so a variable's type is decided at run time and needs no declaration.
  3. Its syntax is short and readable, and blocks are marked by indentation instead of braces.
  4. It is free, open source and portable across Windows, Linux and macOS.
  5. It supports procedural, object-oriented and functional styles and has a large standard library.

Basic syntax

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Syntax is the set of rules that decide how a valid Python statement is written.</mark>

Key points.

  1. Each statement normally ends at the end of the line; no semicolon is needed.
  2. Python is case-sensitive, so Total and total are different names.
  3. Identifiers may contain letters, digits and underscore, and cannot start with a digit or be a keyword such as if, for or class.
  4. A long statement can continue on the next line with a backslash \ or inside brackets.
  5. Blocks are defined by indentation, and a line ending with a colon opens a block.

Literal Constants

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A literal is a fixed value written directly in the source code, such as 10, 3.5, 'hi' or True.</mark>

Key points.

  1. Numeric literals are integers (25, 0b101, 0o17, 0xFF), floats (3.14, 1e3) and complex numbers (2+3j).
  2. String literals are text in single, double or triple quotes.
  3. Boolean literals are True and False.
  4. The special literal None means "no value".
  5. Collection literals include list [1,2], tuple (1,2), dictionary {'a':1} and set {1,2}.

Numbers

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Python has three numeric types: int for whole numbers, float for real numbers with a decimal point, and complex for numbers of the form a+bj.</mark>

Key points.

  1. An int has unlimited size, so very large integers never overflow.
  2. A float is stored in binary, so 0.1 + 0.2 gives 0.30000000000000004.
  3. A complex number has a real and an imaginary part, for example 3+2j.
  4. type() shows the type of a value, and int(), float() and complex() convert between types.
  5. bool is a subtype of int, with True equal to 1 and False equal to 0.

Variable and Basic data types

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. ==A variable is a named memory location that refers to a value; it is created by assignment, for example x = 5.==

Key points.

  1. No type declaration is needed; the type comes from the value, and the same name can later refer to a value of another type.
  2. Basic types are int, float, complex, str, bool and NoneType.
  3. Multiple assignment is allowed, for example a, b = 1, 2 and a = b = 0.
  4. type(x) gives the type and id(x) gives the identity of the object.
  5. Variable names should be meaningful, lowercase and follow the identifier rules.
x = 5           # int
x = "five"      # now str
print(type(x))  # <class 'str'>

String

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A string is an immutable sequence of characters written inside single, double or triple quotes.</mark>

Key points.

  1. Triple quotes allow a string to span several lines.
  2. Indexing starts at 0, and negative indexes count from the end, so 'hello'[1:3] is 'el' and 'hello'[::-1] is 'olleh'.
  3. Strings are immutable, so a character cannot be changed in place.
  4. + joins strings, * repeats them, and len() gives the length.
  5. Common methods are upper(), lower(), strip(), split(), replace() and find().

Escape Sequences

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An escape sequence is a backslash followed by a character that stands for a special character inside a string.</mark>

Key points.

Sequence Meaning
\n new line
\t tab
\\ backslash
\' and \" quote characters
  1. A raw string such as r"C:\new" turns off escape processing.
  2. Without escaping, \n in a path would be read as a new line.

Operators and Expressions

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>An operator is a symbol that acts on operands, and an expression is a combination of operands and operators that evaluates to a value.</mark>

Key points.

  1. Arithmetic operators are + - * / // % **, where / gives a float, // floors, so 7//2 is 3 and -7//2 is -4, and ** is power.
  2. Relational operators == != < > <= >= return True or False, and chaining such as 10>5>3 is allowed.
  3. Logical operators are and, or and not.
  4. Assignment operators include =, +=, -= and *=.
  5. Other operators are bitwise & | ^ ~ << >>, membership in, not in, and identity is, is not.

Evaluation Order

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Operator precedence decides which operator is evaluated first in an expression, and associativity decides the order among operators of equal precedence.</mark>

Key points.

  1. The order from highest to lowest is: brackets, **, unary -, * / // %, + -, comparisons, not, and, or.
  2. Most operators associate left to right, but ** associates right to left, so 2**3**2 is 512.
  3. ** binds tighter than unary minus, so -3**2 is -9.
  4. For example 2+3*4**2 is 2+3*16 = 2+48 = 50.
  5. Brackets override precedence and make the order clear.

Indentation

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Indentation is the leading whitespace at the start of a line, and Python uses it to define blocks of code.</mark>

Key points.

  1. All statements in one block must have the same indentation, and four spaces is the standard.
  2. A block starts after a colon in if, for, while, def and class.
  3. Wrong indentation raises IndentationError.
  4. Mixing tabs and spaces should be avoided.
if 5 > 3:
    print("yes")   # inside the block

Input

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>input() reads a line typed by the user and returns it as a string.</mark>

Key points.

  1. An optional prompt can be given, as in input("Enter: ").
  2. The result is always a str, so int() or float() is needed for numbers.
  3. Example: n = int(input("Enter n: ")).
  4. Several values can be read with a, b = map(int, input().split()).

Output

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>print() displays values on the screen.</mark>

Key points.

  1. Multiple values are separated by a space by default, and sep changes the separator.
  2. end changes the ending, which is a new line by default, so print('a','b',sep='-',end='!') shows a-b!.
  3. Formatting can use f-strings such as f"x = {x}", format() or %.
  4. print() accepts any object and converts it to text.

Functions

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A function is a named, reusable block of code defined with def that runs when it is called and may return a value.</mark>

Key points.

  1. The syntax is def name(parameters): followed by an indented body.
  2. return sends a value back; without it the function returns None.
  3. Arguments can be positional, keyword or default, as in def f(a, b=2).
  4. Functions give reuse and modularity, and built-in ones include len(), print() and type().
def add(a, b=2):
    return a + b
print(add(3))   # 5

Comments

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A comment is text ignored by the interpreter that explains the code to the reader.</mark>

Key points.

  1. A single-line comment starts with # and runs to the end of the line.
  2. Python has no true multi-line comment; a triple-quoted string is used as a docstring or a block note.
  3. Comments make code readable and can temporarily disable a line.

Last-minute revision

  • Python is high-level, interpreted, dynamically typed, created by Guido van Rossum in 1991.
  • Blocks are marked by indentation after a colon; wrong indentation raises IndentationError.
  • Numeric types are int, float and complex; int has unlimited size.
  • Strings are immutable; 'hello'[1:3] is 'el'.
  • / gives a float, // floors (-7//2 is -4), ** is power.
  • 2**3**2 is 512 and -3**2 is -9.
  • input() always returns a string.
  • print(sep=, end=) control separator and ending.
  • def defines a function; no return means None.
  • # starts a comment.
  • Escape sequences: \n, \t, \\.

Memory hooks

  • PEMDAS-like: brackets, power, times/divide, plus/minus, compare, not, and, or.
  • "input() is always a string": convert before adding.
  • Colon opens a block, indentation fills it.
  • // floors toward minus infinity.

Coverage checklist

  • Introduction to python language: definition and features.
  • Basic syntax: statements, case sensitivity, identifiers.
  • Literal Constants: numeric, string, boolean, None.
  • Numbers: int, float, complex.
  • Variable and Basic data types: assignment, dynamic typing.
  • String: quotes, slicing, immutability.
  • Escape Sequences: \n, \t, raw strings.
  • Operators and Expressions: all operator groups.
  • Evaluation Order: precedence and associativity.
  • Indentation: blocks and IndentationError.
  • Input: input() and conversion.
  • Output: print() with sep and end.
  • Functions: def, return, arguments.
  • Comments: # and docstrings.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in