<jupyter_start><jupyter_text>prepared by <NAME>                This cell contains some macros. If there is a problem with displaying mathematical formulas, please run this cell to load these macros.    $ \newcommand{\bra}[1]{\langle 1|} $$ \newcommand{\ket}[1]{|1\rangle} $$ \newcommand{\braket}[2]{\langle 1|2\rangle} $$ \newcommand{\inner}[2]{\langle 1,2\rangle} $$ \newcommand{\biginner}[2]{\left\langle 1,2\right\rangle} $$ \newcommand{\mymatrix}[2]{\left( \begin{array}{1} 2\end{array} \right)} $$ \newcommand{\myvector}[1]{\mymatrix{c}{1}} $$ \newcommand{\myrvector}[1]{\mymatrix{r}{1}} $$ \newcommand{\mypar}[1]{\left( 1 \right)} $$ \newcommand{\mybigpar}[1]{ \Big( 1 \Big)} $$ \newcommand{\sqrttwo}{\frac{1}{\sqrt{2}}} $$ \newcommand{\dsqrttwo}{\dfrac{1}{\sqrt{2}}} $$ \newcommand{\onehalf}{\frac{1}{2}} $$ \newcommand{\donehalf}{\dfrac{1}{2}} $$ \newcommand{\hadamard}{ \mymatrix{rr}{ \sqrttwo & \sqrttwo \\ \sqrttwo & -\sqrttwo }} $$ \newcommand{\vzero}{\myvector{1\\0}} $$ \newcommand{\vone}{\myvector{0\\1}} $$ \newcommand{\vhadamardzero}{\myvector{ \sqrttwo \\  \sqrttwo } } $$ \newcommand{\vhadamardone}{ \myrvector{ \sqrttwo \\ -\sqrttwo } } $$ \newcommand{\myarray}[2]{ \begin{array}{1}2\end{array}} $$ \newcommand{\X}{ \mymatrix{cc}{0 & 1 \\ 1 & 0}  } $$ \newcommand{\Z}{ \mymatrix{rr}{1 & 0 \\ 0 & -1}  } $$ \newcommand{\Htwo}{ \mymatrix{rrrr}{ \frac{1}{2} & \frac{1}{2} & \frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & \frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} \\ \frac{1}{2} & -\frac{1}{2} & -\frac{1}{2} & \frac{1}{2} } } $$ \newcommand{\CNOT}{ \mymatrix{cccc}{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0} } $$ \newcommand{\norm}[1]{ \left\lVert 1 \right\rVert } $  Basics of Python: Conditionals We review using conditionals in Python here. Please run each cell and check the results.<jupyter_code># let's randomly pick a number between 0 and 9, and print its value if it is greater than 5

from random import randrange
r = randrange(10)

if r > 5: print(r) # when the condition (r > 5) is valid/true, the code (print(r)) will be executed
# you may need to execute your code more than once to see an outcome
# repeat the same task four times, and also print the value of iteration variable (i) 

for i in range(4):
    r = randrange(10) # this code belongs to for-loop, and so it is indented
    if r > 5: # this code also belongs to for-loop, and so it is indented as well
        print("i =",i,"r =",r) # this code belongs to if-statement, and so it is indented with respect to if-statement

# if you are unlucky (with probabability less than 13/100), you may not see any outcome after a single run
# do the same task 100 times, and find the percentage of successful iterations (attempts)
# an iteration (attempt) is successful if the randomly picked number is greater than 5
# the expected percentage is 40, because, out of 10 numbers, there are 4 numbers greater than 5 
#     but the experimental results differ

success = 0
for i in range(100):
    r = randrange(10)
    if r > 5: 
        success = success + 1
print(success,"%")

# each experiment most probably will give different percentage value
# let's randomly pick a number between 0 and 9, and print whether it is less than 6 or not
# we use two conditionals here

r = randrange(10)
print("the picked number is ",r)

if r < 6:
    print("it is less than 6")
if r >= 6:
    print("it is greater than or equal to 6")
# let's write the same algorithm by using if-else structure

r = randrange(10)
print("the picked number is ",r)

if r < 6:
    print("it is less than 6")
else: # if the above condition (r<6) is False
    print("it is greater than or equal to 6")<jupyter_output><empty_output><jupyter_text>Task 1 Randomly pick a number between 10 and 50, and say whether it is even or not.Remember the reminder/mod operator (%). A number is even if is exactly divided by 2. That is, the reminder is zero when divided by 2 over integers.If the number is odd, then the reminder will be 1 when divided by 2 over integers.<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output><jupyter_text>click for our solution<jupyter_code>#
