Python PPT
Python PPT
Suma Debsarma
Department of Applied Mathematics
University of Calcutta
Python – a mysterious name
Python is a widely used general-purpose,
high level programming language. It was
initially designed by Dutch programmer
Guido van Rossum in 1991.
The name Python comes from an old BBC
television comedy sketch series called
Monty Python’s Flying Circus. When
Guido van Rossum was creating Python, he
was also reading the scripts of Monty
Python. He thought the name Python was
appropriately short and slightly mysterious.
Why should we learn Python?
Set is a collection of elements that is written with curly brackets. It is unindexed and
unordered. Example: S = {x for x in 'abracadabra' if x not in 'abc'}
Dictionary is a collection which is ordered, changeable and does not allow duplicates.
It is written with curly brackets and objects are stored in key: value format.
Example: X = {1:’A’, 2:’B’, 3:’c’}
print function
>>>type(print)
Output:
builtin_function_or_method
1 print(item, end=’ ‘)
2
3 Output:
4 1 2 3 4 5
5
Operators
Addition + Subtraction -
Multiplication * Exponentiation **
Division / Integer division //
Remainder %
Binary left shift << Binary right shift >>
And & Or |
Less than < Greater than >
Less than or equal to <= Greater than or equal to >=
Check equality == Check not equal !=
Precedence of operators
Parenthesized expression ( ….. )
Exponentiation **
Positive, negative, bitwise not +n, -n, ~n
Multiplication, float division, int division, remainder *, /, //, %
Addition, subtraction +, -
Bitwise left, right shifts <<, >>
Bitwise and &
Bitwise or |
Membership and equality tests in, not in, is, is
not, <, <=, >, >=, !=, ==
Boolean (logical) not not x
Boolean and and
Boolean or or
Conditional expression if ….. else
Precedence of Operators
Examples:
a = 20
b = 10
c = 15
d=5
e=2
f = (a + b) * c / d
print( f)
g = a + (b * c) / d - e
print(g)
h = a + b*c**e
print(h)
Multiple Assignment
Python allows you to assign a single value to several variables
simultaneously.
a = b = c = 1.5
a, b, c = 1, 2, " Red“
Here, two integer objects with values 1 and 2 are assigned to
variables a and b respectively and one string object with the
value "Red" is assigned to the variable c.
Special Use of + and *
Examples:
x = "Python is "
y = "awesome."
z=x+y
print(z)
Output:
Python is awesome.
A Multi line comment is useful when we need to comment on many lines. In python,
triple double quote(“ “ “) and single quote(‘ ‘ ‘)are used for multi-line commenting.
Example:
“““ My Program to find
Average of three numbers ”””
a = 29 # Assigning value of a
b = 17 # Assigning value of b
c = 36 # Assigning value of c
average = ( a + b + c)/3
print(“Average value is ”, average)
id( ) function, ord( ) function
id( ) function: It is a built-in function that returns the unique identifier of
an object. The identifier is an integer, which represents the memory address
of the object. The id( ) function is commonly used to check if two variables
or objects refer to the same memory location.
>>> a=5
>>> id(a)
1403804521000552
>>> ord(‘A’)
65
>>> chr(65)
‘A’
Control Flow Structures
1. Conditional if ( if )
2. Alternative if ( if else )
3. Chained Conditional if ( if elif else )
4. While loop
5. For loop
Conditional if
Example:
a=10
if a > 9 :
print("a is greater than 9")
Output:
a is greater than 9
Alternative if
Example:
Output:
PASS
# Test if the given letter is vowel or not
letter = ‘o’
if letter == ‘a’ or letter == ‘e’ or letter == ‘i’ or letter == ‘o’
or letter == ‘u’ :
print(letter, ‘is a vowel.’)
else:
print(letter, ‘is not a vowel.’)
Output:
o is a vowel.
# Program to find the greatest of three different numbers
a = int(input('Enter 1st no’))
b = int(input('Enter 2nd no'))
c= int(input('Enter 3rd no'))
if a > b:
if a>c: Output:
print('The greatest no is ', a) Enter 1st no 12
else: Enter 2nd no 31
print('The greatest no is ', c) Enter 3rd no 9
else: The greatest no is 31
if b>c:
print('The greatest no is ', b)
else:
print('The greatest no is ', c)
Chained conditional if
# Program to guess the vegetable
color = “green”
if color == “red”:
print(‘It is a tomato.’)
elif color == “purple”:
print(‘It is a brinjal.')
elif color == “green”:
print(‘It is a papaya. ')
else:
print(‘There is no such vegetable.’)
Output:
It is a papaya.
# Program to find out the greatest of four different numbers
a=int(input('Enter 1st no ‘))
b=int(input('Enter 2nd no ‘))
c=int(input('Enter 3rd no ‘))
d=int(input('Enter 4th no ‘))
if (a>b and a>c and a>d):
print('The greatest no is ', a) Output:
elif (b>c and b>d): Enter 1st no 23
print('The greatest no is ', b) Enter 2nd no 10
elif (c>d): Enter 3rd no 34
print('The greatest no is ', c) Enter 4th no 7
elif d>c : The greatest no is 34
print('The greatest no is ', d)
else:
print('At least two values are equal')
# Program to find out Grade
marks = int(input('Enter total marks ‘))
total = 500 # Total marks
percentage=(marks/total)*100
if percentage >= 80:
print('Grade O')
elif percentage >=70:
print('Grade A')
elif percentage >=60: Output:
print('Grade B') Enter total marks 312
elif percentage >=40: Grade B
print('Grade C')
else:
print('Fail‘)
While loop
# Python program to find first ten Fibonacci numbers
a=1
print(a)
b=1
print(b)
i=3
while i<= 10:
c=a+b
print(c)
a=b
b=c
i=i+1
For loop
# Program to find the sum of squares of first n natural numbers
n = int(input('Enter the last number '))
sum = 0
for i in range(1, n+1):
sum = sum + i*i
print('The sum is ', sum)
Output:
Enter the last number 8
The sum is 204
For loop
# Program to find the sum of a given set of numbers
numbers = [11, 17, 24, 65, 32, 69]
sum = 0
for no in numbers:
sum = sum + no
print('The sum is ', sum)
Output:
The sum is 218
# Program to print 1, 22, 333, 444, .... in triangular form
n = int(input('Enter the number of rows '))
for i in range(1, n+1):
for j in range(1, i+1):
print(i, end='')
print()
Output:
22
# Program to print opposite right triangle
n = int(input('Enter the number of rows '))
for i in range(n, 0, -1):
for j in range(1, i+1):
print('*', end='')
print()
Output:
*****
****
***
# Program to print opposite star pattern
n = int(input('Enter the number of rows '))
for i in range(0, n):
for j in range(0, n-i):
print(' ', end='')
for k in range(0, i+1):
print('*’, end='')
Output:
print('')
*
**
***
****
*****
# Program to print A, AB, ABC, ABCD, ......
ch = str(input('Enter a character '))
val=ord(ch)
for i in range(65, val+1):
for j in range(65, i+1):
print(chr(j), end='')
print()
Output:
AB
ABC
# Program to test Palindrome numbers
n=int(input('Enter an integer '))
x=n
r=0
while n>0:
d=n%10
r=r*10+d
n=n//10
if x==r:
print(x,' is Palindrome number’)
else:
print(x, ' is not Palindrome number')
# Program to print Pascal Triangle
n=int(input('Enter number of rows '))
for i in range(0, n):
for j in range(0, n-i-1):
print(end=' ')
for j in range(0, i+1): Output:
print('*', end=' ') Enter number of rows 6
print() *
* *
* * *
* * * *
* * * * *
* * * * * *
Break and Continue
In Python, break and continue statements can alter the flow of a normal
loop.
Output:
The number 88 is found
Break and Continue
‘x’ Creates a new file. If file already exists, the operation fails.
.
Creating output file
# Write a python program to open and write “hello world” into a file.
f=open("file.txt","a")
f.write("hello world")
f.close( )
# Python program to write the content “Hi python programming” for the existing file.
f=open("MyFile.txt",'w')
f.write("Hi python programming")
f.close()
# Write a python program to open and write the content to file and read it.
f=open("abc.txt","w+")
f.write("Python Programming")
print(f.read())
f.close()
References