Question:
Write a python program to generate passwords
Program:
1 2 3 4 5 |
import string from random import * characters = string.ascii_letters + string.punctuation + string.digits password = "".join(choice(characters) for x in range(randint(8, 16))) print (password) |
Explanation:
Following are the criteria for creating a password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$#@]
4. Minimum length of transaction password: 6
5. Maximum length of transaction password: 12
Output:
1 |
-|>GR5UZY3 |
Question:
Calculate area of a rectangle using classes
Program:
1 2 3 4 5 6 7 8 9 10 11 12 |
class rect(): def __init__(self,breadth,length): self.breadth=breadth self.length=length def area(self): return self.breadth*self.length a=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rect(a,b) # Creating an object 'obj' of class rect print "Area of rectangle:",obj.area() |
Explanation:
A class rect is created with two breadth and length, and method area which tabulates the area of the rectangle.
An object obj of class rect is created and used
Output:
1 2 3 |
Enter length of rectangle: 10 Enter breadth of rectangle: 20 Area of rectangle: 200 |
Question:
Create a list of even numbers between 0 and 10 using list comprehension.
Program:
1 2 |
listl=[i for i in range (10) if(i%2==0)] print listl |
Explanation:
List comprehensions provide a concise way to create lists.
It consists of brackets containing an expression followed by a for clause, then
zero or more for or if clauses. The expressions can be anything, meaning you can
put in all kinds of objects in lists.
List comprehensions provide a concise way to create new list of elements that satisfy a given condition from an iterable. Here the iterable prints out even numbers.
Output:
1 |
[0, 2, 4, 6, 8] |
Question :
Write a program to print prime numbers within a range
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#Accept the range from user lower = int(input("Enter lower range: ")) upper = int(input("Enter upper range: ")) print "Prime numbers between",lower,"and",upper,"are:", for num in range(lower,upper + 1): # prime numbers are greater than 1 if num > 1: for i in range(2,num): if (num % i) == 0: break else: print num, |
Explanation:
A prime number is a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
Output:
1 2 3 4 5 |
Enter lower range: 2 Enter upper range: 10 Prime numbers between 2 and 10 are: 2 3 5 7 |
Question:
Write a program to show the difference between range() and xrange() function
Program:
1 2 3 4 5 6 7 |
import sys r=range(10000) x=xrange(10000) print (sys.getsizeof(r)) #output:40036 print (sys.getsizeof(x)) #output:20 #sys.getsizeof() returns the memory size occupied by a variable or object |
Explanation:
The variable holding the range created using range() uses so much memory compared to
the variable created using xrange()
The reason is that range creates a list holding all the values
while xrange creates an object that can iterate over the numbers on demand.
Eventhough the xrange is memory efficient, the price we have to pay
for this efficieny is access time. The range variable created with range()
will have a faster access time compare to the variable created with xrange()
as it is entirely stored in the memory so it is readily available
whereas the variable created with xrange() have to load its contents
to memory ‘on demand’ only. So time efficiency is a drawback of xrange()
Output:
1 2 |
80072 40 |