<jupyter_start><jupyter_text>Booleans and Operators What are operators? Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. There are several types of operators in Python:          Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.        Relational operators: Relational operators are used to compare values. It either returns True or False according to the condition.        Logical operators: Logical operators are the ‘and’, ‘or’ and ‘not’ operators.        Bitwise operators: Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.        Assignment operators: Assignment operators are used in Python to assign values to variables.        Special operators: Python language offers some special type of operators like the identity operator or the membership  operator. They are described below with examples.                    Identity operators: ‘is’ and ‘is not’ are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical            Membership operators: in and not in are the membership operators in Python. They are used to test whether a value or   variable is found in a sequence (string, list, tuple, set and dictionary).            Relational operators between two variables (say ‘a’ and ‘b’) will return True if the relation between ‘a’ and ‘b’ evaluates to true, else False will be returned. The values True and False are called Boolean variables i.e. Booleans are variables which contain truth values. There are many relational operators used in Python, for instance greater (>), equality (=) and so on. Suppose if ‘a’ were 10 and ‘b’ were 20, then a>b will return False because ‘a’ is obviously lesser than ‘b’.<jupyter_code># Declaring a Boolean variable
myBool = True

# Various types of operators

result = 10 + 20    # Arithmetic operators
diff = 10 - 20 * 47 # Arithmetic operators
print(result)
print(diff)

var1 = 2 # Assignment operator
var2 = 56

myBool = var1 <= var2 # Relational operator
print(myBool)

bit = 40
bitop = 40 << 2 # Bitwise Left Shift operator
print(bitop)

a = True
b = False
c = a and b # Logical operator
print(c)<jupyter_output>30
-930
True
160
False<jupyter_text>Conditionals Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.This is where conditional statements are used. Conditional statements deal with conditions – what to do if something is this way? What to do if it’s not? The conditionals offered in Python are ‘if’, ‘else if’ (written as elif) and ‘else’. We start with writing an if statement and then writing the commands (what to execute if the if statement holds true), then we write elif (only if and when required) and write the commands under it, and finally the else statement. Note that indentation is very, very important in Python, because unlike in other languages you don’t have brackets here to contain multiple commands under a single if statement.<jupyter_code>a = 100
b = 200

if a > b:    # If statement
    print(a)
elif a < b:    # Else if statement
    print(b)
else:        # Else statement
    print(a+b)<jupyter_output>200<jupyter_text>Loops In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. We can control the number of times it is being done, and what all tasks are being done. There are multiple loops offered in Python, and the most basic one of them is the ‘while’ loop. Also, we don’t specify our iteration statement in a bracket (as we do in other languages), but instead write ‘while’ and then our statement. We then indent the loop statements below, and once we’re done with the loop statements we continue writing our program un-indented.<jupyter_code>n = 9
i = 0

# While loop
while i<n:
    print(i+1)
    i = i+2<jupyter_output>1
3
5
7
9<jupyter_text>For LoopNext is the ‘for’ loop. In a for loop we can specify the number of iterations by using the range() function. Range(a,b) will return all numbers between a and b (including a, excluding b), with an increment of 1. We can give it the step size (the amount by which the iterator is incremented) in input as well; for instance range(1,10,2) will print the alternate elements between 1 and 10 because step size is 2.There is one important type of statement to know, and that is the loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. There are three types of loop control statements:            break statement: It is used to break out of the loop the moment some condition occurs, and is useful for optimization (the loop doesn’t have to run unnecessarily once some required condition is fulfilled).             continue statement: It causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.            pass statement: The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.<jupyter_code># For loop
for i in range(1,15,5):
    print(i)

# Loop control statements
for i in range(1,20):
    if i == 2:
        continue
    elif i==7:
        break
    print(i)<jupyter_output>1
6
11
1
3
4
5
6<jupyter_text>There is a way to iterate fast and more conveniently over strings, arrays etc. using a ‘for’ loop. Suppose we have a string s, and we wish to iterate over it. We simply write ‘for x in s:’ and continue with our loop statements. Note that we did not have to write the range function or specify the string size; we simply wrote the string name and that is enough for iterating over every element.<jupyter_code># Fast iteration
myStr = 'Coding Ninjas'
for i in myStr:
    print(i)<jupyter_output>C
o
d
i
n
g
 
N
i
n
j
a
s<jupyter_text>Functions You must have come across functions on several occasions. Functions are used for writing blocks of code that can be used over and over again as per the demands of the program. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Functions can be written in various languages and for various purposes. Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. Let's see how they're done in Python.You can define functions to provide the required functionality. Here are simple rules to define a function in Python.Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.The first statement of a function can be an optional statement - the documentation string of the function or docstring.The code block within every function starts with a colon (:) and is indented.The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.<jupyter_code># Functions in Python

def product(x,y): 
    return x*y

X = 20
Y = 4
print(product(X,Y))
def demo(a):
    a += 1 
a = 20
demo(a)
print(a)<jupyter_output>20<jupyter_text>We can provide a function with default arguments so we need not bother giving it all arguments during calls. Let us see an example.<jupyter_code>def result(x=10,y=20):
    a = x + y
    return a

print(result()) # All default arguments will be used to calculate results.
print(result(13)) # 13 is the value we provide for x. Y will have default value 20.
print(result(11,23)) # These are the values which will be used to compute the results.<jupyter_output>30
33
34<jupyter_text>We can also pass any number of arguments we want; unlike in many other languages, we can have variable number of arguments in Python by using the *args argument.<jupyter_code>def show(*args):
    print(args)
    
show(10,20)
show(10,100,2000)<jupyter_output>(10, 20)
(10, 100, 2000)
{'fraction_non_alphanumeric': 0.04928404928404929, 'fraction_numerical': 0.014763014763014764, 'mean_word_length': 4.193083573487032, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '497452', 'license': 'Apache-2.0', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2199, 'n_words': 1365, 'path': 'notes/2 loops and functions/Conditionals and Loops.ipynb', 'repo_name': 'subhendu17620/ML-Coding-Ninjas', 'size': '9009'}