0% found this document useful (0 votes)
5 views

Revision Notes

This document provides a revision of Python basics, including: 1. An overview of Python's history and key qualities like interactive and script modes, indentation, comments, variables, data types, literals, and operators. 2. Explanations of core concepts like tokens, identifiers, reserved words, expressions, and data types. 3. Details about conditional statements in Python like if, if-else, if-elif-else, and nested if statements. 4. A brief introduction to iteration constructs in Python including for, while, and nested loops.

Uploaded by

dummycat2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Revision Notes

This document provides a revision of Python basics, including: 1. An overview of Python's history and key qualities like interactive and script modes, indentation, comments, variables, data types, literals, and operators. 2. Explanations of core concepts like tokens, identifiers, reserved words, expressions, and data types. 3. Details about conditional statements in Python like if, if-else, if-elif-else, and nested if statements. 4. A brief introduction to iteration constructs in Python including for, while, and nested loops.

Uploaded by

dummycat2022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit-I : Programming And Computational Thinking

CHAPTER-1
REVISION OF THE BASICS OF PYTHON

TOPIC -1
Python Basics

Revision Notes
• Python was created by Guido van Rossum in late 1980 at National Research Institute in the Netherland. Python
got its name from a BBC comedy series – “Monty Python’s Flying Circus”. Some qualities of Python based on the
programming fundamentals are given below:
• Interactive Mode: Interactive mode, as the name suggests, allows us to interact with OS.
• Script Mode: In script mode, we type Python program in a file and then use interpreter to execute the content of the
file.
• Indentation: Blocks of code are denoted by line indentation, which is rigidly enforced.
• Comments: A hash sign (#) that is not inside a string literal begins a single line comment. We can use triple quoted
string for giving multiple-line comments.
• Variables: A variable in Python is defined through assignment. There is no concept of declaring a variable outside
of that assignment. Value of variable can be manipulated during program run.
• Dynamic Typing: In Python, while the value that a variable points to has a type, the variable itself has no strict type
in its definition.
• Static Typing : In Static typing, a data type is attached with a variable when it is defined first and it is fixed.
• Multiple Assignment: Python allows you to assign a single value to several variables simultaneously.
For example: a = b = c = 1
a, b, c = 1, 2, “john”
• Token : The smallest individual unit in a program is known as a Token or a lexical unit.
• Identifiers : An identifier is a name used to identify a variable, function, class, module, or other object. An identifier
starts with a letter A to Z or a to z or an underscore ( _ ) followed by zero or more letters, underscores, and digits (0
to 9).
• Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive
programming language. Thus, Value and value are two different identifiers in Python.
Here are following identifiers naming convention for Python:
 Class names start with an uppercase letter and all other identifiers with a lowercase letter.
 Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be
private.
 Starting an identifier with two leading underscores indicates a strongly private identifier.
 If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
• Reserved Words(Keywords) : The following list shows the reserved words in Python
Python Keyword List
and del from not while

as elif global or with

assert else if pass yield

break except import print

class except in raise

continue finally is return

def for lambda try


2 ] Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII

These reserved words may not be used as constant or variable or any other identifier names. All the Python key-
words contain lowercase letters only.
• Literal/ Values : Literals (Often referred to as constant value) are data items that have a fixed value. Python allows
several kind of literals. String literals, Numeric literals, Boolean literals, special literal None, literal Collections
• Data Types : Data type is a set of values and the allowable operations on those values. Python has a great set of use-
ful data types. Python’s data types are built in the core of the language. They are easy to use and straight forward.

DATA TYPES

NUMBERS SEQUENCES SETS MAPS

int long float complex string list tuple dictionary

• Numbers can be either integers or floating point numbers.


• A sequence is an ordered collection of items, indexed by integers starting from 0. Sequences can be grouped
into strings, tuples and lists.
 Strings are lines of text that can contain any character. They can be declared with single or double quotes.
 Lists are used to group other data. They are similar to arrays.
 A tuple consists of a number of values separated by commas.
