Python Revision Tour Class 12 Notes | CBSE Computer Science

Python Revision Tour Class 12 Notes covers Python Fundamentals of class 11 including Variables, Operators, Input and Output, Flow of control, Expressions, Type Casting, Strings, List, Tuples, Dictionary. All the concepts are explained with examples so that students can build strong foundation in Python Programming Fundamentals.

Getting Started with Python

Python is a General Purpose high level Programming language used for developing application softwares.

Features of Python

  • High Level
  • Free and Open Source
  • Case Sensitive
  • Interpreted
  • Plateform Independent
  • Rich library of functions
  • Dynamic
  • General Purpose

Working with Python

To write and run python programs we need Python Interpreter also called Python IDLE.

Execution Mode

We can use Python Interpreter in two ways:

  • Interactive mode
  • Script mode

Interactive Mode

  • Instant execution of individual statement
  • Convenient for testing single line of code
  • We cannot save statements for future use

Script Mode

  • Allows us to write and execute more than one Instruction together.
  • We can save programs (python script) for future use
  • Python scripts are saved as file with extension “.py”

How to execute or run python program

  • Open python IDLE
  • Click ‘File’ and select ‘New’ to open Script mode
  • Type source code and save it
  • Click on ‘Run’ menu and select ‘Run Module’

Python Keywords

  • These are predefined words which a specific meaning to Python Interpreter.
  • These are reserve keywords
  • Keywords in python are case sensitive

Identifier

Identifiers are name used to identify a variable, function or any other entities in a programs.

Rules for naming Identifier

  • The name should begin with an alphabet or and underscore sign and can be followed by any combination of charaters a-z, A-Z, 0-9 or underscore.
  • It can be of any length but we should keep it simple, short and meaningful.
  • it should not be a python keyword or reserved word.
  • We cannot use special symbols like !, @, #, $, % etc. in identifiers.

Variables

  • It can be referred as an object or element that occupies memory space which can contain a value.
  • Value of variable can be numeric, alphanumeric or combination of both.
  • In python assignment statement is used to create variable and assign values to it.

Data types

These are keywords which determine the type of data stored in a variable. Following table show data types used in python:

Comments

  • These are statement ignored by python interpreter during execution.
  • It is used add a remark or note in the source code.
  • It starts with # (hash sign) in python.

Operators

These are special symbols used to perform specific operation on values. Different types of operator supported in python are given below:

  • Arithmetic operator
  • Relational operator
  • Assignment operator
  • Logical operator
  • Identity operator
  • Membership operator

Expressions

  • An expression is combination of different variables, operators and constant which is always evaluated to a value.
  • A value or a standalone variable is also considered as an expression.

Example

56 + (23-13) + 89%8 – 2*3

Evaluation:

= 56 + (23 -13) + 89%8 – 2*3        #step1
= 56 + 10 + (89%8) – 2*3              #step2
= 56 + 10 + 1 – (2*3)                     #step3
= (56 + 10) + 1 – 6                        #step4
= (66 + 1) – 6                                 #step5
= 67 – 6                                          #step6
= 61

Statement

A statement is unit of code that the python interpreter can execute.

Example:

var1 = var2                                         #assignment statement
x = input (“enter a number”)      #input statement
print (“total = “, R)                           #output statement

How to input values in python?

In python we have input() function for taking user input.

Syntax:

Input([prompt])

How to display output in python?

In python we have print() function to display output.

Syntax:

print([message/value])

Example:

Python program to input and output your name

var = input(“Enter your name”)
print(“Name you have entered is “, var)

Example:

Addition of two numbers

Var1 = int(input(“enter no1”))
Var2 = int(input(“enter no2”))
Total = Var1 + Var2
Print(“Total = “, Total)

Type Conversion

Type conversion refers to converting one type of data to another type.

Type conversion can happen in two ways:

  • Explicit conversion
  • Implicit conversion

Explicit Conversion

  • Explicit conversion also refers to type casting.
  • In explicit conversion, data type conversion is forced by programmer in program

Syntax:

(new_data_type) = (expression)
Explicit type conversion function

Example:

Program of explicit type conversion from float to int

x = 12
y = 5
print(x/y)                           #output – 2.4
print(int(x/y))                    #output – 2

Program of explicit type conversion from string to int

x = input(“enter a number”)
print(x+2)                                            #output – produce error “can only concatenate str to str
x = int(input(Enter a number”))                          
print(x+2)                                            #output – will display addition of value of x and 2

Implicit conversion

  • Implicit conversion is also known as coercion.
  • In implicit conversion data type conversion is done automatically.
  • Implicit conversion allows conversion from smaller data type to wider size data type without any loss of information

Example:

Program to show implicit conversion from int to float

var1 = 10                              #var1 is integer
var2 = 3.4                             #var2 is float
res  = var1 – var2              #res becomes float automatically after subtraction
print(res)                             #output – 6.6
print(type(res))                                #output – class ‘Float’

Strings

  • String is basically a sequence which is made up of one or more UNICODE characters.
  • Character in string can be any letter, digit, whitespace or any other symbol.
  • String can be created by enclosing one or more characters in single, double or triple quotes.

Examples:

Accessing characters in a string (INDEX)

  • Individual character in a string can be accessed using indexes.
  • Indexes are unique numbers assigned to each character in a string to identify them
  • Index always begins from 0 and written in square brackets “[]”.
  • Index must be an zero, positive or negative integer.
  • We get IndexError when we give index value out of the range.

Negative Index

  • Python allows negative indexing also.
  • Negative indices are used when you want to access string in reverse order.
  • Starting from the right side, the first character has the index as -1 and the last character (leftmost) has the index –n where n is length of string.

