Python Revision Tour 1 Class 12 Notes

Teachers and Examiners (CBSESkillEduction) collaborated to create the Python Revision Tour 1 Class 12 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 12.

Python Revision Tour 1 Class 12 Notes

Token in Python

The smallest unit in a Python programme is called a token. In python all the instructions and statements in a program are built with tokens. Different type of tokens in python are keywords, identifier, Literals/values, operators and punctuators –

Keywords

Words with a particular importance or meaning in a programming language are known as keywords. They are utilised for their unique qualities. There are 33 keywords in Python True, False, class, break, continue, and, as, try, while, for, or, not, if, elif, print, etc.

Identifiers

The names assigned to any variable, function, class, list, method, etc. for their identification are known as identifiers. Python has certain guidelines for naming identifiers and is a case-sensitive language. To name an identifier, follow these guidelines: –

  1. Code in python is case – sensitive
  2. Always identifier starts with capital letter(A – Z), small letter (a – z) or an underscore( _ ).
  3. Digits are not allowed to define in first character but you can use between.
  4. No whitespace or special characters are allowed.
  5. No keyword is allowed to define identifier.

Python Revision Tour 1 Class 12 Notes

Literals or Values

In Python, literals are the raw data that is assigned to variables or constants during programming. There are five different types of literals string literals, numeric literals, Boolean literals and special literals none.

a) String literals

The string literals in Python are represented by text enclosed in single, double, or triple quotations. Examples include “Computer Science,” ‘Computer Science’, ”’Computer Science”’ etc.
Note – Triple quotes string is used to write multiple line.
Example –
n1 = ‘Computer Science’
n2 = “Computer Science”
n3 = ”’Computer Science”’
print(n1)
print(n2)
print(n3)

b) Numeric Literals

Literals that have been use to storing numbers is known is Numeric Literals. There are basicaly three numerical literals –

  1. Integer Literal
  2. Float Literal
  3. Complex Literal

Example
num1 = 10
num2 = 15.5
num3 = -20
print(num1)
print(num2)
print(num3)

c) Boolean Literal

Boolean literals have only two values True of False.
Example
num = (4 == 4)
print(num)

d) Special literals none

The special literal “None” in Python used to signify no values, the absence of values, or nothingness.
Example
str = None
print(str)

Python Revision Tour 1 Class 12 Notes

Operators

In Python, operators are specialized symbols that perform arithmetic or logical operations. The operation in the variable are applied using operands.

The operators can be arithmetic operators(+, -, * /, %, **, //), bitwise operators (&, ^, |), shift operators (<<, >>), identity operators(is, is not), relational operators(>, <, >=, <=, ==, !=), logical operators (and, or), assignment operator ( = ), membership operators (in, not in), arithmetic-assignment operators (/=, +=, -=, %=, **=, //=).

Punctuators

The structures, statements, and expressions in Python are organized using these symbols known as punctuators. Several punctuators are in python [ ] { } ( ) @ -= += *= //= **== = , etc.

Barebones of a python program

a) Expressions – An expression is a set of operators and operands that together produce a unique value when interpreted.

b) Statements – Which are programming instructions or pieces of code that a Python interpreter can carry out.

c) Comments – Python comments start with the hash symbol # and continue to the end of the line. Comments can be single line comments and multi-line comments.

d) Functions – A function is a block of code that only executes when called. You can supply parameters—data—to a function. As a result, a function may return data.

e) Block or suit – In Python, a set of discrete statements that together form a single code block are referred to as suites. All statements inside a block or suite are indented at the same level.

Variables and Assignments

A symbolic name that serves as a reference or pointer to an object is called a variable in Python. You can use the variable name to refer to an object once it has been assigned to it. However, the object itself still holds the data.
Example
name = “Python”

Python Revision Tour 1 Class 12 Notes

Dynamic Type vs Static Type

The term “dynamic typing” refers to a variable’s type only being established during runtime. Whereas the statically type done at compile time in a python. Statically type of a variable cannot be change.

Multiple Assignments

