coming soon....
Sunday 25 February 2024
Friday 23 February 2024
Decision control statements
Decision control statements: To know in detail click here
3. Nested if:
if a > 10:
print("Above ten,")
if a > 20:
print("and also above 20!")
else:
print("but not above 20.")
4. elif Statement:
if x > y:
print("x is greater")
elif x == y:
print("both x and y are equal")
else:
print("y is greater")
Python has two primitive loop commands:
- while loops
- for loops
while i < 6:
print(i)
i += 1
The break Statement:
while i < 6:
print(i)
if i == 4:
break
i += 1
The continue Statement:
while n < 6:
n = n + 1
if n == 3:
continue
print(n)
The else Statement
while i < 8:
print(i)
i += 1
else:
print("i is no longer less than 8")
What is pass statement in Python?
To avoid the indentation error in Python we simply used pass in Python. now you doubt in mind why we get an indentation error, so let's check this code.
Example:1
x
=
20
if
x >
20
:
# write code your here
print
(
'Hello all of you!'
)
output:
IndentationError: expected an indented block after 'if' statement
This is happen when the user does not know what code to write, So the user simply places a pass at that line.
Sometimes, the pass is used when the user doesn’t want any code to execute.
So users can simply place a pass where empty code is not allowed,
like in loops, function definitions, class definitions, or in if statements. So using a pass statement user avoids this error.
Use of pass keyword in Function
def
addition():
pass
Use of pass keyword in Python Class
Example:3
class
studentClass:
pass
Use of pass keyword in Conditional statement
Example:4
m
=
10
n
=
20
if
(m<n):
pass
else
:
print
(
"n<m"
)
Use of pass keyword in Python Loop
Example:5
list1
=
[
'a1'
,
'b1'
,
'c1'
,
'd1'
]
for
i
in
list1:
if
(i
=
=
'a1'
):
pass
else
:
print
(i)
2. for loop:Execute the statement as long as the condition is true.
Syntax:
for x in range(10): # do something
The range() Function
To Iterate (loop) through a set of code a specified number of times, we can use the range() function,
Example:1
for x in range(6):
print(x)
else:
print("Finally finished!")
Example:2The range() function defaults to 0 as a starting value, however, it is possible to specify the starting value by adding a parameter: range(2, 8), which means values from 2 to 6 (but not including 8):
for m in range(2, 8):
print(m)
Example:3
The range() function defaults to increment the sequence by 1, however it is
possible to specify the increment value by adding a third parameter: range(3, 60, 3):
for x in range(3, 60, 3):
print(x)
Example:4
Note:
The else block will not be executed if the for
loop is stopped by the break keyword.
for m in range(8):
if m == 4: break
print(m)
else:
print("Finally finished!")
Python Collections (Arrays)
There are four collection data types in the Python
programming language:
1.List:
- It is a collection which is ordered and changeable.
- Allows duplicate members.
Example:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x,y)
2. Tuple:
- It is a collection which is ordered and
unchangeable.
- Allows duplicate members.
Example:
tuple2 = (1, 5, 7, 9, 3)
print(tuple2)
3. Set :
- It is a collection which is unordered,
unchangeable*, and unindexed.
- No duplicate members.
- due to unordered you can not be sure in which
order items appear in output.
Example:
set1 = {"abc", 34, True, 40, "male"}
print(set1)
4. Dictionary
- It is a collection which is ordered** and
changeable. from 3.6 onwards unordered.
- No duplicate members.
Example:
thisdict1 = dict(name = "Joshi", age = 46, country = "India")
print(thisdict1)
Example:
thisdictstud = {
"name": "Ram",
"age": 25,
"class": "BE Comp"
}
print(thisdictstud["name"])