0% found this document useful (0 votes)
4 views9 pages

Ch5 Getting Started With Python Notes Part2

Chapter 5 covers the basics of Python, including operators (arithmetic, relational, assignment, logical, identity, and membership), expressions, statements, input/output functions, type conversion, and debugging. It explains how operators work, the importance of operator precedence, and how to handle user input and output formatting. Additionally, it outlines common types of errors in programming and their debugging approaches.

Uploaded by

swatiankola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Ch5 Getting Started With Python Notes Part2

Chapter 5 covers the basics of Python, including operators (arithmetic, relational, assignment, logical, identity, and membership), expressions, statements, input/output functions, type conversion, and debugging. It explains how operators work, the importance of operator precedence, and how to handle user input and output formatting. Additionally, it outlines common types of errors in programming and their debugging approaches.

Uploaded by

swatiankola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.

13 + Summary)

CHAPTER 5

Getting Started with Python


Study Notes — Part 2 of 2 (Sections 5.8 – 5.13 + Chapter Summary)

5.8 Operators
An operator performs a specific mathematical or logical action on values called operands. In 10 + num, the
operands are 10 and num, and + is the operator.

5.8.1 Arithmetic Operators


Operator Meaning Example

+ Addition (also concatenates strings) 5 + 6 → 11 | 'Hi'+'Bye' → 'HiBye'

- Subtraction 5 - 6 → -1

* Multiplication (repeats a string if other operand is int) 5*6 → 30 | 'Hi'*2 → 'HiHi'

/ Division — always returns a float 8 / 4 → 2.0

% Modulus — remainder of division 13 % 5 → 3

// Floor division — quotient with decimal part dropped 13 // 4 → 3

** Exponent — power 3 ** 4 → 81

5.8.2 Relational (Comparison) Operators


Assume a = 10, b = 0, c = 10 for the examples below.

Operator True when… Example

== Operands are equal a == c → True

!= Operands are not equal a != b → True

> Left operand is greater a > b → True

< Left operand is smaller a < c → False

>= Left is greater than or equal a >= c → True

<= Left is less than or equal b <= a → True

■ Conceptual Trap
• Relational operators also work on strings — Python compares them lexicographically, character by character, using each
character's ASCII value.

Page 1
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

5.8.3 Assignment Operators


Operator Same as Example (x = 10 initially)

= assigns value x = 10

+= x=x+y x += 5 → x becomes 15

-= x=x-y x -= 5 → x becomes 5

*= x=x*y x *= 3 → x becomes 30

/= x=x/y x /= 2 → x becomes 5.0

%= x=x%y x %= 3 → x becomes 1

//= x = x // y x //= 3 → x becomes 3

**= x = x ** y x **= 2 → x becomes 100

5.8.4 Logical Operators


Written only in lowercase — and, or, not. Every value in Python is logically True or False by default; only None,
False, 0 and empty collections ("", (), [], {}) count as False.

Operator Rule Example

and True only if BOTH operands are True True and False → False

or True if AT LEAST ONE operand is True True or False → True

not Reverses the logical state (unary) not True → False

5.8.5 Identity Operators


is and is not check whether two names refer to the exact same object in memory (i.e., whether their id() values
match) — not merely whether their values are equal.
>>> a = 5
>>> b = a
>>> a is b # True — same object
>>> a is not b # False

5.8.6 Membership Operators


in and not in test whether a value exists inside a sequence such as a list, tuple, or string.
>>> nums = [1, 2, 3]
>>> 2 in nums # True
>>> 10 not in nums # True

■ 30-Second Revision
✓ Arithmetic: + - * / % // ** (note: / always gives float, // drops decimals)
✓ Relational: == != > < >= <= → always evaluate to True/False
✓ Assignment: = += -= *= /= %= //= **= → shorthand for update-and-assign
✓ Logical: and, or, not — lowercase only; default falsy values are None/False/0/empty
✓ Identity: is, is not — compare memory identity, not just value
✓ Membership: in, not in — check presence inside a sequence
Keywords: operator • operand • arithmetic • relational • assignment • logical • identity • membership
One-line summary: Python's six operator families — arithmetic, relational, assignment, logical, identity and membership —
cover computation, comparison, updating, boolean logic, object identity, and sequence checks.

