Uncategorized

5
Jul

Generate Passwords

Question:

Write a python program to generate passwords

Program:

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:

4
Jul

Area of Rectangle using classes

Question:

Calculate area of a rectangle using classes

Program:

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:

3
Jul

Even numbers using list comprehension

Question:

Create a list of even numbers between 0 and 10 using list comprehension.

Program:

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:

2
Jul

Prime Numbers within a range

Question :

Write a program to print prime numbers within a range

Program:

Explanation:

A prime number is a number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).

Output:

1
Jul

Difference between range() and xrange()

Question:

Write a program to show the difference between range() and xrange() function

Program:

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:

30
Jun

Tuple Operations

Question:

Perform the following operations on the below tuple (‘abc’, ‘def’, ‘ghi’, ‘jklm’, ‘nopqr’, ‘st’, ‘uv’, ‘wxyz’, ’23’, ‘s98’, ‘123’, ’87’)

  •  prints the length of the tuple

  • Slicing

  • Reverse all items in the tuple

  • Removing whole tuple

  • Concatenate two tuples

Program:

Explanation:

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Output:

29
Jun

Check Validity of Password

Question:

Write a program to check the validity of password input by users.

Accept a sequence of comma separated passwords and check them according to the above criteria. Print the valid passwords

Program:

Explanation:

Following are the criteria for checking the password:

