Python Revision Tour I : Basics of Python – Notes

Class 12 – Computer Science : Python Revision Tour – I

[A] – Basics of Python


Topics are :

  1. A : Basics of Pythons
  2. B : Decision Making Statements
  3. C : Loop and Jump Statements

INTRODUCTION

A python is an object-oriented, interpreted, high-level language and a very powerful programming language.

Developed by Guido Van Rossum  in 1991.

INSTALLATION

Download it from www.python.org  and install your computer by clicking on the installation file.

WORKING IN PYTHON 

In two modes  – (i) Interactive Mode and  (ii) Script Mode.

TOKENS IN PYTHON

The smallest individual unit in a program is called a token.

TYPES OF TOKEN

(i) Keyword  (ii) Identifiers   (iii) Literals  (iv) Operators  & (v) Punctuators

Keyword

The word has a special meaning in a programming language.  If, else, while, for, or etc 

How to find the list of keywords in Python?

python_keyword_list
Keywords in Python

Identifiers

Names that are given to different parts of programs. Variable, Function, Class, etc.

Literals

Data items that have a fixed value.  Several types of literal in Python.

(a) String  (b) Numeric (c) Boolean (d) None

Operators

Triggers some computation or action when applied to the variables. +, -, / .

Punctuators

Symbols that are used  to organize sentence, structures and expressions. ‘  “ #  \ ( ) { } @ , : ; ` =, etc.

STRUCTURE  OF PYTHON PROGRAM

# This is a single line  comment

# Function

def thanks( name ):

  print(“Thanks Mr/Ms. “, name , “for watching Anjeev Singh academy”)

#Main Program

a  =  20  # variable declaration and intialization

b  =  30 

c  =  a  +    b  # expression

print (“Sum of “, a”, “and”, b, “is”, c ) 

if c > 40 :  # start of if block , showing by :

  print(“You are eligible for lucky draw”)

else:

  print(“Sorry, Better Luck next time….”)

VARIABLES AND ASSIGNMENTS

A Variable is a named memory location, used for storing and manipulating values.

Example : Name = “Rashmi” , Age   = 18

Variable Assignment

DYNAMIC TYPING

A variable pointing to a value of a certain type can be made to point to a value/object of a different type. This is called Dynamic Typing. For example:

N = “Rashmi”

N   = 18

Dynamic Typing

MULTIPLE ASSIGNMENTS

Assigning same value to multiple variables. E.g.  a = b = c = 10

Assigning  multiple values to multiple variables. E.g.  a , b , c  = 10, 20, 30

INPUT

In python 3.x , input( ) is a built-in function, which allows user to input a value.

in_varibale  = input( <prompt message> )

Note : input( ) function always return  a value of String type.

int ( ) :- To convert a value in integer. => x = int (input(“A number plz”)).

float() :- To convert a value in float. => f = float (input(“a number plz:”)).

bool( ) :- To convert a value in Boolean. => b = bool (input(“Boolean value plz:”)) .

PRINT / OUTPUT

In python 3.x, print( ) is a built-in function, which allows the user to send output to a standard output device, i.e. monitor.

print( object , [ sep = ‘ ‘  or <separator string>, end = ‘\n’ or <end-string>] )

print (“Hello”)

print (“Your marks is”, 20)

print (“Welcome”, “You”, “All”, sep = ‘===‘)

print( “Mobile”, end=‘ ‘)

print (“9898989898”)

DATA TYPES

Data types are means to identify the type of data and the set of valid operations for it.

There are the following built-in core data types:-

Data Types in Python | www.mycstutorial.in

Numbers Data Types

Integers

Integers – Two types of integers in Python

  • a) Integer (signed) – number having only integer, either positive or negative
  • b) Booleans – represent truth values False and True. Boolean values False and True behave like values 0 and 1, respectively.

Floating Point Numbers

Is used to store numbers with a fraction part. They can be represented in scientific notation. Also called real numbers.

Example :  x = 25.036

     y = 4.95e3  => 495.00  => 4.95 x 10^3

Complex Numbers

Python allows to store the complex number.  Complex numbers are pairs of real and imaginary numbers. They can store in the form of ‘a+bj .  Both real and imag are internally represented as float value.

Example :   t = 2 + 3j

    t.real => gives the real part

    t.imag => gives the imaginary part as a float.

Sequence Data Types

Sequence Data Types is an ordered collection of items, indexed by integers (both +ve or –ve). Three types of sequence are – 