Values are assigned by Python from right to left. You can assign numerous variables simultaneously in a single line of code using multiple assignment.
a) Assigning same value to multiple variables
n3 = n2 = n1 = 20
b) Assigning multiple values to multiple variables
n1, n2, n3 = 10, 20, 30

Simple input and output

Input 

Python automatically treats every input as a string input. To convert it to any other data type, we must explicitly convert the input. For this, we must utilize the int() and float() methods to convert the input to an int or a float, respectively.
example
name = input(‘What is your name’)
age = int(input(‘What is your age’)

Output

The print() method in Python allows users to display output on the screen. print statement automatically converts the items to strings.
example
num = 10
print(“Number is “)
print(num) # auto converted to string
print(34) # auto converted to string

Python Revision Tour 1 Class 12 Notes

Data Type

The classification or categorizing of data elements is known as data types. It represents the type of value that specifies the operations that can be carried out on a given set of data. In Python programming, everything is an object, hence variables and data types are both instances or objects.

Python has following data types by default.

Data types for Numbers

a) Integer – The integers represent by “int”. It contains positive or negative values.
b) Boolean – The boolean type store on True or False, behave like the value 0 and 1.
c) Floating – It is a real number with specified with decimal point.
d) Complex – Complex class is a representation of a complex number (real part) + (imaginary part)j. For example – 2+3j.

Data types for String

Strings in Python are collections of Unicode characters. A single, double, or triple quote are used to store string. There is no character data type in Python.
Example
name = ‘Computer Science’
name1 = “Computer Science’
name2 = ”’Computer Science”’

Lists

Lists are similar to arrays, it is a collections of data in an ordered way. list’s items don’t have to be of the same type in python.
Example
my_list = [1, 2, 3]
my_list1 = [“Computer”, “Science”, “Python”]
my_list2 = [“Computer Science”, 99]

Python Revision Tour 1 Class 12 Notes

Tuples

Tuple is similar to lists, It is also an ordered collection of python. The only difference between tuple and list is that tuples can’t be modified after it is created, So, that’s why it is known as immutable.
Example
my_tuple = (1, 2, 3)
my_tuple1 = (“Computer”, “Science”, “Python”)
my_tuple2 = (“Computer Science”, 99)

Dictionaries

In Python, a dictionary is an unordered collection of data values that can be used to store data values like a map. This dictionary can hold only single value as an element, Dictionary holds key:value pair.
Example
my_dict = (1: ‘Computer’, 2: ‘Mathematics’, 3: ‘Biology’)
my_dict = (‘Computer’: 1, ‘Mathematics’: 2, ‘Biology’: 3)

Mutable and Immutable Types

In Python, there are two different types of objects: mutable and immutable. It is possible to change mutable data types after they have been formed, whereas immutable objects cannot be altered once they have been created.

Mutable types

The mutable types are those whose values can be changed in place. Only three types are mutable in python lists, dictionaries and sets.

Immutable types

The immutable types are those that can never change their value in place. In Python, the following types are immutable in python integers, floating point numbers, Booleans, strings, tuples.

Python Revision Tour 1 Class 12 Notes

Expressions

In Python, an expression is made up of both operators and operands. example of expressions in python are num = num + 3 0, num = num + 40.
Operators in Python are specialised symbols that indicate that a particular type of computation should be carried out. Operands are the values that an operator manipulates. An expression is a collection of operators and operands, such as num = num + 3 0.

Type Casting (Explicit Type Conversion)

Type casting, often known as explicit type conversion, Users change the data type of an object to the required data type via explicit type conversion. To achieve explicit type conversion, we use predefined functions like int(), float(), str(), etc.

Math Library Functions