• A set is an unordered collection with no duplicate items.
• A dictionary is an unordered set of key value pairs where the keys are unique.
• Operator : Operators are special symbols which perform some computation. Operators and operands form an
expression. Python operators can be classified as given below :
Python operator types

Operators

Arithmetic Relational Logical Assignment Bitwise Membership


operators operators operators operators operators operators

+ < or = & in
– > and += | not in
* <= not –= ^
/ >= *= -
% !=or<> /= <<
** == %= >>
// //=
**=

• Expressions : An expression in Python is any valid combination of operators, literals and variables.

TOPIC-2
Conditional Statements

Revision Notes
• A conditional is a statement which is executed, on the basis of result of a condition.
• If conditional in Python have the following forms.
(a) Plain if
Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII [ 3
Syntax:
if <conditional expression>:
test Statement
False
experation
[statements]
?

True
Body of if

(b) The if-else conditional

Syntax:
if <conditional expression>:
test False Statement
experation Body of else
? [statements]
else:
True statement
Body of if [statements]

(c) The if-elif conditional statement


if <conditional expression>:
Statement
.
.
.
[statements]
elif <conditional expressions>:
statement
.
.
.
[statements]
AND
if <conditional expression>:
Statement
.
.
.
[statements]
elif <conditional expressions>:
statement
.
.
.
[statements]
else:
statement
.
.
.
[statements]
(d) Nested if
 A nested if is an if that has another if in its if ’s body or in elif ’s body or in its else’s body.
 Storing conditions – Complex and repetitive conditions can be named and then used in if statements.
4 ] Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII

TOPIC-3
Iteration Constructs

Revision Notes
• The iteration statements or repetition statements allow a set of instructions to be performed repeatedly.
• Python provides three types of loops
(a) Counting loops repeat a certain number of times e.g. for
(b) Conditional loops repeat until a certain thing happens e.g. while.
(c) Nested loops.
• Loop Constructs in Python
We will learn about different types of Python Looping statements. In Python, we have different types of loops like
For Loop, While Loop, Break & Continue Loop Control Statements, and Nested For Loop in Python with their sub-
types, syntax, and examples.

while for
loops loops

Phython

Loops Nested
while
Control
loops Statements

• Types of loop constructs in Python


• Python for Loop
• Python while Loop
• Python loop Control Statements
• Nested for Loop in Python
1. Python While Loop
A while loop in Python iterates till its condition becomes False. In other words, it executes the statements under itself
while the condition it takes is true.
While loop Syntax
while <logical Expression>:

loop body

False
Condition

True

Block of Code

When the program control reaches the while loop, the condition is checked. If the condition is true, the block of
code under it is executed.
Remember to indent all statements under the loop equally. After that, the condition is checked again. This continues
until the condition becomes false. Then the first statement, if any after the loop is executed.
e.g.
>>> a=3
>>> while(a>0):
Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII [ 5
print(a)
a-=1
Output
3
2
1
a. An Infinite Loop
Be careful while using a while loop. Because if you forget to increment the counter variable in Python, or write
flawed logic, the condition may never become false. In such a case, the loop will run infinitely, and the conditions
after the loop will starve. To stop execution, press Ctrl+C. However, an infinite loop may actually be useful.
b. The else statement for while loop
A while loop may have an else statement after it. When the condition becomes false, the block under the else
statement is executed. However, it doesn’t execute if you break out of the loop or if an exception is raised.
e.g.
>>> a=3
>>> while(a>0):
print(a)
a-=1
else:
print("Reached 0")
Output
3
2
1
Reached 0
In the following code, we put a break statement in the body of the while loop for a==1. So, when that happens,
the statement in the else block is not executed.
e.g.
>>> a=3
>>> while(a>0):
print(a)
a-=1
if(a==1):
break;
else:
print("Reached 0")
Output
3
2
c. Single Statement while
Like an if statement, if we have only one statement in while loop’s body, we can write it all in one line.
e.g.
>>> a=3
>>> while a>0: print(a); a-=1;
Output
3
2
1
You can see that there were two statements in while loop’s body, but we used semicolons to separate them.
Without the second statement, it would form an infinite loop.
2. Python for Loop
Python for loop can iterate over a sequence of items. The structure of a for loop in Python is different than that in
C++ or Java. That is, for(int i=0;i<n;i++) won’t work here. In Python, we use the ‘in’ keyword.
6 ] Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII

