> For the complete documentation index, see [llms.txt](https://ankit-apdc.gitbook.io/python-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ankit-apdc.gitbook.io/python-1/part-1-basics/control-flow-and-loop.md).

# Control Flow and Loop

## Comparison Operator

* We use a comparison operator to compare values. It will return a boolean value
* A comparison operator can be used to compare data of the same types

```python
# Value for all comparison operators listed below is True

10 > 3 # Greater than
10 >=3 # Greater than or equal to
10 < 20 # Less than
10 <= 20 # Less than or equal to
10 == 10 # Equal to 
10 != "10" # Not equal to

"bag" > "apple" # When we sort these two strings bag comes after apple thats why true
print(ord("b")) # is 98
print(ord("B")) # is 66
```

## Conditional Statements

* If statement will contain an expression that will return a boolean(True, False) value&#x20;
* If we use an if statement, always terminate it with a colon&#x20;
* All the statement which are indented by a tab will belong to this if statement

```python
# Self explanatory conditional statement example is here

temperature = 35
if temperature > 30:
    print("Inside if")
    print("Still inside if")
elif temperature > 20:
    print("Inside elif")
else:
    print("This is optional")
print("Outside if")
```

### Ternary Operator

Using ternary operator we can write multiple conditions in one line itself

```python
# Sample if condition
age = 22
if age >= 18:
    message = "Eligible"
else:
    message = "Not Eligible"
print(message)

# Same if condition using Ternary Operator
age = 22
message = "Eligible" if age >= 18 else "Not Eligible"
print(message)
```

### Logical Operator

We have three types of logical operators: "and", "or", and "not"

#### "and" operator

```python
# If both the conditions are true the result will be true

high_income = False
good_credit = True

if high_income and good_credit:
    print("Eligible")
else:
    print("Not eligible")
```

#### "or" operator

```python
# If one of the many conditions are true the result will be true
high_income = False
good_credit = True

if high_income or good_credit:
    print("Eligible")
else:
    print("Not eligible")
```

#### "not" operator

```python
# It inverses the value of a boolean

student = True
if not student:
    print("Eligible")
else:
    print("Not eligible")
```

### Short Circuit Evaluation

* In python the if statement is evaluated from left to right
* In case of "and" if any argument is false then it stops the evaluation of further arguments
* In case of "or" if any argument is true then it stops the evaluation of further arguments
* In python, logical operators are short circuit

```python
high_income = False
good_credit = True
student = True

# checked for high_income, it's False, so does not evaluate any further
if high_income and good_credit and not student: 
    print("Eligible")
else:
    print("Not eligible")
```

### Chaining Condition Operator

```python
age = 22

# Noraml if condition
if age >= 18 and age <65:
    print("Eligible")

# If condition using chaining
if 18 <= age < 65: # "18 <= age < 65" is called chaining comparison operator
    print("Eligible")
```

## Loops

### "range"

```python
range(4) 
#It will return a sequence from 0 to 3

range(1,4) 
#It will return a sequence from 1 to 3

range(0,4,2) 
#It will return a sequence from 0 to 3 incremented by step of 2
```

### "for" Loop

* We use loops to create repetition&#x20;
* Similar to if statements we need to terminate our for loop with colon(:)

```python
for number in range(1,4):
    print("Attempt",(number), (number) * "." )
```

#### Break Statement

```python
count = 0
for number in range(25):
    print(number)
    if count == 10:
        break
```

#### Nested Loop

for loop within for loop

```python
for x in range(5):
    for y in range(3):
        print("Coordinates", x, y)
```

#### Iterables

* Iterable is an object, which one can iterate over.&#x20;
* Previously we saw range() function, which returns an object which is iterable.

```python
# range as iteranle
for number in range(3):
    print(number)
    
# string as iterable
for character in "String":
    print(character )
    
# list as iterable (note that we can have different dtypes in lists)
for list_item in [1,"string",4,7]:
    print(list_item )
```

### "while" Loop

```python
number = 100
while number > 0:
    print(number)
    number = number/2
```

#### Infinite Loop

It a loop that runs forever We can break an infinite loop using a break statement

```python
# the code will keep on taking input and printing, unless quit is the input

while True:
    command = input(">")
    print("Echo",command)
    if command.lower() == "quit":
        break
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ankit-apdc.gitbook.io/python-1/part-1-basics/control-flow-and-loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
