1
Python Language
Python is a general-purpose interpreted, interactive, object-oriented, and
high-level programming language.
It was developed by Guido van Rossum during 1985- 1990.
Like Perl, Python source code is also available under the GNU General
Public License (GPL).
Python is case sensitive computer language
Program 1 :
Display the string “ Hello Python”
print ('hello Python')
Using Variables
Program 2 : Store numbers and display, total, difference, product, division,
Exponent, Modulus and Integer division. (Practice with Variables and
Arithmetic operators)
x=10
y=4
print ('x=',x); print ('y=',y)
print ('total=',x+y);
print ('difference=',x-y)
print ('product=',x*y); print ('division=',x/y)
print ('Exponent=',x**y)
print ('Modulus=',x%y)
print ('Integer division',x//y)
2
Program 3
Store first name, last name and display the full name.
fname='Kumari'
lname='Perera'
print ('First Name:',fname)
print ('Last Name:',lname)
print ('Full Name:',fname+' '+lname)
Program 4
Input two numbers and display the Total.
num1=int(input('enter a number:'))
num2=int(input('enter next number:'))
print('total:',num1+num2);
Exercise 1
Write a python program to Input Marks of three subjects and display the total and
average of them.
Operators
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
3
Python Arithmetic Operators
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
Python Assignment Operators
Operator Example Same As
= x=5 x=5
+= 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
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
4
Python Comparison Operators
Operator Name Example
== 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
Python Logical Operators
Operator Description Example
and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of x < 5 or x < 4
the statements is true
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true
Python Bitwise Operators : Bitwise operators are used to compare
(binary) numbers
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right
and let the leftmost bits fall off
5
>> Signed right Shift right by pushing copies of the leftmost
shift bit in from the left, and let the rightmost bits fall off
Problems with “Selections” control structure.
If Statement
1. Input marks and if the marks greater than 90 display a message “Excellent”.
m=int(input('Enter Marks:'))
if (m>90): print('Exelent')
If / Else Statement
2. Input marks and if the marks greater than or equal to 50 display a message
“Pass” otherwise display “Fail”.
m=int(input('Enter Marks:'))
if (m>=50):
print('Pass')
else:
print ('Fail')
6
Nested if statement
3. Input science marks and if marks >= to 75 display grade=’A’
marks >= to 65 display grade=’B’
marks >= to 55 display grade=’C’
marks >= to 35 display grade=’S’
otherwise display grade=’W’
m=int(input('Enter Science Marks:'))
if (m>=75): grade='A'
elif (m>=65): grade='B'
elif (m>=55): grade='C'
elif (m>=35): grade='S'
else : grade='W'
print ('Grade is: ',grade)
Exercise 2
Input age of a person and if it is greater than or equal to 18 display “Adult”
otherwise display “Child”.
Input Income and Expenses of a person of the September month and find the
difference between them.
If the difference greater then 5000 display “Very Good” ,
If the difference greater then 1000 display “Good” ,
Otherwise display “Bad” ,
7
Loops
While Loop
1. .Display 1 to 5 numbers
Method 1
x=1
while x<=5:
print(x)
x=x+1
Method 2
x=1
while x<=5:
print(x, end=' ')
x=x+1
2. Display even numbers from 1 to 10
3. Display odd numbers from 1 to 10
4. Display numbers from 10 to 1 in descending order
8
Working with Strings
word='Hello Python'
print(word)
print(word[0])
print(word[6:12])
print(word[10:])
print(word*2)
print(word+' '+'CRC')
>>> print (word[2:]) llo Python
>>> print (word[:2]) He
>>>print (word[:2:]) He
>>>print (word[::2]) HloPto # one after the other
print('Sri\tLanka') #insert tab between words
print('Welcome\nCRC') #goto next line
String Methods
word.capitalize() 'Hello python'
word.upper() 'HELLO PYTHON'
word.lower() 'hello python'