for loop

Item for
sequence

Block of Code

>>> a = 1
>>> for a in range(3):
print(a)
Output
1
2
If we wanted to print 1 to 3, we could write the following code.
>>> for a in range(3):
print(a+1)
Output
1
2
3
a. The range() function
This function yields a sequence of numbers. When called with one argument, say n, it creates a sequence of numbers
from 0 to n-1.
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
We use the list function to convert the range object into a list object. Calling it with two arguments creates a se-
quence of numbers from the first to the second.
>>> list(range(2,7))
[2, 3, 4, 5, 6]
You can also pass three arguments. The third argument is the interval.
>>> list(range(2,12,2))
[2, 4, 6, 8, 10]
Remember, the interval can also be negative.
>>> list(range(12,2,-2))
[12, 10, 8, 6, 4]
• Jump Statements Python offers two jump statements break and continue-to be used within loops to jump out of
loop iterations.
• break statement A break statement terminates the very loop it lies within. It skips the rest of the loop and jump
over to the statement following the loop.
• continue statement Unlike break statement, the continue statement forces the next iteration of the loop to take
place, skipping any code in between.
• Nested Loops A loop may contain another loop in its body. This form of a loop, is called nested loop. But in a
nested loop, the inner loop must terminate before the outer loop.
e.g.
for i in range(1,6):
for j in range (1,i)
print "*",
print
Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII [ 7

TOPIC-4
Idea of debugging

Revision Notes
• An error or a bug is anything in the code that prevents a program from compiling and running correctly.
• These are three types of errors
Compile Time errors occur at compile time.
These are of two types :
(i) Syntax errors occur when rules of a programming Language are misused.
(ii) Semantics errors occur when statements are not meaningful.
Run Time errors occur during the execution of a program.
Logical Errors occur due to programmer ’s mistaken analysis of the error.
To remove logical errors is called debugging.
This can be done by testing the software time again.
(a) TDD – Test Driven Development
(b) using print statements
(c) By using a debugger such as Python debugger(pdb).

TOPIC-5
Lists, Tuples and Dictionary
Revision Notes
List
• A list is a standard data type of Python that can store a sequence of values belonging to any type.
• The lists are depicted through square brackets.
• These are mutable (i.e. modifiable), you can change elements of a list in place.
• Lists store a reference at each index.
• We can index, slice and access individual list elements.
• len (L) returns the number of items in the list L membership operators in and not in can be used with list.
• To join two lists use `+’ (concatenation) operator.
• L [start: stop] create a list slice with starting index as start till stop as stopping index but excluding stop.
• List manipulation functions are
append(), insert(), extend(),sort(), remove(), reverse() and pop().
Tuples
 Tuples are immutable Python sequences, i.e. you cannot change elements of a tuple in place.
 Tuples items are indexed.
 Tuples store a reference at each index.
 Tuples can be indexed sliced and its individual items can be indexed.
 len (T) returns count of tuple elements.
 Tuple manipulation functions are: len(), max(), min(), cmp() and tuple().
Dictionaries
 Dictionaries in Python are a collection of some key-value pairs.
 These are mutable and unordered collection with elements in the form of a key : value pairs that associate keys
to values.
 The key of dictionaries are immutable type and unique.
 To manipulate dictionaries functions are : len(). clear(), has_key(),items(), keys(), values(), update(), and cmp().
 The membership operators in and not in work with dictionary keys only.
8 ] Oswaal CBSE Chapterwise & Topicwise Revision Notes, COMPUTER SCIENCE, Class – XII
(i) rithm scans A from A[1] to A[N] inserting each element A[z] into its proper position in the previously sorted

You might also like