0% found this document useful (0 votes)
19 views40 pages

Python Basic Sample Note (Ab Sir's Coaching - 8910350813)

The document discusses Python conditional statements and if/else syntax. It provides examples of using if, if/else, if/elif/else statements with relational operators and logical operators. It also covers nested if statements and the importance of indentation in Python.

Uploaded by

Amitava Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views40 pages

Python Basic Sample Note (Ab Sir's Coaching - 8910350813)

The document discusses Python conditional statements and if/else syntax. It provides examples of using if, if/else, if/elif/else statements with relational operators and logical operators. It also covers nested if statements and the importance of indentation in Python.

Uploaded by

Amitava Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Python Tutorial: Basic

[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’]

Out [39]: ‘Coach’

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…

Operators are used to perform operations on


variables and values.

Example:
a=b+c

Python divides the operators in the following


groups:
5
Python:
Arithmetic Operators

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

A conditional statement, symbolized


by p q, is an if-then statement in
which p is a hypothesis and q is a
conclusion. The logical connector in
a conditional statement is denoted
by the symbol . The conditional is
defined to be true unless a true
hypothesis leads to a false conclusion.
13
Conditional Statements:
if

A conditional statement, symbolized


by p q, is an if-then statement in
which p is a hypothesis and q is a
conclusion. The logical connector in
a conditional statement is denoted
by the symbol . The conditional is
defined to be true unless a true
hypothesis leads to a false conclusion.
14
Conditional Statements:
if (Categories)
Decision making statements in programming
languages decides the direction of flow of
program execution. Decision making statements
available in python are:
•if statement
•if..else statements
•if-elif ladder
•nested if statements
15
Conditional Statements:
if (Relational Operators)
Python supports the usual logical conditions
from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
16
Conditional Statements:
if (Syntax)

 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)

 One line if statement:

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)

 One line if else statement:

a=2
b=3
print("A") if a > b else print("B")

24
Note..!!

This technique is known as Ternary


Operators, or Conditional
Expressions.

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)

 One line if else statement, with 3


conditions:

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

You might also like