<jupyter_start><jupyter_text>Python Basics These assignments aim to get you acquainted with Python, which is an important requirement for all the research done at Solarillion Foundation. Apart from teaching you Python, these assignments also aim to make you a better programmer and cultivate better coding practices. Visit these links for more details: PEP8 Practices: https://www.python.org/dev/peps/pep-0008/ Check PEP8: http://pep8online.com Python Reference: https://www.py4e.com/lessons Do use Google efficiently, and refer to StackOverflow for clarifying any programming doubts. If you're still stuck, feel free to ask a TA to help you.Each task in the assignment comprises of at least two cells. There are function definitions wherein you will name the function(s), and write code to solve the problem at hand. You will call the function(s) in the last cell of each task, and check your output.We encourage you to play around and learn as much as possible, and be as creative as you can get. More than anything, have fun doing these assignments. Enjoy!<jupyter_code>print "hello sf"<jupyter_output>hello sf<jupyter_text>Module 1Scope: Conditions, Loops, Exceptions, Data Structures, Lambda Functions  ImportsImport any modules here:<jupyter_code>import time
import math<jupyter_output><empty_output><jupyter_text>Task 1 Print the pattern given in the docstring. **Question 1**<jupyter_code>def slope(n):
    """
        Prints the following pattern for `n` rows:

        1
        1 2
        1 2 3
        ...
        1 2 3 ... n

        Parameters
        ----------
        n : integer
            Number of lines

        Ideas
        -----
        Looping, Nested Loops
    """
    for i in range (n):
        for j in range (i+1):
            print j+1,
        print "\n"
    
slope(5)<jupyter_output>1 

1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5<jupyter_text>**Question 2**<jupyter_code>def interrupt():
    """
        Prints the following pattern forever (till interrupt):

        *
        00
        ***
        0000
        *****
        0000
        ***
        00
        *

        Ideas
        -----
        Looping, Conditions
    """
    # toolbar: kernel->interrupt to stop
    while True:
        for i in range (9):
            if i<5:
                for j in range (i+1):
                    if (i+1)%2==1:
                        print "*",
                    else:
                        print "0",
            else:
                for j in range (9-i):
                    if (i+1)%2==1:
                        print "*",
                    else:
                        print "0",
            print "\n"
            time.sleep(0.5)
            
            
    
                        
# Call your function here

interrupt()<jupyter_output>0 1 2 3 4 5 6 7 8
* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0 

* * * 

0 0 0 0 

* * * * * 

0 0 0 0 

* * * 

0 0 

* 

* 

0 0<jupyter_text>**Question 3**<jupyter_code>def printsine(n):
    """
        Prints the following pattern forever (till interrupt):

        **
        ****
        ******
        ******
        ******
        ****
        **
        
        This is a vertical sinusoid. Use the `math.sin` function here.
        Note that your output need not look exactly like this.
        
        Parameters
        ----------
        n : integer
            Number of lines
        
        Ideas
        -----
        Looping, math.sin
    """
    
    while True:
        pi = 3.14
        for i in range (1,n):
            for j in range (int((math.sin(pi*(i/float(n))))*55)):
                print "*",
            print "\n"
        

# Call your function here
printsine(15)<jupyter_output>* * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

* * * * * [...]<jupyter_text>Task 2 Learn about data structures, exception handling and lambda functions. **Question 1**<jupyter_code>dictionary = {'ant': 2, 'dog': 12, 'duck': 20, 'hen': 11, 'other': 99}
keys = ['ant', 'cat', 'duck', 'hen', 'lion', 'zebra']
def printdictionary(dic,ks):
    """
        For all elements in `keys`, print the value associated with them in `dictionary`.
        Use exception handling to take care of a non-existent key, and print the value 
        associated with `other` instead. Use exception handling.

        Ideas
        -----
        try, except
    """
    for i in range (len(ks)):
        try:
            print ks[i], ":" ,dic[ks[i]]
        except:
            print "no value", " \tother value: ", dic["other"]
    
    
# Call your function here
printdictionary(dictionary,keys)<jupyter_output>ant : 2
cat : no value  	other value:  99
duck : 20
hen : 11
lion : no value  	other value:  99
zebra : no value  	other value:  99<jupyter_text>**Question 2**<jupyter_code>data = [0.00, 0.12, 0.24, 0.36, 0.48, 0.52, 0.65, 0.50, 0.11, 0.09]
def t1f0(x):
    if x==True:
        return 1
    else :
        return 0

def roundmean(mydata):
    """
        Round off values in `data` below the mean to 0, and those above the mean to 1
        Do not use looping statements.

        Ideas
        -----
        lambda, map
    """
    sum=0
    sum = reduce((lambda x,y: x +y ), data)
    mean = sum/len(mydata)
    roundlist = map((lambda x: x > mean ), data)
    roundlist = map(t1f0, roundlist)
    print roundlist
# Call your function here
roundmean(data)<jupyter_output>[0, 0, 0, 1, 1, 1, 1, 1, 0, 0]<jupyter_text>**Question 3**<jupyter_code>def perfectsquares(n):
    """
        Return a list of all perfect squares less than `n`, using list comprehension.

        Parameters
        ----------
        n : integer
            Limit value

        Ideas
        -----
        Looping, If, List Comprehension
    """
    mylist = [x**2  for x in range (1,n)  if x**2 < n ]
    print mylist
# Call your function name
perfectsquares(1000)<jupyter_output>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961]
{'fraction_non_alphanumeric': 0.16917969290358278, 'fraction_numerical': 0.04398948678932079, 'mean_word_length': 1.6025917926565876, 'pattern_counts': {'":': 1, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 71, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '618303', 'license': 'MIT', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2500, 'n_words': 711, 'path': 'Module1.ipynb', 'repo_name': 'SachinKrishan/PythonBasics', 'size': '7229'}