#logical operator and with if
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Output:
Both conditions are True
#logical or
#Test if a is greater than b, OR if a is greater than c:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Output:
At least one of the conditions is True
# Nested If
# You can have if statements inside if statements,
# this is called nested if statements.
#x=41
x = 15
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Output:
Above ten,
but not above 20.
# The pass Statement
# if statements cannot be empty,
# but if you for some reason have an if statement with no content,
# put in the pass statement to avoid getting an error.
a = 33
b = 200
if b > a:
pass
print("Welcome")
Output:
Welcome
# Python Loops
# Python has two primitive loop commands:
#
# while loops
# for loops
# The while Loop
# With the while loop we can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
#Note: remember to increment i, or else the loop will continue forever.
Output:
1
2
3
4
5
# The break Statement
# With the break statement we can stop the loop even if the while condition is true:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Output:
1
2
3
# The continue Statement
# With the continue statement we can stop the current iteration, and continue with the next:
i = 0
while i < 6:
i+=1 #i++ not allowed
if i == 3:
continue
# if i == 3:
# continue
print(i)
Output:
1
2
4
5
6
# The else Statement
# With the else statement
# we can run a block of code once when the condition no longer is true:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Output:
1
2
3
4
5
i is no longer less than 6
# Python For Loops
# A for loop is used for iterating over a sequence
# (that is either a list, a tuple, a dictionary, a set, or a string).
#
# This is less like the for keyword in other programming languages,
# and works more like an iterator method
# as found in other object-orientated programming languages.
#
# With the for loop we can execute a set of statements,
# once for each item in a list, tuple, set etc.
fruits = ["apple", "banana", "cherry"]
# print("apple" in fruits)
# in is membership operator
for x in fruits:
print(x)
#The for loop does not require an indexing variable to set beforehand.
Output:
apple
banana
cherry
# Looping Through a String
# Even strings are iterable objects,
# they contain a sequence of characters:
name="SVERI's COE Pandharpur"
for p in name:
print(p)
Output:
S
V
E
R
I
'
s
C
O
E
P
a
n
d
h
a
r
p
u
r
# The break Statement
# With the break statement we can stop the loop
# before it has looped through all the items:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Output:
apple
banana
# Exit the loop when x is "banana",
# but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
Output:
apple
# The continue Statement
# With the continue statement
# we can stop the current iteration of the loop, and continue with the next:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
Output:
apple
cherry
# The range() Function
# To loop through a set of code a specified number of times,
# we can use the range() function,
# The range() function returns a sequence of numbers,
# starting from 0 by default, and increments by 1 (by default),
# and ends at a specified number.
#increment by default 1
# for x in range(5):
# print(x)
#Note that range(6) is not the values of 0 to 6, but the values 0 to 5.
#increment by default 1
for x in range(2, 6):
print(x)
Output:
2
3
4
5
#increment will be with 3
count=0
for x in range(2, 30,3):
print(x)
count+=1
print("Total such numbers=",count)
Output:
2
5
8
11
14
17
20
23
26
29
Total such numbers= 10
# else in For Loop
# The else keyword in a for loop specifies a block of code
# to be executed when the loop is finished:
for x in range(6):
print(x)
else:
print("Finally finished!")
Output:
0
1
2
3
4
5
Finally finished!
# Nested Loops
# A nested loop is a loop inside a loop.
#
# The "inner loop" will be executed one time
# for each iteration of the "outer loop":
# adj = ["healthy", "yellow", "tasty"]
# fruits = ["apple", "orange", "cherry"]
#
# for x in adj:
# for y in fruits:
# print(x, y)
outer= [10,20,30]
inner = [1,2,3,4]
for x in outer:
for y in inner:
print(x, y)
Output:
10 1
10 2
10 3
10 4
20 1
20 2
20 3
20 4
30 1
30 2
30 3
30 4
# The pass Statement
# for loops cannot be empty,
# but if you for some reason have a for loop with no content,
# put in the pass statement to avoid getting an error.
for x in [0, 1, 2]:
pass
Output:
Welcome
#Booleans represent one of two values: True or False.
print(10 > 9)
print(10 == 9)
print(10 < 9)
Output:
True
False
False
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
Output:
b is not greater than a
# Evaluate Values and Variables
# The bool() function allows you to evaluate any value,
# and give you True or False in return,
# print(bool("Hello"))
# print(bool(15))
x = "Hello"
y = 15
#
print(bool(x))
print(bool(y))
Output:
True
True
# Most Values are True
# Almost any value is evaluated to True if it has some sort of content.
# Any string is True, except empty strings.
# Any number is True, except 0.
# Any list, tuple, set, and dictionary are True, except empty ones.
print(bool("abc"))
print(bool(123))
print(bool(["apple", "cherry", "banana"]))
Output:
True
True
True
# Some Values are False
# In fact, there are not many values that evaluates to False,
# except empty values, such as (), [], {}, "",
# the number 0, and the value None.
# And of course the value False evaluates to False.
print(bool(False))
print(bool(None))
print(bool(0))
print(bool(""))
print(bool(()))
print(bool([]))
print(bool({}))
Output:
False
False
False
False
False
False
False
Onkar Kale TYB_71
VINAYAK Gawali TYA_72
Saurabh Nikam TYB_32
TYB_73 vaibhav Jagtap TYB73
TYB_41 Yasar Khatik TYB_41
TYB_55 Saurabh Wadekar TYB 55
Sachin Kshirsagar TYA_42
TYA_18 Yogesh chavan
Pranay Disale TA_50 Pranay Disale
TYA_18 Yogesh chavan TA_18
TYA_25 Yash Gadekar TYA_25
Prathmesh Kirgat TYA_41
deepjyoti sathe TYA_08
sumit Patwari TYB_77
CVP CVP TYA_35 Parchandrao Chandragupta Vinayak
SALIM SHAIKH TYB_45
Pratik Deshmukh TYB_14
Madhuri Parchandrao TYA_06 Madhuri Parchandrao
Milind Jagadale TYA_34
Dhanashree Sonawane TYB06
Yogesh patil TYB_36
Jaydev nanaware TYB_31
Samruddhi Deshpande TYB_01
OMKAR PAWAR TYB_37
Snehal Mane TYB_04
Pravin Somdale TYA_65
TYB_42Kashiling Sarak TYB_42
Shraddha gajakosh TYA_02
Learning is essential. Learn Free TYB_54
TYB_33 Ridham Parmar TYB_33
TYB_75 Hrushikesh Walujkar TYB_75
ADESH BANKAR TYB_09 Adesh Bankar
Aazam Shaikh TYA_63
TYA_64 Sohel Shikalgar TYA 64
PrerAna Ekatpure TYB02
Prajwal Musale TYA_55
Shreeyash Chavan TYA_17
Vijay Mali TYB_26 AMOL MALI
pruthviraj deshmukh TA23
Vaibhav Bhosale TB_13
ABHI KHOTE TYB_22
TYB_03 Arati Lale TB 03
manthan dixit TYB_16
Saleha Mirjkar TYA-05
Nilesh Kadam TYB-21
Shubham Deokar TYB 72
Santosh Patil TYB_35
Akshay Pansare TYA_43
Harshvardhan Ubale TYB_50
TYB_05 Vaishali More TYB_05
Samarth Mane-Deshmukh TA_51
Its_Pranav_ Show TYB 59
TYB_20 Dhondiram Waghmode TB20
Sachin Waghmare TA49
TYB_41 Yasar Khatik TYB_41