ch-7-basic-of-python-programming
ch-7-basic-of-python-programming
syllabus
Chapter 7 2023-24
Basics of Python
Programming
Computer Science
Class XI ( As per CBSE Board)
Visit : dineshkvs.wordpress.com for regular updates
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
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.
E.g.2
if 3 > 2:
print(“Three is greater than two!") //indented so no error
as finally or
continue if return
del in while
elif is with
except
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
OUTPUT
('x + y =', 9)
('x - y =', 1)
('x * y =', 20)
('x / y =', 1)
• Write a
program
in python
to
calculate
the
simple
interest
based on
entered
amount
,rate and
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate, time);
print("Monthly EMI is= ", emi)
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)
= 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
a=30
b=20
if(a==30 and b==20):
print('hello')
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 :-
False
Visit : python.mykvs.in for regular updates
True
Operators
7. Identity Operators
continue
Identity operators in Python compare the memory locations of two objects.
Ope
Exam
rator Description
e.g. ple
s
a = 34
b=34 returns true if two variables point
is a is b
if (a is b): the same object, else false
print('both a and b has same identity') returns true if two variables point a is
is not
else: the different object, else false not b
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
Visit : python.mykvs.in for regular updates
Operator continue
Operators Precedence :
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()
Lvalue and Rvalue refer to the left and right side of the
assignment operator. The Lvalue (pronounced: L value)
concept refers to the requirement that the operand on the
left side of the assignment operator is modifiable, usually
a variable. Rvalue concept fetches the value of the
expression or operand on the right side of the assignment
operator. example:
amount = 390
The value 390 is pulled or fetched (Rvalue) and stored
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
Visit : dineshkvs.wordpress.com
Input and Output
print() Function In Python is used to print output on the screen.
Syntax of Print Function - print(expression/variable)
e.g.
print(122)
Output :-
122
print('hello India')
Output :-
hello India
print(‘Computer',‘Science')
print(‘Computer',‘Science',sep=' & ')
print(‘Computer',‘Science',sep=' & ',end='.')
Output :-
Computer Science
Computer & Science
Computer & Science.
Visit : dineshkvs.wordpress.com