Python Basic Sample Note (Ab Sir's Coaching - 8910350813)
Python Basic Sample Note (Ab Sir's Coaching - 8910350813)
[Part – 1]
By
Prof. Amitav Biswas
(MBA,MCA, B.Sc. (H) in Computer Science)
Teacher/ HOD, Computer Sci. Dept.,
Behala College & Vivekananda College
1
Creating a Dictionary &
replacing elements..
In [35]: Dictionary={‘Kohli’:’Cricket’,’Messi’:’football’}
In [50]: Dictionary
Out [50]: {’Messi’:’football’, ‘Kohli’: ‘Coach’}
In [36]: Dictionary[‘Kohli’]=‘Coach’
In [37]: Dictionary
Out [38]: {’Messi’:’football’, ‘Kohli’: ‘Coach’}
2
Accessing elements
from a dictionary..
In [39]: Dictionary=[‘Kohli’]
In [40]: Dictionary=[‘Messi’]
Out [40]: ‘Football’
3
Removing elements
from a dictionary..
In [41]: del Dictionary=[‘Kohli’]
In [42]: Dictionary
Out [42]: {‘Messi’: ‘Football’}
4
Python Operators…
Example:
a=b+c
6
Python:
Assignment Operators
7
Python:
Comparison / Relational
Operators
8
Python:
Logical Operators
9
Python:
Identity Operators
Identity operators are used to compare the
objects, not if they are equal, but if they are
actually the same object, with the same memory
location:
10
Python:
Membership Operators
Membership operators are used to test if a
sequence is presented in an object:
11
Python:
Bitwise Operators
Bitwise operators are used to compare (binary)
numbers:
12
Conditional Statements
if:
if expression:
statement(s)
17
Conditional Statements:
if (flow chart)
18
Conditional Statements:
if (Example)
a = 10
b = 20
if b > a:
print("b is greater than a")
19
Conditional Statements:
if (Example)
a=20
b=10
if a > b: print("a is greater than b")
20
Conditional Statements:
if..else (Syntax)
if..else:
if expression:
statement(s)
else:
statement(s)
21
Conditional Statements:
if..else (flow chart)
22
Conditional Statements:
if..else (Example)
a = 10
b = 20
if b > a:
print("b is greater than a")
else:
print("a is greater than b")
23
Conditional Statements:
if..else (Example)
a=2
b=3
print("A") if a > b else print("B")
24
Note..!!
25
Conditional Statements:
if..elif..else (Syntax)
if..elif..else:
if expression1:
statement(s)
elif expression2:
statement(s)
……………
else:
statement(s) 26
Conditional Statements:
if..elif..else (flow chart)
27
Conditional Statements:
if..elif..else (Example 1)
a = 10
b = 10
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
28
Conditional Statements:
if..elif..else (Example 2)
a = 20
b = 30
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b") 29
Conditional Statements:
if..elif..else (Example 3)
a = 20
b = 20
print("A") if a > b else print("=") if a
== b else print("B")
30
Conditional Statements:
Nested if
Nested if:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
31
Conditional Statements:
Nested if (flow chart)
32
Conditional Statements:
Nested if (Example)
num = int(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number") 33
Conditional Statements:
if (Indentation)
Python relies on indentation (whitespace at
the beginning of a line) to define scope in
the code. Other programming languages
often use curly-brackets for this purpose.
34
Conditional Statements:
if (Example)
If statement, without indentation (will raise
an error):
a = 33
b = 200
if b > a:
print("b is greater than a")
# you will get an error
35
Conditional Statements:
if (Logical Operator)
And:
The and keyword is a logical operator, and is
used to combine conditional statements:
Example
Test if a is greater than b, AND if c is
greater than a:
36
Conditional Statements:
if (Logical Operator)
a = 20
b=3
c = 50
if a > b and c > a:
print("Both conditions are True..!!")
37
Conditional Statements:
if (Logical Operator)
Or
The or keyword is a logical operator, and is
used to combine conditional statements:
Example
Test if a is greater than b, OR if a is greater
than c:
38
Conditional Statements:
if (Logical Operator)
a = 20
b=3
c = 50
if a > b or a > c:
print("At least one of the conditions is
True")
39
Conditional Statements:
if (Logical Operator)
not
Reverse the result, returns False if the result
is true
a = 20
b = 20
if not (a == b):
print(“both are not equal")
40