Page 2
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

5.9 Expressions
An expression is any combination of constants, variables and operators that evaluates to a single value. Even a
bare constant or a lone variable counts as an expression — but a standalone operator by itself does not.

5.9.1 Operator Precedence


When an expression mixes several operator types, precedence decides which one is applied first. Unary operators
(single-operand, like unary minus) bind tighter than binary ones.

Precedence (high → Operator(s) Description


low)

1 ** Exponentiation

2 ~, +, - Complement, unary plus/minus

3 *, /, %, // Multiply, divide, modulus, floor division

4 +, - Addition, subtraction

5 <=, <, >, >=, ==, != Relational / comparison

6 =, +=, -=, *=, /=, //=, %=, **= Assignment

7 is, is not Identity

8 in, not in Membership

9 not Logical NOT

10 and Logical AND

11 or Logical OR

■ Must Remember
• Parentheses ( ) override precedence — whatever is inside is evaluated first.
• Operators of equal precedence are evaluated left to right.

Worked Examples
Expression Step-by-step evaluation

15 + 20 * 2 * has higher precedence → 15 + 40 = 55

30 - 10 + 5 Equal precedence, left→right → (30-10)+5 = 25

(15 + 5) * 3 Parentheses first → 20 * 3 = 60

18.0 / 4 + (6 + 2.0) Bracket first: (6+2.0)=8.0 → 18.0/4=4.5 → 4.5+8.0 = 12.5

■ 30-Second Revision
✓ An expression = constants + variables + operators, evaluating to one value
✓ Exponentiation (**) has the highest precedence; or has the lowest
✓ Equal-precedence operators evaluate left to right
✓ Parentheses always override default precedence
Keywords: expression • operator precedence • unary operator • binary operator
One-line summary: Expressions combine values and operators into one evaluated result, governed by a strict precedence
order that parentheses can always override.

Page 3
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

5.10 Statement
A statement is a complete unit of code that the Python interpreter can execute — e.g. an assignment, a print
instruction, or a loop line. Every line of runnable Python code is a statement.
x = 5 # assignment statement
square = x ** 2 # assignment statement
print(x, square) # print (function-call) statement

■ 30-Second Revision
✓ A statement is one executable instruction the interpreter can carry out
✓ Assignment, print, and control-flow lines are all statements
Keywords: statement
One-line summary: A statement is any single executable line of Python code — from assignments to function calls.

5.11 Input and Output


The input() Function
input() pauses the program, shows an optional prompt string, and waits for the user to type something and press
Enter. Whatever is typed is always returned as a string — even if the user types digits.
>>> name = input("Enter your name: ")
Enter your name: Kavya
>>> age = input("Enter your age: ")
Enter your age: 17
>>> type(age)
# note: still a string, not an int!

■ Exam Trap
• input() ALWAYS returns a string. To use it as a number, wrap it explicitly: age = int(input("Age: ")).

The print() Function


print() sends output to the screen and, by default, moves to a new line afterward. Its general form is
print(value, ..., sep=' ', end='\n').

Parameter Purpose Default

sep Text placed between multiple values a single space ' '

end Text placed after the very last value newline '\n'
>>> print("Score") → Score
>>> print("a" + "b" + "c") → abc (+ never adds a space)
>>> print("a", 5, "c") → a 5 c (comma adds a space, mixes types)
>>> print(1, 2, 3, sep='-') → 1-2-3

■ 30-Second Revision
✓ input() displays an optional prompt and always returns a string
✓ Convert with int()/float() to use numeric input in calculations
✓ print() writes to the screen; sep controls spacing between values, end controls the line-ending
✓ '+' concatenates strings only (same type); comma in print() can mix types
Keywords: input() • print() • sep • end • prompt
One-line summary: input() always hands back a string that may need explicit conversion, while print() offers sep and end to
control exactly how output is formatted.

Page 4
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

5.12 Type Conversion


Type conversion is changing a value from one data type to another. It happens in two ways — explicit (the
programmer forces it) and implicit (Python does it automatically).

5.12.1 Explicit Conversion (Type Casting)


The general form is new_type(expression). Because this can discard information (e.g. a decimal part), it must
be requested deliberately by the programmer.

Function Converts to

