Professional Documents
Culture Documents
2 Conditions
2 Conditions
Intelligence (AI)
Agenda
▰ Conditional Statements
▰ Loops
▰ Functions
▰ String Handling
2
Conditional Statement
3
Back to main
Conditional Statement:
4
Conditional Statements:
▰ if statements
▰ if-else statements
▰ elif statements
▰ Nested if and if-else statements
5
If Statement:
6
Syntax :
if expression:
Statement
7
Example :
num = 10
if num > 0:
print(num, "is a positive number.")
OUTPUT: 5 is a positive number.
a = 25
b = 100
if b > a:
print("b is greater than a")
OUTPUT: b is greater than a
8
if-else statement:
This statement deals with both the true and false parts
of a given condition. If the condition is true, the
statement inside the if block is executed and if the
condition is false, the statement outside the if block is
executed.
9
Syntax :
if condition :
Statement
#This block execute when the condition is true
else :
Statement
#This block execute when the condition is false
10
Example :
num = 10
if num >= 1:
print("Positive number")
else:
print("Negative number")
OUTPUT: Positive number
11
elif statement:
12
Syntax :
if condition :
Statement
elif condition :
Statement
else:
Statement
13
Example :
num = 5
if num == 0:
print("Zero")
else:
print("Negative number")
OUTPUT: Positive number
14
Nested if and if-else statements:
15
Syntax :
if condition 1:
Executes if condition 1 is true
if condition 2:
Executes if condition 2 is true
Nested if ends here
if ends here
16
Example :
num = 10
if num >= 0:
if num == 0:
print("zero")
else:
print("Positive number")
else:
print("Negative number")
OUTPUT: Positive number
17
Back to main
LOOPS :
18
Back to main
Types of Loops:
Conditional Code
▰ while loop
▰ for loop Condition
▰ nested loop If Condition is True
If Condition is False
19
While Loop :
20
Flow Chart :
21
Example :
num = 1
while num <= 5:
print("Hello World!")
num += 1
OUTPUT:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
22
For Loop :
23
Flow Chart :
https://editor.analyticsvidhya.com/uploads/62260python_for_loop.jpg
24
Example :
list = [1, 2, 3, 4, 5]
for num in list:
print(num)
25
Nested loops:
27
Example : Break statement
28
Example : continue statement
num = 0
while num < 10:
num += 1
if num == 5:
continue
print(num)
29
Back to main
FUNCTIONS:
30
Back to main
Syntax:
def function_name():
Statements
31
Example :
def Hello():
print(“Hello World”)
Calling a Function:
To execute a Function, it needs to be called. In order to
call a function, we simply type the function name.
32
Calling a Function
Hello()
Docstring in a function:
Docstrings stands for Documentation strings used to
describe what the function does.
Inclusion of docstring in a function is optional.
33
Example :
def Hello():
" " "This is a very first function created
in Python " " "
print("Hello World")
34
Function with return Statement
return expression
35
Example :
print(func1("March"))
Hello, March. Good morning!
None
36
Example :
def func2(a,b):
return a+b
func2(5,2) #Calling function with a return statement
37
Function Parameters and Arguments
38
Built In Functions:
40
Built In Functions:
41
Built In Functions:
42
Built In Functions:
43
Back to main
String Handling:
44
Back to main
How to use quotation marks in Strings?
45
String Operations:
String Concatenation:
Concatenation is the joining together of two strings
46
Example:
string1 =”Hello “
string2=”Python”
print (string1 + string2)
string1 =”Hello “
num=100
print (string1 + str(num))
47
String Formatting:
49
Example:
print (string)
50
Formatting with format() string method.
Syntax:
‘String here {} then also {}’.format(‘something1′,’something2’)
51
Formatting with format() string method.
print('The first {p} was alright, but the {p} {p} was tough.'.format(p =
'second'))
52
F-strings
a=5
b = 10
print(f"He said his age is {2 * (a + b)}.")
53
String Template Class:
54
String Methods:
55
String Methods:
58
String Methods: