 # Learn Python Through Anime: Part-2

This is part 2 of Python Tutorials, check part 1 before proceeding further. Part1: Learn Python Through Anime.

Control statements execute the statement on certain conditions and these conditions are either True or False. If a condition is True, a set of statements are executed, or else another set of statements. Sometimes you even need to repeat some statements continuously.

Since we will be dealing with True and False in order to carry out control statements, there are some concepts to be cleared. We know different types of mathematical operators but there are different types of operators as well: Relational And Logical Operators.

### Relational Operators

Relational Operators are operators which are used to compare between two values to test the condition. The return type for relational operators is True or False.

Here is the set for Relational operators:

``````Example [a = 10, b=13]
a>b   #False Greater than
a>=b  #False Greater than or equal to
a<b   #True Less Than
a<=b  #True Less than or equal to
a==b  #False Equals to
a!=b  #True Not Equals to``````

Note: ‘=’ and ‘==’ are totally different.  ‘=’ is used to assign value to a variable, whereas ‘==’ is used to compare two variables.

### Logical Operators

Logical Operators are the operators which check the multiple boolean expression and logically return a boolean type based on the conditions provided. We already know how to test one condition using a relational operator but here we take multiple conditions and based on Boolean logic we return either True or False.

There is three types of Logical Operands

#### AND Operators

In the AND operator, all the conditions should be True to return True. Even if one condition is False the final result will return False.

```Conditions     : Result

False and False: False
False and True : False
True and False : False
True and True  : True```
``````>>>10>5 and 45<100
True
>>>10==11 and 10!=10 #both the conditions are False
False
>>>100 == 100 and 18<56 and 45!=50
True``````

Even if one condition is False among the multiple conditions then it results as False. AND operator works like MULTIPLICATION between conditions. We know that False = 0 and True =1, since AND is said to be multiplication. When one condition is False i.e., 0 no matter how many conditions are True(1) multiplying it with 0(False) will always be 0(False). If all conditions are True(1), multiplying with 1 will return 1(True).

#### OR Operator

In the OR operator, all the conditions should be False to return False. Even if one condition is True the final result will return to True.

```Conditions     : Result

False and False: False
False and True : True
True and False : True
True and True  : True```

e.g., Refer to the above table to understand OR operator

``````>>>10==11 or 10!=10 #both the conditions are False
False
>>>100 == 100 or 18>56 or 45!=50
True
>>>100>100 or 15==25
False``````

Even if one condition is True among the multiple conditions then it results as True. OR operator works like an Addition between conditions. Since OR is said to be an Addition. When one condition is False(0), it only becomes False when it is added with another 0(False). If the other condition is True(1), 0 gets added with 1 and returns 1(True).

#### Not Operator

Not operator is the compliment of the condition i.e. if the condition is True the output will be False but if the condition is False the output will be True.

```Conditions: Result
Not True  : False
Not False : True```
``````>>>a = 10
>>>b = 5
>>>not a==b
False>>>not a!= b
True``````

## Importance of None

By the basic meaning in English, None means nothing or empty. In python, there are many times when you need to provide an empty value for a variable.

``````>>>classroom_of_elite = None
>>>print(classroom_of_elite)
None
>>>type(classroom_of_elite)
<class 'NoneType'
>>>>second_season = print(‘Not releasing’)
>>>print(second_season)
None``````

## If statement

Syntax:

```if(condition):
statements
print(‘Outside if block’)```
```name = input("Enter your name:")
if name=="Luffy":
#this will print only when the name is Luffy
print("Its the Pirate King")
print("Welcome")

output:
Welcome

Its the Pirate King
Welcome```

If the condition is True the block statements inside the if condition will be executed. If the condition is False the flow doesn’t enter if block, rather neglects it. Block here means a set of statements. Every block starts with some indentation. Notice closely the first statement in the block is placed after 4 spaces from the if statement. If you don’t indent you will end up in SyntaxError but no need to worry, as your text editor or PyCharm will automatically indent this space to you. If the statement is not inside the block or if it is not indented this means that it does not belong to if statement, in above example print(‘Welcome’) will print even if the condition is False. But if the condition is True then the statements inside the if block and outside block both will be executed.