int(x) Integer

float(x) Floating-point number

str(x) String

chr(x) Character from an ASCII code

ord(x) ASCII code from a character


>>> price = 25.99
>>> int(price) # 25 — the .99 is simply discarded, not rounded

>>> total = 70
>>> print("Bill: Rs." + total) # TypeError! Can't add int to str
>>> print("Bill: Rs." + str(total)) # 'Bill: Rs.70' — fixed with explicit casting

■ Common Mistake
• int(25.99) truncates to 25 — it does NOT round to 26.
• Mixing a string and a number with + without casting raises TypeError.

5.12.2 Implicit Conversion (Coercion)


Python converts automatically only when it is completely safe — i.e., no information is lost. When an int and a float
are combined, Python widens the int to a float rather than the reverse, since narrowing a float to an int would drop
its decimal part.
>>> whole = 10 # int
>>> decimal = 4.5 # float
>>> result = whole + decimal
>>> print(result, type(result))
14.5 # int silently widened to float — no data lost

■ 30-Second Revision
✓ Explicit conversion = programmer-forced, via int()/float()/str()/chr()/ord()
✓ int() truncates decimals — does not round
✓ Combining str + int with '+' without casting raises TypeError
✓ Implicit conversion happens automatically only when no data would be lost (int→float)
Keywords: type conversion • explicit conversion • type casting • implicit conversion • coercion
One-line summary: Type conversion reshapes a value's type either explicitly (programmer-forced, risk of data loss) or implicitly
(automatic, only when safe).

5.13 Debugging
Debugging is the process of finding and fixing mistakes — bugs — in a program. Errors fall into three categories:

Page 5
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

Error type What happens Example

Syntax error Breaks Python's grammar rules; program will not Missing closing bracket: (7 + 11
run at all

Logical error Program runs fully and gives output, but the output Writing 10 + 12/2 when you meant (10+12)/2 for
is wrong an average

Runtime error Program is syntactically fine but fails mid-execution Dividing by zero; converting the text 'apple' to
int()

■ Must Remember
• A syntax error stops the program before it ever starts running.
• A logical error is the hardest to catch — the program runs successfully but the answer is wrong; you must trace backward
from the output to spot it.
• A runtime error (also called an exception) appears only once execution reaches the faulty line.
• Logical errors are also called semantic errors, since the meaning of the code is off.

