Basics of Python Programming7
Basics of Python Programming7
Basics of Python
Programming
Computer Science
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
# 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')
Output :-
hello
Visit : python.mykvs.in for regular updates
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
return true if value does not exists in the sequence, else
not in a not in list
false.
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
Visit : python.mykvs.in for regular updates
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
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()
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
its value.
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.