<repo_name>Rev4nth/python_learns<file_sep>/ex-11.py
# Ex-13
# Accomplish the following using string slicing
# Print excluding first 2 characters
# Print first 4 characters
# Print every alternate letter
# Reverse the string
animal = 'elephant'

print animal[2:]
print animal[0:3]
print animal[::2]
print animal[::-1]
<file_sep>/ex-10.py
# Ex-12
# Change the last character from 'g' to 't'
# After chaning, pet should contain 'dot'.
pet = 'dog'
print pet
pet = pet[0:-1] + "t"
print pet
<file_sep>/ex-7.py
# Ex-8
# Read about the module random, it has method randint().
# https://docs.python.org/2/library/random.html
# Create a list with 100 random integers.
# Find out the frequency (how many times each integers is repeated in the list)
# Output should be a dictionary key as the random integer and value is the frequency
# Eg: random_integers_list = [1,2,3,4,1,3,6]
# outputs: frequencies = {1:2, 2:1, 3:2, 4: 1, 6: 1}

from random import randint
list = []
frequencies = {}
for num in range (0,100):
    list.append(randint(0,99))
for keynum in list:
    count = 0
    for num in list:
        if keynum == num:
            count = count + 1
    frequencies[keynum] = count
print list
print frequencies
<file_sep>/ex-8.py
# Ex-10
# Write a function isPrime, that takes a number as argument.
# Returns true if it is a prime number, false if it is not.
# Eg: is_prime(20) -> False, is_prime(13) -> True

def is_prime(num):
    count = 0
    for divisors in range(1, num/2):
        if num % divisors == 0:
            count = count + 1
    if count == 1:
        print "%d is a prime" %(num)
    else:
        print "%d is not a prime" %(num)

is_prime(11)
is_prime(19)
<file_sep>/ex-1.py
# Ex-1 Spot the error


    if 2>3:
      print "2 is greater than 3"
    else:
      print "2 is not greater than 3"
<file_sep>/ex-9.py
# Ex-11
# Mutable vs Immutable data types
# Follow this video https://www.youtube.com/watch?v=5qQQ3yzbKp8
# Label the following datatypes whether they are mutable or immutable
# String
# List
# Dictionary
# Tuple

# String and tuple is immutable
# List and Dictionary is mutable

def nthprime(num):
    nth = 2
    eachNum = 2
    while nth != num:
        count = 0
        print eachNum
        for divisors in range(1,eachNum):
            if num % divisors == 0:
                count = count + 1
        print eachNum
        if count == 1:
            nth = nth+1
    print eachNum

nthprime(4)
<file_sep>/ex-13.py
# Ex-15
# Use urllib2 module to get html content from this link:
# http://www.mattmakai.com/links-learning-web-fundamentals.html
# Python has beautifulsoup library to parse html.
# http://www.crummy.com/software/BeautifulSoup/bs4/doc/
# Print all anchor tags, exclude relative links

import urllib2
from bs4 import BeautifulSoup
response = urllib2.urlopen('http://www.mattmakai.com/links-learning-web-fundamentals.html')
html = response.read()
soup = BeautifulSoup(html)
links = soup.find_all('a')
for eachLink in links:
    print eachLink
<file_sep>/ex-4.py
# Ex-5
# Extract filenames that has txt as extension
import re
filenames = ['file1.txt', 'file2.txt', 'file3.pdf', 'file4.pdf']
filenew = []
for filename in filenames:
    match = re.search(r'.txt',filename)
    if match:
        filenew.append(filename)
print filenew
<file_sep>/ex-12.py
# Ex-12
# Write a class Prime.
# It supports the following operations:
# Check out this article to find primes in an optimized way.
# http://stackoverflow.com/a/5813926/2131120


class Prime(object):
    def nthprime(nth):
        primelist = []
        while len(primelist) != nth:
            count = 0
            num = 2
            for divisors in range(1, num):
                if num % divisors == 0:
                    count = count+1
            if count == 0:
                primelist.append(num)
            num = num + 1
        print primelist[nth]
    nthprime(6)
    # def nextprime(num):
    #
    #
    # def previousprime(num):
    #
    #
    # def getallprimes(num):


# p = Prime()
# p.nthprime(100) # prints 100th prime
# p.nextprime(2) # 3. It should return 2 for all numbers <= 1
# p.previousprime(3) # 2. It should return 2 for all numbers <=2
# p.getallprimes(10) # 2, 3, 5, 7
<file_sep>/ex-5.py
# Ex-6
# Read about the module random, it has method randint().
# https://docs.python.org/2/library/random.html
# Create a list with 100 random integers.

from random import randint
for randomNumber in range(1,100):
    print 'Random number %d  is %d' %(randomNumber, randint(200,300))
<file_sep>/ex-6.py
# Ex-7
# Write a function which takes a number as argument.
# Print all odd numbers till that number.



def print_odd_numbers(nums):
    for num in range(1, nums):
        if num%2 == 1:
            print num
        
print_odd_numbers(20)
# Output:
#1
#3
#5
#7
#9
#11
#13
#15
#17
#19
<file_sep>/ex-3.py
# Ex-3
# Write a function print_twice.
# Take a number as input from user and print twice the number
# eg: print_twice(2)
# Output: 4

def print_twice(num):
    num = input("Enter a number to make it twice! \n")
    print num+num
def main():
    print_twice(2)
if __name__ == '__main__':
    main()
<file_sep>/README.md
Clone this repository. Do not push your solutions to this repository.