Strings

The string is a combination of letters, numbers, and special symbols.  In Python 3.x string is a sequence of Pure Unicode characters. Unicode is a system, designed to represent every character from every language.

Example : – “abc”, “12536”, “&*)()^%$”, “abcd12365&^%#’

A string is a sequence of characters and each character can be individually accessed using its index position i.e. forward (0,1, 2, …) and backward (-1, -2, -3, .. …)

Lists

The list is a set of comma-separated values of any datatype inside the square brackets. [  ]

Example =>  [1, 2 , 3 , 4, 5]  

    [1.5, 2.5, 4.5, 9.8] 

    [‘x’,  ‘y’, ‘z’]

    [‘anjeev’, 25,  4589.36, True]

The list can also be accessed through the index position i.e. forward and backward.

Tuples

A Tuple is a set of comma-separated values of any data type inside the parentheses (  )

Example =>  (1, 2 , 3 , 4, 5)  

    (1.5, 2.5, 4.5, 9.8) 

    (‘x’,  ‘y’, ‘z’)

    (‘anjeev’, 25,  4589.36, True )

Tuple can also be accessed through the index position i.e. forward and backward.

Dictionary

A dictionary is a comma-separated key-value pairs within { } and accessed using keys.

No two keys can be the same .

Example :   mon ={‘j’: ‘Jan’, ‘f’: ‘Feb’, ‘m’: ‘Mar’}

      mon[‘j’]    => Jan

      values = { ‘a’:  1, ‘e’: 2, ‘i’: 3, ‘o’: 4,  ‘u’ : 5 }

Mutable and Immutable Data Types

Immutable Types

Immutable types are those that can never change their value in place.

The following are immutable:  integers, floating point numbers, Boolean, strings, tuples

In immutable types, the variable names are stored as a reference to a value object, each time when we change the value, the variable’s reference memory address changes.

Note : use id( ) method to get the memory address/reference.

Mutable Types

The Mutable types are those whose values can be changed in place. The following are mutable :  lists, dictionaries, and sets

Operators and Their Types

Arithmetic Operator

OperatorDescription
+ (unary)Explicitly express a +ve number. Example : +3, +98.36
– (unary)Explicitly express a –ve number. Example : -3, -369.36
+ (binary)Add two numbers, Example: 9 + 6 is 15
– (binary)Subtract one value from another number. Example: 9 – 3 is 6
*Product of two numbers. Example 9 * 6 is 54
/Gives a quotient, when one value is divided by the other. Example 9/ 6 is 1.5
// (floor division)Gives the integer part of the quotient, when one value is divided by the other. Example 9//6 is 1.
% (Remainder)Gives remainder when one value is divided by the other. Example 9 % 6 is 3
** (Exponent)To raise a number to the power of another number. For example, 3**2 is 9
Arithmetic Operators | www.mycstutorial.in

Assignment Operator

OperatorDescription
=Called Assignment operator. Assign a value to the variable. Example, x = 20.
+=Add and Assign, Add the R-Value to the L-Value and assign result to L-value. Example x=20,  x += 30  it means x = x + 30  so the new value of x is 50.
-=Subtract and Assign, Subtract the R-Value from L-Value and assign result to L-value. Example x=50,  x -= 30  it means x = x – 30  so the new value of x is 20.
*=Multiply and Assign,  Multiply the R-Value with L-Value and assign product to L-value. Example x=50,  x *= 3 it means x = x * 3  so the new value of x is 20.
/=Divide and Assign Quotient, Divide the L-value with R-Value and assign quotient to L-value. Example, x = 50, x /= 6  its means x = x / 6, and the new value of x is 8.33333……
//=Floor and assign, Divide the L-value with R-Value and assign integer quotient (floor division) to L-value. Example,  x = 50, x //= 6  its means x = x // 6, and the new value of x is 8.
%=Divide and Assign Remainder, Divide the L-value with R-Value and assign remainder to L-value. Example,  x = 50, x %= 6  its means x = x % 6, and the new value of x is 2.
**=Exponent and Assign, Calculate (L-Value)R-Value and assign the result to L-value. Example, x = 3, x**=2  its means x = x2 and the result of x is 9
Assignment Operator | mycututorial.in

Relational Operator