■ 30-Second Revision
✓ Debugging = finding & removing bugs/errors from a program
✓ Syntax error: breaks Python's rules, caught before execution
✓ Logical/semantic error: runs fine, produces wrong output
✓ Runtime error: syntactically correct, fails while executing
Keywords: debugging • bug • syntax error • logical error • semantic error • runtime error
One-line summary: Errors are syntax (won't run), logical/semantic (runs but wrong), or runtime (fails mid-execution) — each
needs a different debugging approach.

Page 6
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

★ Chapter-End Revision Pack


Chapter Mind Map
GETTING STARTED WITH PYTHON

■■ Language Basics
■ ■■ Program → Algorithm implemented in code
■ ■■ Compiler (all-at-once) vs Interpreter (line-by-line, Python's choice)
■ ■■ Interactive mode (instant) vs Script mode (saved .py file)

■■ Naming
■ ■■ Keywords → 33 reserved words, fixed meaning
■ ■■ Identifiers → rule-bound names for variables/functions
■ ■■ Variables → dynamically-typed name-to-object bindings

■■ Data
■ ■■ Everything is an Object → unique id()
■ ■■ Numbers → int, float, complex, bool
■ ■■ Sequences → String, List, Tuple
■ ■■ Set {} → unordered, unique items
■ ■■ None → absence of value
■ ■■ Dictionary → key : value pairs
■ ■■ Mutable (list/set/dict) vs Immutable (int/float/bool/str/tuple)

■■ Operators & Expressions
■ ■■ Arithmetic, Relational, Assignment
■ ■■ Logical, Identity, Membership
■ ■■ Precedence: ** > unary > */%// > +- > relational > assignment > is > in > not > a
nd > or

■■ I/O
■ ■■ input() → always returns a string
■ ■■ print() → sep and end control formatting

■■ Type Conversion
■ ■■ Explicit (int/float/str/chr/ord) → programmer-forced
■ ■■ Implicit (coercion) → automatic, only when safe

■■ Debugging
■■ Syntax error → won't run
■■ Logical/semantic error → runs, wrong output
■■ Runtime error → fails mid-execution

Concept Connections
■ Variables are identifiers that must follow the same naming rules, and can never be a keyword.
■ Every variable's value is an object with a data type, which decides whether it is mutable or immutable.
■ Operators act on values of specific data types — e.g. + on numbers adds, but + on strings concatenates.
■ input() always returns a string, so it frequently needs explicit type conversion before being used in arithmetic
expressions.
■ A wrong data-type assumption in an expression often causes a runtime error (TypeError), which is discovered
only through debugging.

Page 7
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

Master Keyword Sheet


program, programming language, source code, compiler, interpreter, interactive mode, script mode, keyword,
identifier, variable, implicit declaration, comment, object, id(), data type, int, float, complex, bool, string, list, tuple,
set, None, dictionary, mutable, immutable, operator, operand, arithmetic operator, relational operator, assignment
operator, logical operator, identity operator, membership operator, expression, operator precedence, statement,
input(), print(), sep, end, type conversion, explicit conversion, type casting, implicit conversion, coercion,
debugging, bug, syntax error, logical error, semantic error, runtime error.

Definitions Sheet
Term Definition

Program An ordered set of instructions executed by a computer to perform a task

Keyword A reserved word with a fixed meaning to the interpreter; cannot be used as an identifier

Identifier A name used to identify a variable, function or other program entity

Variable An identifier referring to an object stored in memory

Comment Non-executable text starting with # meant for human readers

Data type Classification that decides what values a variable can hold and what operations apply

Mutable A data type whose value can be changed in place after creation

Immutable A data type that cannot be changed in place after creation

Operator A symbol that performs a specific operation on operands

Expression A combination of constants, variables and operators that evaluates to a value

Statement A complete unit of code the interpreter can execute

Type conversion Changing a value from one data type to another

Debugging The process of identifying and removing errors (bugs) from a program

Operator Precedence — Quick Formula Box


HIGH **
■ ~ , unary+ , unary-
■ * / % //
■ + -
■ <= < > >= == !=
■ = += -= *= /= //= %= **=
■ is , is not
■ in , not in
■ not
■ and
LOW or

Tables Summary — Data Type Mutability


Immutable Mutable

int · float · bool · complex · string · tuple list · set · dictionary

Page 8
Class XI Computer Science | Ch. 5 — Getting Started with Python Part 2 of 2 (5.8 – 5.13 + Summary)

One-Page Cheat Sheet


Everything on one page
• Program = instructions; Python = interpreted, case-sensitive, indentation-based.
• Keyword = reserved word (33 total) — never usable as a name.
• Identifier rule: start with letter/underscore, then letters/digits/underscores, no symbols, not a keyword.
• Variable = name bound to an object; type inferred automatically at assignment.
• Comment: starts with #, ignored by interpreter.
• Data types: Numbers (int/float/complex/bool) · Sequences (str/list/tuple) · Set · None · Dictionary.
• Mutable: list, set, dict. Immutable: int, float, bool, complex, str, tuple.
• Operators: Arithmetic + - * / % // ** | Relational == != > < >= <= | Assignment = += -= etc. | Logical and/or/not | Identity is/is
not | Membership in/not in.
• Precedence (high→low): ** → unary → */%// → +- → relational → assignment → is → in → not → and → or.
• input() always returns a string — cast with int()/float() before doing maths.
• print(sep=..., end=...) controls spacing and line-ending of output.
• Type conversion: explicit = int()/float()/str() forced by you; implicit = automatic & safe only.
• Errors: Syntax (won't run) · Logical (wrong output) · Runtime (crashes mid-run).

■ Frequently Tested Ideas (High-Yield)


• Difference between compiler and interpreter, and why Python is interpreted.
• Writing valid vs invalid identifiers.
• Predicting the type() and output of mixed-type expressions.
• Evaluating expressions using the operator precedence table.
• Explaining why input() output needs explicit type conversion.
• Classifying a given error as syntax, logical, or runtime.
• Distinguishing mutable from immutable data types with examples.

Page 9

You might also like