Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 12  >  Chapter Notes: Python Revision

Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts PDF Download

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 Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts
    These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords 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 useful data types. Python’s data types are built in the core of the language. They are easy to use and straight forward.
    Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

  • 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 Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

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

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
    Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/ArtsPython Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts
    (b) The if-else conditional
    Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts
    (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.

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 subtypes, syntax, and examples.
    Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts
  • Types of loop constructs in Python
    • Python for Loop
    • Python while Loop
    • Python loop Control Statements
    • Nested for Loop in Python

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.
Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts
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):
print(a)
a-=1
Output
3
2
1

  • 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.
  • 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
  • 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.

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.
Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

>>> 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

  • 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 sequence 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

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).

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.

Sorting Algorithm

Revision Notes

  • Sorting means arranging the elements in a specified order i.e. either ascending or descending order.
  • Two sorting techniques are
    • Bubble Sort – It compares two adjoining values and exchange them if they are not in proper order.
    • Insertion Sort – suppose an array A with n elements A[1], A[2],…..A[N] is in memory. The insertion sort algorithm scans A from A[1] to A[N] inserting each element A[z] into its proper position in the previously sorted subarray A[1], A[2]…….A[x-1]

Strings in Python

Revision Notes

  • Strings in Python are stored as individual characters in contiguous location, with two way index for each location. 
  • Strings are immutable  and hence item assignment is not supported.
  • Following operations can be used on strings.
    • Concatenation `+’
    • Replication `*’
    • Membership in and not in
    • Comparison Operators ==, ! =, <, >, < =, > =
  • Built in functions
    ord() – returns ASCII value of passed character.
    chr() – returns character corresponding to passed ASCII code
  • String slice refers to a part of the string, where strings are sliced using a range of indices
    Syntax : string [n:m] slice the string.

The document Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts is a part of the Humanities/Arts Course Computer Science for Class 12.
All you need of Humanities/Arts at this link: Humanities/Arts
1 videos|26 docs|18 tests

Top Courses for Humanities/Arts

1 videos|26 docs|18 tests
Download as PDF
Explore Courses for Humanities/Arts exam

Top Courses for Humanities/Arts

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

ppt

,

Viva Questions

,

shortcuts and tricks

,

Extra Questions

,

mock tests for examination

,

practice quizzes

,

past year papers

,

Free

,

Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

,

Exam

,

Previous Year Questions with Solutions

,

study material

,

MCQs

,

Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

,

pdf

,

Summary

,

Objective type Questions

,

Python Revision Chapter Notes | Computer Science for Class 12 - Humanities/Arts

,

Important questions

,

video lectures

,

Semester Notes

;