DAY 5 - Decision Control & Loop
#DecisionControl #Loop
#Command for user input:
name = input("Enter your name: ")
#Decision Control
##########################################
1.
if [boolean expression]:
[statements]
2.
if [boolean expression]:
[statements]
else:
[statements]
3.
if [boolean expression]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
else:
[statements]
#Loop
##########################################
1.
#While Loop
while [boolean expression]:
[statements]
for example:
num =0
while num< 5:
num = num + 1
print("num =", num)
o/p:
num = 1
num = 2
num = 3
num = 4
num = 5
2.
#For Loop
#The for loop is used with sequence types such as list, tuple and set.The body of the for loop is executed for each member element in the sequence.
for x in sequence:
statements
for example:
for x in mylist/mytuple/set:
print(x) #prints each element of these object(s)
statements
#variable x in the for statement refers to the item at the 0 index in the sequence and keeps on moving
#for loop for Dictionary:
dict={ 1:100, 2:200, 3:300 }
for pair in dict.items():
print (pair)
for k,v in dict.items():
print("key=" , k)
print("value=", v)
for k in dict.keys():
print("key=" , k)
for v in dict.values():
print("value=" , v)
o/p:
(1, 100)
(2, 200)
(3, 300)
key=1
value=100
key=2
value=200
key=3
value=300
key=1
key=2
key=3
value=100
value=200
value=300
#items() method of the dictionary object returns a list of tuples, each tuple having a key and value corresponding to each item.
#for loop for string
for char in "Python":
print (char)
o/p:
P
y
t
h
o
n
#loop using range command
#Python range() generates the integer numbers between the given start integer to the stop integer, which is generally used to iterate over with for loop.
#range (start, stop[, step])
1.Out of the three 2 arguments are optional. I.e., Start and Step are the optional arguments.
2.A start argument is a starting number of the sequence. i.e., lower limit. By default, it starts with 0 if not specified.
3.A stop argument is an upper limit. i.e.generate numbers up to this number, The range() function doesn’t include this number in the result.
4.The step is a difference between each number in the result. The default value of the step is 1 if not specified.for example: start,start+step,...and so on
for i in range(1, 10, 2):
print(i, end=', ')
o/p:
1, 3, 5, 7, 9,
Observations:
1.range() function only works with the integers i.e., whole numbers.
2.The step value must not be zero. If a step is zero python raises a ValueError exception.
3.Using a len(list), we got total elements of a list so we can iterate for loop fixed number of time.
4.reverseed_range = list(reversed(range(0,5))) # [4, 3, 2, 1, 0]
5. code to generate letters from a to z
print ("""Generates the characters from `a` to `z`, inclusive.""")
def character_range(char1, char2):
for char in range(ord(char1), ord(char2)+1):
yield (char)
for letter in character_range('a', 'z'):
print( chr(letter), end=", " )
3. break - if the required object is found, an early exit from the loop is sought using break statement
#prime Number Code
num=int(input("Enter a number: "))
for x in range(2,num):
if num%x==0:
print("{} is not prime".format(num))
break
else:
print ("{} is prime".format(num))
o/p:
Enter a number: 57
57 is not prime
4. continue - the continue statement skips the remaining statements in the current loop and starts the next iteration
for num in range(1,6):
if num==3:
continue
print ("Num = {} ".format(num))
print ("Out of loop")
o/p:
Num = 1
Num = 2
Num = 4
Num = 5
Out of loop
5. pass - the pass statement is simply ignored by the Python interpreter and can be seen as a null statement.
for num in range(1,6):
if num==3:
pass
else:
print ("Num = {} ".format(num))
o/p:
Num = 1
Num = 2
Num = 4
Num = 5
#RR #Day5 #Python #DecisionControl #Loop #DataScience #HappyLearning #WeLearnEveryday
#Command for user input:
name = input("Enter your name: ")
#Decision Control
##########################################
1.
if [boolean expression]:
[statements]
2.
if [boolean expression]:
[statements]
else:
[statements]
3.
if [boolean expression]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
else:
[statements]
#Loop
##########################################
1.
#While Loop
while [boolean expression]:
[statements]
for example:
num =0
while num< 5:
num = num + 1
print("num =", num)
o/p:
num = 1
num = 2
num = 3
num = 4
num = 5
2.
#For Loop
#The for loop is used with sequence types such as list, tuple and set.The body of the for loop is executed for each member element in the sequence.
for x in sequence:
statements
for example:
for x in mylist/mytuple/set:
print(x) #prints each element of these object(s)
statements
#variable x in the for statement refers to the item at the 0 index in the sequence and keeps on moving
#for loop for Dictionary:
dict={ 1:100, 2:200, 3:300 }
for pair in dict.items():
print (pair)
for k,v in dict.items():
print("key=" , k)
print("value=", v)
for k in dict.keys():
print("key=" , k)
for v in dict.values():
print("value=" , v)
o/p:
(1, 100)
(2, 200)
(3, 300)
key=1
value=100
key=2
value=200
key=3
value=300
key=1
key=2
key=3
value=100
value=200
value=300
#items() method of the dictionary object returns a list of tuples, each tuple having a key and value corresponding to each item.
#for loop for string
for char in "Python":
print (char)
o/p:
P
y
t
h
o
n
#loop using range command
#Python range() generates the integer numbers between the given start integer to the stop integer, which is generally used to iterate over with for loop.
#range (start, stop[, step])
1.Out of the three 2 arguments are optional. I.e., Start and Step are the optional arguments.
2.A start argument is a starting number of the sequence. i.e., lower limit. By default, it starts with 0 if not specified.
3.A stop argument is an upper limit. i.e.generate numbers up to this number, The range() function doesn’t include this number in the result.
4.The step is a difference between each number in the result. The default value of the step is 1 if not specified.for example: start,start+step,...and so on
for i in range(1, 10, 2):
print(i, end=', ')
o/p:
1, 3, 5, 7, 9,
Observations:
1.range() function only works with the integers i.e., whole numbers.
2.The step value must not be zero. If a step is zero python raises a ValueError exception.
3.Using a len(list), we got total elements of a list so we can iterate for loop fixed number of time.
4.reverseed_range = list(reversed(range(0,5))) # [4, 3, 2, 1, 0]
5. code to generate letters from a to z
print ("""Generates the characters from `a` to `z`, inclusive.""")
def character_range(char1, char2):
for char in range(ord(char1), ord(char2)+1):
yield (char)
for letter in character_range('a', 'z'):
print( chr(letter), end=", " )
3. break - if the required object is found, an early exit from the loop is sought using break statement
#prime Number Code
num=int(input("Enter a number: "))
for x in range(2,num):
if num%x==0:
print("{} is not prime".format(num))
break
else:
print ("{} is prime".format(num))
o/p:
Enter a number: 57
57 is not prime
4. continue - the continue statement skips the remaining statements in the current loop and starts the next iteration
for num in range(1,6):
if num==3:
continue
print ("Num = {} ".format(num))
print ("Out of loop")
o/p:
Num = 1
Num = 2
Num = 4
Num = 5
Out of loop
5. pass - the pass statement is simply ignored by the Python interpreter and can be seen as a null statement.
for num in range(1,6):
if num==3:
pass
else:
print ("Num = {} ".format(num))
o/p:
Num = 1
Num = 2
Num = 4
Num = 5
#RR #Day5 #Python #DecisionControl #Loop #DataScience #HappyLearning #WeLearnEveryday
Comments
Post a Comment