OperatorDescription
==  (Equality)Compares two values for equality, Returns True if they are equal, otherwise returns False.
!=  (Not equal)Compares two values for inequality, Returns True if they are not equal, otherwise returns False.
<  (Less than)Compares two values, Returns True if first value is less than the second, otherwise returns False.
> (greater than)Compares two values, Returns True if first value is greater than the second, otherwise returns False.
<= (Less than or equal to)Compares two values, Returns True if first value is less than or equal to the second, otherwise returns False.
>= (greater than or equal to)Compares two values, Returns True if first value is greater than or equal to the second, otherwise returns False.
Relational Operator | mycstutorial.in

Logical Operator

OperatorDescription
notNegate a condition and  Returns True if the condition is false otherwise returns False.
andCombines two conditions and returns True if both the conditions are true, otherwise returns False.
orCombines two conditions and returns True if at least one of the condition is true, otherwise returns False.
Logical Operator | mycstutorial.in

Bitwise Operator

OperatorDescription
& (bitwise and)The AND (&) operator compares two bits and generates a result of 1 if both bits are 1 otherwise, it returns 0
| (bitwise or)The OR (|) operator compares tow bits and generate a result 1 if the bits are complementary, otherwise it returns 0.
^ (bitwise xor)The EXCLUSIVE-OR (XOR) operator compares two bits and return 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1
~ (bitwise complement)The COMPLEMENT operator is used to invert all of the bits of the operand.
Bitwise Operator | mycstutorial.in

Note: bin( ) returns binary representation number.

Membership Operator

OperatorDescription
inReturns True if the given value is available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False.
not inReturns True if the given value is not available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False.
Membership Operators | www.mycstutorial.in

Identity Operator

OperatorDescription
isReturns True, if both operands have the same identity i.e. memory reference, otherwise return False
Is notReturns True, if both operands have not the same identity i.e. memory reference, otherwise return False
Identity Operators | www.mycstutorial.in

Operator Precedence  :

Execution of the operator depends on the precedence of the operator. 

OrderOperatorDescription
Highest()Parentheses
**Exponentiation
|~Bitwise nor
|+, –Unary plus and Minus
|*, /, //, %Multiplication, Division, floor division, remainder
|+, –Addition, Subtraction
|&Bitwise and
|^Bitwise XOR
||Bitwise OR
|<, <=, >, >=, <>, !=, ==, is, is notRelational Operators, Identity Operators
|notBoolean NOT
andBoolean AND
LowestorBoolean OR
Operator Precedence | www.mycstutorial.in

Expression

In Python, An expression is a valid combination of operators, literals, and variables.

There are three types of  Expression in Python, These are-

  • Arithmetic Expressions:-  combination of integers, floating-point numbers, complex numbers, and arithmetic operators.  Example; 2+9, 9.2/3
  • Relational Expression :-  combination of literals and/or variables of any valid type and relational operators . Example; x > y, 5 < 8, z == y
  • Logical Expression:- combination of literals, variables and logical operators. Example;  t and q,   p or g
  • String Expression :- combination of string literals and string operators ( + -> concatenate, and  * -> replicate). Example; 
    • Examples:
      “Hello” + “Anjeev” = “Hello Anjeev”
      “Like” * 3 =  “LikeLikeLike”

Types Casting / Explicit Type Conversion

The conversion of one type to a specific type explicitly, is called Type Casting / Explicit Type Conversion.

Syntax:  <type>(  )

Example  x = int ( “123”)

    y = float(“5369.36”)

    z = str(598698.36)

Comments in Python

Comments are a non-executable statement in Python, used for the documentation of the program i.e. specify the purpose of the program, the purpose of expression, variables, statements, blocks, etc.

There are two types of Comments in Python –

1. Full Line or Inline Comment

  # symbol is used to write a single or inline comment in Python ->full line comment

  r = 20  # rate of interest -> inline comment

2. Multiline Comments or Block Comments

  ‘’’   ‘’’    or  “””  “””  – triple quotes (single or double) are used for multiline comments in python..


Click here for –

Python Revision Tour – I (B) : Decision-Making Statements

Python Revision Tour – I (C): Looping & Jump Statements


Class 12 Computer Science




By Anjeev Kr Singh – Computer Science Educator
Published on : May 5, 2021 | Updated on : December 24, 2025

About the Author

Anjeev Kr Singh

Anjeev Kr Singh

Computer Science Educator, Author, and HOD. Guiding CBSE students in CS, IP, IT, WA & AI via mycstutorial.in. Creator of Question Bank for Class 10 & 12 students.

You cannot copy content of this page

Scroll to Top