FAQ on PHP & Python
- What is called suite in PYTHON?
A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on subsequent lines. Only the latter form of suite can contain nested compound statemetns.For instance, the statementif x<y<z: print x; print y; print z;will print the values of x, y and z respectively if their values are in ascending order.The following is an example for a suite that contains nested compound statement:if a>b :if a>c:print a;else:print c;else:if b>c :print b;else:print c;This will display the greatest value among three – a, b and c. - List down the various Built-in Data-types in Python.
Python has many built-in (native) data types. Here are the important ones among them:
- Booleans
- Numbers
- Strings
- Bytes and byte arrays
- Lists
- Tuples
- Sets and
- Dictionaries
- Give the functionalities of the following Sequence Type Operator?
i) seq[ind1:ind2]
ii) obj
in seq
Colon
(:) Operator:
This
operator is applied on sequence data types such as List, String or
Tuple to extract a list of items from them. In Python, the
colon : allows the square brackets to take as many as two numbers.
For any sequence which only uses numeric indexes, this will return
the portion which is between the specified indexes. This
is known as 'slicing', and the result of slicing a string is often
called a 'substring'.
The
following code written in Python illustrates this:
'Hello,
World!'[3:9]
will
return 'lo, wo'.
string
= 'Hello, World!';
print
string[:5];
results in the string 'Hello'. And
print
string[-6:-1];
will
return the string 'World'
in
Operator:
The in
operator is used to iterate through all the items in a sequence of
items.
- List down the built in functions in Python.
Python
documentation lists 80 built-in functions at:
http://docs.python.org/library/functions.html
Some
of the most widely used built-in functions in Python are as follows:
- Math functions: abs(x), round(x, n)
- Type conversion functions: bool(x), float(x), int(x), long(x), str(x)
- Input functions: raw_input(x), input(x)
- Miscellaneous: len(x), id(x)
5. How
do you create a functions in PYTHON? And write a Python program to print Fibonacci Series.
The
keyword def
introduces a function definition.
It must be followed by the function name and the parenthesized list
of formal parameters. The statements that form the body of the
function start at the next line, indented by a tab stop.
The first
statement of the function body can optionally be a string literal;
this string literal is the function's documentation string, or
docstring.
There are tools which use docstrings to automatically produce printed
documentation, or to let the user interactively browse through code;
it's good practice to include docstrings in code that you write, so
try to make a habit of it.
The
following is a script written using Python for
creating a function that
writes the Fibonacci series to an arbitrary boundary:
>>> def fib(n): # write Fibonacci series up to n ... "Print a Fibonacci series up to n" ... a, b = 0, 1 ... while b < n: ... print b, ... a, b = b, a+b ... >>> # Now call the function we just defined: ... fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
Big Questions:
1.
A) How will you PHP to access data from MySQL? (16)
(OR)
B)
Write a python program to create and use an account object with two
functions. (16)
2.
A) Write notes on the following:
i) Create
and Assign Lists
ii) Access
Values in Lists
iii) Update
Lists
iv) Remove
List Elements and Lists (16)
(OR)
B-1)Define
a procedure
histogram()
that takes a list of integers and prints a histogram to the screen.
For example, the input 4,
9, 7
should print the following:
****
*********
******* (8)
*********
******* (8)
B-2)
Write a simple program in python for finding the greatest ans
smallest of n numbers. (8)
3.
A) Write a program in Python to simulate a simple calculator
(8)
(OR)
B) With a neat
sketch say how the dictionaries are compared in PYTHON? (8)