More if examples

```episodes = int(input("Enter your favourite anime total episode:"))
if episodes == 12:
print("Well you seem to like slice of anime")
if episodes == 720:
if episodes>900:
print("You must be One Piece Fan")

Output:
Enter your favorite anime total episode:1000
You must be One Piece Fan```

## If else

Syntax:

```if(condition):
print('Inside if')
else:
print('Inside else, in case if condition is false')
print('This will print nonetheless')```
```name = input("Enter your name:")
if name=="Luffy":
print("Its the Pirate King")
else:
print("Not a Pirate King")
print("Welcome to Grand Line")```

As you have already seen in if that for having the true condition, the set of statements are performed that are indented within the if block, but if you need to display a different message for False condition, what will you do. Once the interpreter checks the if conditions if it’s False it directly jumps out of the if block. In order to print the False statement, you can use the else block. To understand in simple words, in if-else only one will be executed, either if or else.

Else statement is used to handle the false condition for if, i.e., If True do this, else do this logic. At first, the compiler checks the if block, if the condition is True the if block will be executed, and else block, will be neglected but if the if condition is False the else block will be executed, and if block will be neglected.

More examples of if-else statements

```#program to pint even or odd number.
number = int(input(" Enter a number:"))
if number%2==0:
print(“Even number”)
else:
print(“Odd number”)```

Now then you should be familiar with the modulus operator by now, To revise up modulus operator returns the remainder. In even and odd logic, the even number is the reminder of 0 if the given number is divided by 2. Thus using the if statement you can perform an even and odd program. If the number is divisible by 2 if block will be executed and Even number will be printed and if it is not divisible by two else block will be executed.

## Elif

Syntax:

```if condition:
print(‘if true’)
elif condition:
print(‘if False, else if True’)
else:
print(‘Both elif and I False, then come in else’)```

Example

```score = int(input(“Enter your marks scored in Anime Quiz (out of 10):"))
if score>=8:
print(“Great”)
elif score=>5 and score<8:
else:
print(“You should watch anime”)

output:
Enter your marks scored in Anime Quiz (out of 10):9
Great```
```#check if the given number is 0 or positive or negative
number = int(input(“Enter a number: "))
if number>0:
print(“Positive number”)
elif number==0:
print(“Number is zero”)
else:
print(“Negative number”)

output:
Enter a number: 8
Positive number
Enter a number: 0
Number is zero
Enter a number:-6
Negative number```

The explanation for elif: Till now we have only seen conditions, where if is executed for True conditions and else, is executed for False conditions. But all the time this will not work. You need to check more conditions, take an example of your college/school grade, where there are different categories like Destination, First Class, Second Class, and Fail. So using the if-else condition could become complicated, you can do it by if-else but to overcome the complexity there is the concept of elif which is known as else if. Now while executing the program if the if condition is False, it will check elif (else if) and if all the conditions are False, else is executed. See the difference in if-else and elif conditions.

## Loop In Python

### while loop

```initial_value = int_value
while condition:
set_of_statements
initial_value increment/decrement```

Before looking into the while loop example lets understand what’s this increment and decrement actually means.

### Short hand operators

So far we got to know mathematical operators and also relational and logical. But we have never mentioned the shorthand operator. Just think of shorthand as a shortcut. Shortcut operators come in handy when we have to perform operations on the same variable. Now, what is this same variable? Now consider you have a variable a with value of 10. You need to add 10 on the same variable that is a. Now, this is easy to do right?

``````>>>a = 10
>>>a = a+10
>>>a
20
#using short hand operator
>>>a=10
>>>a+=10
>>>a
20``````

See this was simple, we can use an arithmetic operator before the assignment operator, this is what we called a shorthand operator. You can use + – * / % ** as shorthand operator. In shorthand operator we are assigning back the value in the same variable thus it updates the new value into the variable

