5 967
Подписчики
Нет данных24 часа
-357 дней
-22530 дней
Архив постов
5 967
WHILE LOOP
while loop is used to run a block code until a certain condition is met. Syntax of while loop :
5 967
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(2, 30, 3):
5 967
EXAMPLE 2 :
The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):
5 967
RANGE FUNCTION IN FOR LOOP
The Python range() function returns a sequence of numbers, in a given range.
The most common use of it is to iterate sequence on a sequence of numbers using Python loops.
5 967
USING FOR LOOP WITH SET
thisset = {"Rushi", "Sumit", "Tamrej"}
for x in thisset:
print(x)
Output:
Rushi
Sumit
Tamrej5 967
ACCESS DICTIONARY VALUES USING FOR LOOP
thisdict = {
"Name": "Rushi",
"Class": "10th",
"Status": "Programmer"
}
for x in thisdict:
print(thisdict[x])
Output:
Rushi
11th
Programmer5 967
USING FOR LOOP WITH DICTIONARY
thisdict = {
"Name": "Rushi",
"Class": "10th",
"Status": "Programmer"
}
for x in thisdict:
print(x)
Output:
Name
Class
Status5 967
USING FOR LOOP WITH TUPLE
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
Output:
apple
banana
cherry5 967
LOOPING THROUGH A STRING
Even strings are iterable objects, they contain a sequence of characters:
for x in "Rushi":
print(x)
Output:
R
u
s
h
i5 967
Example :
data = ["Rushi", "Atharva", "Kartik"]
for x in data:
print(x)
Output:
Rushi
Atharva
Kartik5 967
FOR LOOP
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
5 967
TYPES OF LOOP IN PYTHON
There are two main types of loops in Python :
1. for loop
2. while loop
5 967
WHAT IS LOOP ?
In Python, a loop is a way to repeatedly execute a block of code.
5 967
NESTED IF ELSE
We can also use an if statement inside of an if statement. This is known as a nested if statement.
5 967
IF, ELIF & ELSE EXAMPLE :
number = 0
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number') print('This statement is always executed')
Output:
Zero
This statement is always executed