Is string immutable?

  • Yes, string is immutable data type. The content of string once assigned cannot be altered than.
  • Trying to alter string content may lead an error.

String operations

String supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing

Concatenation

  • Concatenation refers to joining two strings.
  • Plus (‘+’) is used as concatenation operator.

Repetition

  • Repetition as it name implies repeat the given string.
  • Asterisk (‘*’) is used as repetition operator.

Membership

  • Membership operation refers to checking a string or character is part or subpart of an existing string or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the first string or character appears as substring in the second string.
  • ‘not in’ returns true if the first string or character does not appears as substring in the second string.

Slicing

  • Extracting a specific part of string or substring is called slicing
  • Subset occurred after slicing contains contiguous elements
  • Slicing is done using index range like string[start_index : end_index : step_value]
  • End index is always excluded in resultant substring.
  • Negative index can also be used for slicing.

Traversing a String

  • Traversing a string refers to accessing each character of a given string sequentially.
  • For or while loop is used for traversing a string

String Functions

List

  • List is built in sequence data type in python.
  • Stores multiple values
  • List item can be of different data types
  • All the items are comma separated and enclosed in square bracket.
  • Individual item in a list can be accessed using index which begins from 0.

Examples

Accessing elements in a List

  • Individual item in a list can be accessed using indexes.
  • Indexes are unique numbers assigned to each item in a list to identify them
  • Index always begins from 0 and written in square brackets “[]”.

Example:

List is Mutable

  • Yes list is mutable.
  • Content of the list can be changed after it has been created.

List operations

List supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing

Concatenation

  • Concatenation refers to joining two List.
  • Plus (‘+’) is used as concatenation operator.
  • Concatenating List with other data type produces TypeErrors.

Example:

Repetition

  • Repetition as it name implies used to replicate a list at specified no of times.
  • Asterisk (‘*’) is used as repetition operator.

Membership

  • Membership operation refers to checking an item is exists in the list or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the item specified present in the list.
  • ‘not in’ returns true if the item specified present in the list.

Example:

Slicing

  • Extracting a subset of items from given list is called slicing
  • Subset occurred after slicing contains contiguous items.
  • Slicing is done using index range like List[start_index : end_index : step_value]
  • End index is always excluded in resultant subset of list.
  • Negative index can also be used for slicing.

Examples:

Traversing a List

  • Traversing a List refers to accessing each item a given list sequentially.
  • for or while loop can be used for traversing a list.

Examples:

List methods | List Functions

Nested List

One list appears as an element inside another list.

Example:

Tuple

  • Tuple is built in sequence data type in python.
  • Stores multiple values
  • Tuple item can be of different data types
  • All the items are comma separated and enclosed in parenthesis ‘()’.
  • Individual item in a Tuple can be accessed using index which begins from 0.
  • In case of single item present in tuple, it should also be followed by a comma.
  • A sequence without parenthesis is treated as tuple by default.

Examples

Accessing elements in a Tuple

  • Individual item in a Tuple can be accessed using indexes.
  • Indexes are unique numbers assigned to each item in a Tuple to identify them
  • Index always begins from 0 and written in square brackets “[]”.

Example:

Tuple is Immutable

  • Yes Tuple is Immutable.
  • Content of the Tuple cannot be changed after it has been created.

Example:

Tuple operations

Tuple supports following operations:

  • Concatenation
  • Repetition
  • Membership
  • Slicing

Concatenation

  • Concatenation refers to joining two Tuple.
  • Plus (‘+’) is used as concatenation operator.
  • Concatenation operator can also be used for extending an existing tuple.

Example:

Repetition

  • Repetition as it name implies used to repeat elements of  a Tuple at specified no of times.
  • Asterisk (‘*’) is used as repetition operator.

Membership

  • Membership operation refers to checking an item exists in Tuple or not.
  • Python uses ‘in’ and ‘not in’ as membership operator.
  • ‘in’ returns true if the item specified present in the Tuple.
  • ‘not in’ returns true if the item specified present in the Tuple.

Example:

Slicing

  • Extracting a subset of items from given Tuple is called slicing
  • Subset occurred after slicing contains contiguous items.
  • Slicing is done using index range like Tuple[start_index : end_index : step_value]
  • End index is always excluded in resultant subset of Tuple.
  • Negative index can also be used for slicing.

Examples:

Traversing a Tuple

  • Traversing a Tuple refers to accessing each item a given Tuple sequentially.
  • for or while loop can be used for traversing a Tuple.

Examples:

Tuple Methods and Built in Functions

Nested Tuple

One Tuple appears as an element inside another Tuple.

Example:

Tuple Assignment

Tuple Assignment allows elements of tuple on the left side of assignment operator to be assigned respective values from a tuple on the right side.

Dictionary

  • Dictionaries are unordered collection of items falls under mapping.
  • Stores data in form of key:value pair called item.
  • Key is separated from its value by colon ‘:’ and items are separated by comma.
  • All items of dictionaries are enclosed in curly bracket ‘{}’.
  • The keys in the dictionary must be unique and should be of immutable data type.
  • The value can be repeated and of any data type.

Creating a Dictionary

Accessing items in a Dictionary

  • Items of dictionary are accessed using keys.
  • Each key of dictionary serve as index and maps to a value.
  • If key is not present in dictionary, then we get KeyError.

Examples

Dictionary is Mutable

Yes, Dictionary is Mutable as its content can be changed after it has been created.

Adding a new item in Dictionary

Modifying an existing Item in Dictionary

Traversing a dictionary

Dictionary methods and built in functions

2 thoughts on “Python Revision Tour Class 12 Notes | CBSE Computer Science”

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!