Fundamentals of Python
Informatics Practices (065)
Fundamenatals of
As per CBSE Syllabus 2023-24
8460831302 Page 1
Fundamentals of Python
• Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
How to work with python?
There are two ways to work in python.
• in Interactive mode
One command can executed at time.
8460831302 Page 2
Fundamentals of Python
in Script mode
• Set of commands can execeuted at a time
8460831302 Page 3
Fundamentals of Python
tokens
• Tokens are smallest individual unit of python.There are following types of
token in Python.
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
8460831302 Page 4
Fundamentals of Python
keywords
• Keyword have special meaning to language complier.
• List of keywords:-
Keywords in Python
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
8460831302 Page 5
Fundamentals of Python
assert del global not with
async elif if or yield
identifiers
• Identifier is user defined name.
Rules of naming identifier:-
1. A-Z,a-z,0-9,_ are allowed.
2. Space not allowed
3. It can be of any length.
8460831302 Page 6
Fundamentals of Python
4. Cannot start with digit.
5. Case-sensitive(Total,TOTAL,total)
Valid identifier Invalid Identifier
• myfile • My File
• My_file • My-file
• Ifs • if
• For • for
8460831302 Page 7
Fundamentals of Python
literal
• Literal have fixed value.
• There are following type of literal in Python.
• String literals :: "halo" , '12345'
• Int literals :: 0,1,2,-1,-2
• Float literals :: 3.14
• Boolean literals :: True or False
• Special literals :: None
• Literal collection:-
• List literals :: [5,6,7]
• Tuple literals :: (8,9,0)
• Dict literals :: {'x':1}
operators
8460831302 Page 8
Fundamentals of Python
• Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
Arithmetic operators
8460831302 Page 9
Fundamentals of Python
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
8460831302 Page 10
Fundamentals of Python
= is used as a assignmentoperators.
e.g:-a=5
5 will assign to variable a.
You can assign single value to multiple variable.
e.g:-a=b=c=10
10 will assign to variable a ,b and c.
You can use shorthand assignment operators for quick calculation.
e.g:- a+=5 is same as a=a+5
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
8460831302 Page 11
Fundamentals of Python
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Operator Name Example
8460831302 Page 12
Fundamentals of Python
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
8460831302 Page 13
Fundamentals of Python
Operator Description Example
and Returns True if both statements x < 5 and x < 10
are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False not(x < 5 and x < 10)
if the result is true
8460831302 Page 14
Fundamentals of Python
Operator Description Example
is Returns True if both x is y
variables are the same
object
is not Returns True if both x is not y
variables are not the same
object
8460831302 Page 15
Fundamentals of Python
Operator Description Example
in Returns True if a sequence x in y
with the specified value is
present in the object
8460831302 Page 16
Fundamentals of Python
not in Returns True if a sequence x not in y
with the specified value is
not present in the object
8460831302 Page 17
Fundamentals of Python
Multi line
Single line comment comment
block
keywords
Expression Statement
identifier
8460831302 Page 18
Fundamentals of Python
• Comments can be used to explain Python
code.
• Comments can be used to make the code
more readable.
• Comments can be used to prevent execution
when testing code.
• # is used to start single line comment
8460831302 Page 19
Fundamentals of Python
• ## or triple quotes are used to started
multiple line comment.
statement expression
8460831302 Page 20
Fundamentals of Python
A statement is a An expression is any
complete line of code section of the code that
that performs some evaluates to a value.
action
Print(a) A=b+10
8460831302 Page 21
Fundamentals of Python
• It is a valid combination of operators, literals
and variable.
– Arithmetic expression :- e.g. c=a+b
– Relational expression :- e.g. x>y
– Logical expression :- a or b
– String expression :- c=“comp”+”sc”
8460831302 Page 22
Fundamentals of Python
• A block is a piece of Python program text that is
executed as a unit. The following are blocks: a
module, a function body, and a class definition.
Each command typed interactively is a block.
E.g:-
if (a>0):
8460831302 Page 23
Fundamentals of Python
else:
8460831302 Page 24
Fundamentals of Python
print("+")
• A Python variable is a reserved memory
location to store values. In other words,
a variable in a python program gives data to
the computer for processing.
• E.g:-a,b
8460831302 Page 25
Fundamentals of Python
• Rules are same as identifiers.
• Always give meaningful name to variable
such as to store price, take variable name p
or price
• A constant is a type of variable whose value
cannot be changed.
• E.g:-PI = 3.14
8460831302 Page 26
Fundamentals of Python
Syntax:-
Variable name =value
E.g:-
A=10
a=b=c=10
a,b,c=10,20,30
8460831302 Page 27
Fundamentals of Python
• Variables can store data of different types, and
different types can do different things.
Data Types In Python:-
1. Number
2. String
3. Boolean
4. List
5. Tuple
6. Set
7. Dictionary
8460831302 Page 28
Fundamentals of Python
• Python can store number as:-
1. int:- a=5 or a=0
2. float a=2.5
3. complex a=3+5j(in a+bj format)
• String is collection of characters enclosed with
quotes.
8460831302 Page 29
Fundamentals of Python
• You can use single, double or triple quotes.
E.g:-
S=”vibrant”
Escape sequence:-\n,\t
• Can store only two value. True of False.
E.g:-a=True
8460831302 Page 30
Fundamentals of Python
• Tuple is a collection of data(any type of data)
separated by comma and enclosed with ().
E.g:-
T=(1,2,3,4,5)
8460831302 Page 31
Fundamentals of Python
• List is a collection of data(any type of data)
separated by comma and enclosed with [].
E.g:-
l=[1,2,3,4,5]
8460831302 Page 32
Fundamentals of Python
• Dictionary holds key:value pair seperated by
comma enclosed with {}.
E.g:-
D={‘name’:’Aayushi’,’class’:12}
8460831302 Page 33
Fundamentals of Python
• Input is used to input data from user.[in string]
• To convert inputted string to different data type
we need to write data type(int,float) or eval.
• Print is used to display output to the user.
E.g:-
a=int(input("enter number"))
print(a)
8460831302 Page 34
Fundamentals of Python
1. Write a Python program to display addition, subtraction,
multiplication, division of two numbers.
2. Write a Python program to display amount when user input
no of product and price of product.
3. Add statement to display GST(9% amount) in above
program.
4. Write a Python program to display simple interest when
principal amount, rate of interest , no of years input by user.
5. Write a Python program to display discount(5% of amount)
8460831302 Page 35
Fundamentals of Python
in program no 2.
8460831302 Page 36