Here are some more examples:

``````>>>b=21
>>>b%=3  #reminder of 21%3 is zero
>>>b
0
>>>b*=10  #we are multiplying b with 10, i.e., 0*10 and not 21*10
>>>b
0
>>>a = 25
>>>b = 15
>>>a//=5+b**6%10
>>>b%=a+81//9   #keep in mind here in this line a is not 25
>>>a
2
>>>b
4``````

### Increment and Decrement

Increment means an increase in the value, and decrement means a decrease in a value. Check out these examples we will be using this in the while loop.

```>>>episode = 12
>>>episode+=1 #increment by 1
>>>episode
13
>>>characters = 23
>>>characters-=1 #decrement by 1
>>>characters
22```

Most of the time we will only increment or decrement by 1 but you can also use other values based on the problem.

Example: while loop

``````start = 1 #start i.e., 1 is the initial value
while start<=5:
start+=1  #increment the initial value

output:
``````start = 8
while start>=0:
print(start)
start-=1  #decrement the initial value

output:
8
7
6
5
4
3
2
1
0``````

## How does while works?

At first, we have to initialize a start value. Then use while and give what condition you want, you can either use start value 1 and print till ‘n’ number you want or enter ‘n’ value and decrement till 0. For more example, have you ever noticed in the login details if you enter the wrong name or password the system keeps asking you the correct name, this is one of the while loop specialty and also in bank machines it keeps repeating the process unless to enter a correct pin, but you can set a limit here? In login details, you can set an unlimited number of tries and in the bank pin program, you can set the limit. Now this is confusing, give me code so that I can understand, then there you go: Understand while loop properly with these set of examples:

``````#Check valid name

valid = True
while valid:
user = input("Enter your name: ").lower()   #we will learn about lower next
if user!="killua":  #the code will run endlessly if the user is not killua
print("Wrong name entry")
else:
print("Welcome Killua")
valid=False

output
Wrong name entry
Wrong name entry
Welcome Killua``````

### For Loop

```Syntax:

for variable in range(start_value,final_value,step_value)
set_of_statements

#step_value is 1 by default
#start_value is 0 by default```

in is the keyword which checks the variable is in range or not. If a variable is in range it keeps running, and if the variable is not of range then the program is terminated.

Example

```for i in range(7):
print(i)

0
1
2
3
4
5
6```
```for i in range(2,10,2):
print(i)

2
4
6
8```

For and while are the same but have different syntax. For you have step value as the third parameter inside the range(), whereas in while you can increment the start value to 2 instead of 1, to get 2 multiples.

If you need to give a step value then it should be given as a third parameter, If you enter just 2 values inside the range(), it will treat as start and stop and the step value is set as 1 by default. In the first example, you have given just 10 inside the range(), which means the start is 0 and the step is 1 by default. Now, why is 10 neglected, in range when you apply any value say range(5), that means it starts from 0 and ends at n-1, where n is 5. So the range(5) is equal to 0,1,2,3,4. In short, excluding the last value. i.e., n or 10 in the first example.

Note: range(n) starts from 0 and ends at n-1. As the name goes range

More examples

```for i in range(1,5):  #starts at 1 and ends at n-1(5-1 = 4)
print(i)
1
2
3
4```
```for i in range(10,2): #you will get no output
print(i)```
```for i in range(10,2,-1):  #-1 means go reverse from 10 to 3, 2 is excluded
print(i)

10
9
8
7
6
5
4
3```

Note: while printing reverse start>end. While printing the correct order, end>start. In reverse start to n+1 and the correct order start to n-1.

## Infinite Loop

Infinite means the thing you can’t count on, and an infinite loop is something that runs endlessly and never stops. It keeps running. Now when does this infinite loop occurs, when the condition is True, and when there is no way to make that condition False? e.g.,

```while True:
print(“Peoples Dream Never End”)  #this line has no end```