# when there are many related conditionals, we can use if-elif-else structure
#

# let's randomly pick an even number between 1 and 99
# then determine whether it is less than 25, between 25 and 50, between 51 and 75, or greater than 75.

r = randrange(2,100,2) # randonmly pick a number in range {2,4,6,...,98}, which satisfies our condition
# let's print this range to verify our claim
print(list(range(2,100,2)))

print() # print an empty line
print("the picked number is",r)

if r < 25:
    print("it is less than 25")
elif r<=50: # if the above condition is False and the condition here is True
    print("it is between 25 and 50")
elif r<=75: # if both conditions above are False and the condition here is True
    print("it is between 51 and 75")
else: # if none of the above conditions is True
    print("it is greater than 75")<jupyter_output><empty_output><jupyter_text>Task 2 Randomly pick a number between 0 and 99. With half probability $\left( 0.5 = \dfrac{1}{2} \right)$, it is expected to be between 0 and 49 or between 50 and 99."Let's try to calculate this probability experimentally".N=100 times randomly pick a number between 0 and 99, calculate the frequencies of both cases, and then divide each by 100. Repeat the same experiment for N=1,000, N=10,000, and N=100,000.Experimental results should get closer to the ideal ratio when N increases.<jupyter_code>#
# your solution is here
#<jupyter_output><empty_output><jupyter_text>click for our solution  Prime numbersA number is prime if it greater than 1 and it is divided by only 1 and itself.For example, any negative number, 0, or 1 is not prime.2, 3, 5, 7, and 11 are the first five prime numbers. 31 is another prime number because it cannot be divided by any natural number other than 1 and 31. &nbsp;&nbsp;&nbsp;&nbsp;In other words, any number in {2,3,4,...,30} does not divide 31. 4 is not a prime number because it is divided by 2. The reminder is 0 when divided by 2.9 is not a prime number because it is divided by 3. The reminder is 0 when divided by 3.15 is not a prime number because it is divided by 3 or 5. The reminder is 0 when divided by 3 or 5.<jupyter_code># let's determine whether a randomly picked number between -10 and 100 is prime or not.
# this is a good example for using more than one conditional in different parts of the program
# this is also an example for "break" command, which terminates any loop immediately

r = randrange(-10,101) # pick a number between -10 and 100
print(r) # print is value

if r < 2: print("it is NOT a prime number") # this is by definition
elif r == 2: print("it is a PRIME number") # we already know this
else:
    prime=True # we assume that r is prime, and try to falsify this assumption by looking for a divisor in the following loop
    
    for i in range(2,r): # check all integers between 2 and r-1
        if r % i ==0: # if i divides r without any reminder (or reminder is zero), then r is not a prime number
            print("it is NOT a prime number")
            prime=False # our assumption is falsifed
            break # TERMINATE the iteration immediately
        # we are out of if-scope
    # we are out of for-loop-scope
    if prime == True: # if our assumption is still True (if it was not falsified inside for-loop)
        print("it is a PRIME number")<jupyter_output><empty_output><jupyter_text>Writing a function in Python Our code determines whether a given integer is prime or not.We can store it and use it later.We can write it as a function (module), and then call it whenever it is needed.<jupyter_code># this is an example to write a function
# our function will return a Boolean value True or False

def prime(number): # our function takes one parameter (has one argument)
    if number < 2: return False # once return command is executed, we exit the function
    if number == 2: return True # because of return command, we can use "if" instead of "elif"
    if number % 2 == 0: return False # any even number greater than 2 is not prime, because it is divisible by 2
    for i in range(3,number,2): # we can skip even integers
        if number % i == 0: return False # once we find a divisor of the number, we return False and exit the function
    return True # the number has passed all checks until now
    # by using "return" command appropriately, the programs can be shortened
    #     remark that this might not be a good choice everytime for readibility of codes 

# let's test our program by printing all prime numbers between -10 and 30
for i in range(-10,30):
    # we pass i to the function prime
    if prime(i): # the function prime(i) returns True or False
        print(i) # this code will be executed if i is prime, i.e., prime(i) returns True<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11244979919678715, 'fraction_numerical': 0.03536367692994199, 'mean_word_length': 3.7283755274261603, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 31, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '106876', 'license': 'MIT', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2628, 'n_words': 1270, 'path': 'python/Python16_Basics_Conditionals.ipynb', 'repo_name': 'HasanIjaz-HB/Quantum-Computing', 'size': '8976'}