The Python standard library includes a built-in module called math that offers common mathematical functions.

  • math.ceil() – The ceil() function return the smallest integer.
  • math.sqrt() – The sqrt() function returns the square root of the value.
  • math.exp() – The exp() function returns the natural logarithm reised to the power.
  • math.fabs() – The fabs() function returns the absolute value.
  • math.floor() – The floor() function returns round a number down to the nearest intger.
  • math.log() – The log() function is used to calculate the natural logarithmic value.
  • math.pow() – The pow() function returns base raised to exp power.
  • math.sin() – The sin() function returns sine of a number.
  • math.cos() – The cos() function return the cosine of the value.
  • math.tan() – The tan() function return the tangent of the value.
  • math.degrees() – The degrees() converts angle of value from degrees to radians.

Python Revision Tour 1 Class 12 Notes

Statement Flow Control

Control flow refers to the sequence in which a program’s code is executed. Conditions, loops, and function calls all play a role in how a Python programme is controlled.
Python has three types of control structures –
a) Sequential – By default mode
b) Selection – Used in decision making like if, swithc etc.
c) Repetition – It is used in looping or repeating a code multiple times

Compound Statement

Compound statements include other statements (or sets of statements), and they modify or direct how those other statements are executed. Compound statements often take up numerous lines, however in some cases, a complete compound statement can fit on a single line.
Example
<compound statement header> :
<indented body containing multiple simple and/ or compound statement>

The IF & IF-ELSE conditionals

When using the if statement in Python, a Boolean expression is evaluated to determine whether it is true or false. If the condition is true, the statement inside the if block is executed. If the condition is false, the statement inside the else block is only executed if you have written the else block; otherwise, nothing happens.
Syntax of IF condition
if <conditional expression> :
     statement
     [statement]

Syntax of IF-ELSE condition
if <conditional expression> :
     statement
     [statement]
else :
     statement
     [statement]

Python Revision Tour 1 Class 12 Notes

Nested IF statement

One IF function contains one test, with TRUE or FALSE as the two possible results. You can test numerous criteria and expand the number of outcomes by using nested IF functions, which are one IF function inside of another.
Syntax of Nested IF statement
if condition :
     if condition :
          Statement
     else :
          Statement

Looping Statement

In Python, looping statements are used to run a block of statements or code continuously for as many times as the user specifies. Python offers us two different forms of loops for loop and while loop.

The For loop

Python’s for loop is made to go over each element of any sequence, such a list or a string, one by one.
Syntax of FOR loop –
for <variable> in <sequence> :
     statements_to_repeat

The range() based for loop

The range() function allows us to cycle across a set of code a predetermined number of times. The range() function returns a series of numbers and, by default, starts at 0 and increments by 1 before stopping at a predetermined value.
Syntax –
range(stop)
range(start, stop)
range(start, stop, step)

Python Revision Tour 1 Class 12 Notes

The While loop

A while loop is a conditional loop that will repeat the instructions within itself as long as a conditional remain true.
Syntax –
while <logical expression> :
loop-body

JUMP Statemement – break and condinue

Python offers two jump statement – break and continue – to be used within loops to jump out of loop-iterations.

The break Statement

A break statement terminates the very loop it lies within. Execution resumes at the statement immediately following the body of the terminated statement.
Syntax –
break

Example –
a = b = c = 0
for i in range(1, 11) :
     a = int(input(“Enter number 1 :”))
     b = int(input(“Enter number 2 :))
     if b == 0 :
          print(“Division by zero error! Aborting”)
          break
     else
          c=a/b
          print(“Quotient = “, c)
print(“program over!”)

The continue statement

Unlike break statement, the continue statement forces the next iteration of the loop to take place, skipping any code in between.
Syntax –
continue

Python Revision Tour 1 Class 12 Notes

Loop else statement

Python supports combining the else keyword with both the for and while loops. After the body of the loop, the else block is displayed. After each iteration, the statements in the else block will be carried out. Only when the else block has been run does the programme break from the loop.
Syntax –
for <variable> in <sequence> :
     statement1
     statement2
else :
     statement(s)

while <test condition> :
     statement1
     statement2
else :
     statement(s)

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.
The following is an example of a nested loop :
for i in range(1,6) :
     for j in range(1, i) :
          print(“*”, end =’ ‘)
     print()

error: Content is protected !!