Python Note 1
Python Note 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 :
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 ('total=',x+y);
print ('difference=',x-y)
print ('Exponent=',x**y)
print ('Modulus=',x%y)
Program 3
Store first name, last name and display the full name.
fname='Kumari'
lname='Perera'
Program 4
num1=int(input('enter a 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
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
= 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
== Equal x == y
!= Not equal x != y
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true
<< Zero fill left shift Shift left by pushing zeros in from the right
and let the leftmost bits fall off
5
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
if (m>=75): grade='A'
else : grade='W'
Exercise 2
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:
x=x+1
word='Hello Python'
print(word)
print(word[0])
print(word[6:12])
print(word[10:])
print(word*2)
print(word+' '+'CRC')
String Methods
word.capitalize() 'Hello python'
word.upper() 'HELLO PYTHON'
word.lower() 'hello python'