Since the condition is True and there is no way to prove it False thus this is an infinite loop, that has no end. To come out of the infinite loop, press Ctrl+C  or exit the window, peace. There are many ways where you can break the infinite loop, as you have seen in the example to check valid users, it’s an endless loop until the correct name is entered. Infinite Loop is very necessary, but also you should know how to break it. Now let’s get into that part as well.

## Break, Continue and Pass

We have finally reached the end of this chapter, and it’s the perfect time to introduce these 3 concepts. Break, continue and pass are very important to understand before we advance further.

### Break

The break is used to terminate the program. The break is one best way to break an infinite loop. Break only works inside the loop, i.e., you can’t use break outside the loop. Now when you are checking a valid name or valid password, you can use a break and exit the program.

Let’s use a break and check out how it works in detail. Consider the same example used in the while loop i.e., to check the valid name.

```while True:
user = input("Enter your name: ").lower()
if user!="killua":  #the code will run endlessly if the user is not killua
print("Wrong name entry"
else:
print("Welcome Killua")
break```

The break is just used to exit or terminate the program if the user is valid. In the first example, we used a flag variable as True and then updated it to False. This method was basically adopted before because we know that the loop will be endlessly running if the condition is True. We need an infinite loop in this kind of problem but we should also know How to counter it? In order to counter the infinite loop a Boolean variable value was used If the valid user is found, you can make the condition False.

```>>> for boring_anime in range(1,7):
...     if boring_anime>2:
...         break
...     print("Boring Anime Episode",boring_anime)
...
Boring Anime Episode 1
Boring Anime Episode 2```

It seems we have watched 2 episodes of boring anime and just gave a break to it. What is the boring anime you have watched? let me know in the comments. Because I haven’t watched any boring anime.

### Continue

Continue doesn’t terminate the loop, rather it keeps running. This is kind of endless, but it helps us to skip some information based on conditions. So in basic knowledge continue is used to skip data based on condition or else keep running.

```>>> for period in range(1,7):
...  if period==3:
...      continue
...  print("Watching",period,"episodes")

Output:
Watching 1 episodes
Watching 2 episodes
Watching 4 episodes
Watching 5 episodes
Watching 6 episodes
```

So now there is a repeated episode on episode 3 so I don’t want to watch it. Continue skips the information of watching the third episode but executes the rest of the sentence. This is what continue is used for.

```limit = 5
while limit>0:
pin = int(input("Enter OTP:"))
if pin!=1234:   #continue again if the pin mismatch
continue
if limit==0:  #the code will run endlessly if the pin is not 1234
print("You reached the limit")
break
else:
print("Welcome")
break   #or limit= -1 to make the while loop False```

Continue prompts back the user to enter the pin if the pin enter is wrong. The program will run endlessly unless the user enters the correct pin or reach out the limit. You can either use break or limit=-1 to terminate the program.

Note: Both continue and break are to be used only inside the loop.

### Pass

Pass simply means to let through. It can be used anywhere, unlike continue and break which should work inside of loops only. Pass is very important. t when there are functions necessary just to pass through. As you proceed further you will understand the usage of pass.

```while True:
pass```

This code prints nothing but is an infinite loop. Pass does nothing it just allows you ahead. As mentioned Pass can be used even outside the loop.

```character = “Sakata Gintoki”
if character==”Sakata Gintoki”:
pass```

## Extra Pro Tip:

```Syntax to use if-else in one line
variable_name = True if condition else False```
``````gender = input(“Enter your gender:").lower()
phrase = “He/Him” if gender==”male” else “She/Her”``````

Here we need to use he or she, on what gender user enters. So use the phrase as variable and if gender is male then use He else she. Well gender doesn’t matter, does it? I kinda like Haku tho.

### Neglecting variable names

```for _ in range(3):
print("Luffy will become the pirate King")```

Part 2 ends here. Check part 1: https://animevyuh.org/learn-python if you haven’t.

Filler Article Just Like Filler Anime Episode: https://animevyuh.org/import-in-python/

If you like our content, support us by Buying a Coffee.