2 - Basics of Python Programming7
2 - Basics of Python Programming7
2 - Basics of Python Programming7
Syllabus
2021-22
Chapter 7
Basics of
Python
Programming
Computer Science
Class XI ( As per CBSE Board)
Basics of Python Programming
Structure of a python program
Program
|->Module -> main program
| -> functions
| ->libraries
|->Statements -> simple statement
| ->compound statement
|->expressions -> Operators
| -> expressions
|----objects ->data model
Python basics
var1=‘Computer Science'
var2=‘Informatics Practices'
print(var1,' and ',var2,' )
Output :-
Computer Science and Informatics Practices
raw_input() Function In Python allows a user to give input to a program from a
keyboard but in the form of string.
NOTE : raw_input() function is deprecated in python 3
e.g.
age = int(raw_input(‘enter your age’))
percentage = float(raw_input(‘enter percentage’))
input() Function In Python allows a user to give input to a program from a keyboard
but returns the value accordingly.
e.g.
age = int(input(‘enter your age’))
C = age+2 #will not produce any error
NOTE : input() function always enter string value in python 3.so on need int(),float()
function can be used for data conversion.
Indentation
Indentation refers to the spaces applied at the beginning of
a code line. In other programming languages the
indentation in code is for readability only, whereas the
indentation in Python is very important.
Python uses indentation to indicate a block of code or used
in block of codes.
E.g.1
if 3 > 2:
print(“Three is greater than two!") //syntax error due to not indented
E.g.2
if 3 > 2:
print(“Three is greater than two!") //indented so no error
Token
as finally or
continue if return
del in while
elif is with
except
Identifiers
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Operators continue
1. Arithmetic Operators
Arithmetic Operators are used to perform arithmetic
operations like addition, multiplication, division etc.
Operators Description Example
OUTPUT
('x + y =', 9) • Write a program in python to calculate the simple
('x - y =', 1) interest based on entered amount ,rate and time
('x * y =', 20)
('x / y =', 1)
('x // y =', 1)
('x ** y =', 625)
Operator continue
Arithmatic operator continue
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate, time);
print("Monthly EMI is= ", emi)
Operator continue
Arithmatic operator continue
How to calculate GST
GST ( Goods and Services Tax ) which is included in netprice of
product for get GST % first need to calculate GST Amount by subtract original
cost from Netprice and then apply
GST % formula = (GST_Amount*100) / original_cost
Output
('x > y is', False)
('x < y is', True)
('x == y is', False)
('x != y is', True)
('x >= y is', False)
('x <= y is', True)
Operators continue
3. Augmented Assignment Operators
Used to assign values to the variables.
Operators Description Example
= Assigns values from right side operands to left side operand a=b
//= Perform floor division on 2 numbers and assigns the result to left operand. a//=b
**= calculate power on operators and assigns the result to left operand. a**=b
Operators continue
4. Logical Operators
Logical Operators are used to perform logical operations on
the given two variables or values.
Operators Description Example
and return true if both condition are true x and y
return true if either or both
or x or y
condition are true
not reverse the condition not(a>b)
a=30
b=20
if(a==30 and b==20):
print('hello')
Output :-
hello
Operators continue
6. Membership Operators
The membership operators in Python are used to validate
whether a value is found within a sequence such as such as
strings, lists, or tuples.
Operators Description Example
in return true if value exists in the sequence, else false. a in list
not in return true if value does not exists in the sequence, else false. a not in list
E.g.
a = 22
list = [22,99,27,31]
In_Ans = a in list
NotIn_Ans = a not in list
print(In_Ans)
print(NotIn_Ans)
Output :-
True
False
Operators continue
7. Identity Operators
Identity operators in Python compare the memory locations of two
objects. Operators Description Example
is returns true if two variables point the same object, else false a is b
e.g. is not returns true if two variables point the different object, else false a is not b
a = 34
b=34
if (a is b):
print('both a and b has same identity')
else:
print('a and b has different identity')
b=99
if (a is b):
print('both a and b has same identity')
else:
print('a and b has different identity')
Output :-
both a and b has same identity
a and b has different identity
Operator continue
Operators Precedence :
highest precedence to lowest precedence table. Precedence is used to decide ,which
operator to be taken first for evaluation when two or more operators comes in an
expression.
Operator Description
** Exponentiation (raise to the power)
~+- Complement, unary plus,minus(method names for the last two are+@and -@)
* / % // Multiply, divide, modulo and floor division
fun()
print(x) #error will be shown
2. Global Variable
x=8
def fun():
print(x) # Calling variable ‘x’ inside fun()
fun()
print(x) # Calling variable ‘x’ outside fun()
Variables
Create a main.py:
import constant
print(constant.PI)
Note: In reality, we can not create constants in Python. Naming them in all
capital letters is a convention to separate them from variables, however, it
does not actually prevent reassignment, so we can change it’s value
Input and Output
print(‘Computer',‘Science')
print(‘Computer',‘Science',sep=' & ')
print(‘Computer',‘Science',sep=' & ',end='.')
Output :-
Computer Science
Computer & Science
Computer & Science.