1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
3. At least 1 letter between [A-Z]
4. At least 1 character from [$#@]
5. Minimum length of transaction password: 6
6. Maximum length of transaction password: 12

Output:

28
Jun

Print Inverted Star Pattern

Question:

Write a program to print the following pattern

 

Program:

 

27
Jun

Number Right angle Triangle Pattern

Question:

Print the below pattern

 

Program:

 

26
Jun

Star Pyramid Triangle

Question:

Write a program to print the following pattern

Program:
25
Jun

Remove duplicate number

Question:

With a given list [5,3,5,3,6,8,3,1,9,4,5,9,0,7], write a program to print this list after removing all duplicate values with original order reserved.

Program:

Explanation:

Set() function stores number of values without duplicate.

Output:

 

24
Jun

Swap without third variable

Question :

Swap two variables without using a third temporary variable

Program:

Explanation:
Here num1, num2 acts as a tuple. Since tuples are mutable, the above assignment works perfectly well

Output:

23
Jun

Distance travelled by Robot

Question:
A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
UP 5
DOWN 3
LEFT 3
RIGHT 2
The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.
Example:
If the following tuples are given as input to the program:
UP 5
DOWN 3
LEFT 3
RIGHT 2
Then, the output of the program should be:

2

Program:

Explanation:

The distance formula is derived from the Pythagorean theorem. To find the distance between two points (x1,y1) and (x2,y2), all that you need to do is use the coordinates of these ordered pairs and apply the formula  d=​{(x2x1)2+(y2y1)​2}​.This formula is used to find the distance.

Output:

22
Jun

Remove Duplicate words and Sort the string

Question:
Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.

Program:

Explanation:

The string is accepted to s and split to a list using s.split. the list is then sorted and then joined to form the output string

Output:

 

21
Jun

Print multiples of 7 using yield

Question:
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.Use yield.

Program:

Explanation

The yield statement is used to define generators, replacing the return of a function to provide a result to its caller without destroying local variables. Unlike a function, where on each call it starts with new set of variables, a generator will resume the execution where it was left off.

Output:

20
Jun

Sort Student details

Question:

Write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers.

1: Sort based on name;

2: Then sort based on age;

3: Then sort by score.

The priority is that name > age > score.

We use itemgetter to enable multiple sort keys.

Program:

Explanation:

operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

Output:

19
Jun

Python Program: Frequency of words

Question:
Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.

Program:

Explanation:

The sort() method sorts the elements of a given list in a specific order – Ascending or Descending.

Output:

 

18
Jun

Python Program to Sort string

Question:

Write a program to sort the string accepted from the user

Program:

Explanation:

The sort function returns the sorted data item.

Output:

17
Jun

Web Application Framework Flask

FLASK

Flask is a micro web framework written in Python

What is Web Framework?

Web Application Framework or simply Web Framework represents a collection of libraries and modules that enables a web application developer to write applications without having to bother about low-level details such as protocols, thread management etc.

Installation:

virtualenv which is a virtual Python environment builder, is used to help a user to create multiple Python environments side-by-side. Thereby, it can avoid compatibility issues between the different versions of the libraries.

The following command installs virtualenv

Once installed, a new virtual environment is created in a folder.

So now we have a virtual environment Test created in the FlaskTest folder

We now install Flask in this environment.

Now we have Flask installed in our system and we can import it in our projects.

As we have setup environment to work with Flask library, lets run a sample code to test Flask. Let’s create a python file HelloWorld.py and write the following code:

  • The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function.In the above example, ‘/’ URL is bound with hello_world() function. Hence, when the home page of web server is opened in browser, the output of this function will be displayed.
  • The run() method of Flask class runs the application on the local development server.

All parameters for the run function are optional

host

Hostname to listen on. Defaults to 127.0.0.1 (localhost). You have to set it to ‘0.0.0.0’ to have server available externally

port

Default value is 5000

debug

Default value is false. If it is set to true, we get debug information

options

To be forwarded to underlying Werkzeug server.

Now we can execute the program and see its working. Execute the program using the command

You will get the following output.

Now if you open your browser and type in your localhost address that is, 127.0.0.1 along with the port number 5000

127.0.0.1:5000

You can see HELLO WORLD being printed on your browser.

You can use Ctrl+C to stop running the program.

Now if you need to set a specific URL other than the homepage of localhost, you can specify it in the route() function.

If you change the route function in the above code to:

And run the program again, you can see that opening the homepage of localhost renders no output, but instead if you add the URL specified in route function to the homepage address:

127.0.0.1:5000/hello

Now you can see your program being run…

Benefits of Flask:

  • Extensibility
  • Faster development
  • Easier to read, write and maintain

Have fun working with Flask library

16
Jun

Serverless Architecture: Zappa

Maintaining the backends or servers is always a part of the headache for the programer. And here walk in the solution, serverless Architectures.

What are Serverless architectures?

It refer to applications that significantly depend on third-party services (known as Backend as a Service or “BaaS”) or on custom code that’s run in ephemeral containers (Function as a Service or “FaaS”), the best known vendor host of which currently is AWS Lambda

Using an amazing library called Zappa, it is now easy to deploy in AWS Lambda.

What is Zappa?

Zappa is a system for running “serverless” Python web applications using AWS Lambda and AWS API Gateway. It handles all of the configuration and deployment automatically . Now it is easy to deploy an infinitely scalable application to the cloud with a just single command at the least possible cost often just a small fraction of the cost of a traditional web server.

Installation:

You can install zappa easily using

To install AWS command tools,

Let’s get started

So before we start using Zappa, let’s create a simple hello world website using flask and run it on our locla server

At a directory of your choice, save the following program as Prg.py

And run it on your local server using the following commands at your cmd

Now check out http://127.0.0.1.5000/. You can see a simple webpage like below.

Before we get our hands on some zappa, we need to ensure we have a valid AWS account.

You can create a AWS account by following the steps one by one here.

After creating an account, run the following command to configure your AWS account in your command line and and fill in the access key id, the secret access key and the default region..

Let’s configure Zappa now.

Run the follwing command in your project directory

This creates a file named zappa_settings.json inside our project directory with the following content.

Now deploy zappa using

And it’s done!!Thtat’s all it takes.!Just a single command!

Benefits of Zappa:

  • No more tedious web server configuration!
  • No more paying for 24/7 server uptime!
  • No more worrying about load balancing / scalability!
  • No more worrying about web server security!
  • Scalability since we can have as many responses processed in parallel as we need. Since AWS Lambda handles all of the requests
  • 100 function executions per second,and if you scale beyond that you only need to ask Amazon to raise your limit.
  • Inexpensive since we pay by the millisecond.. Not to mention the cost saving on not having to spend time on deployment, operations and maintenance!
  • Maintainability and Ease of Use
  • Easy to deploy.- deployed in a single command

Challenges of Zappa

  • Integration with the other services of the cloud platform is often said to raise an issue.

To learn more of zappa, which I am sure you would want to, check out these sites: