1. What is the role of an
interpreter? Give a detailed note on python interpreter and interactive mode of
operation.
An interpreter is a computer program that executes instructions
written in a programming language. It can either
·
execute the source code directly or
·
translates the source code in a first
step into a more efficient representation and executes this code
Python interpreter and interactive
mode
With the Python interactive interpreter it is easy to check
Python commands. The Python interpreter can be invoked by typing the command
"python" without any parameter followed by the "return" key
at the shell prompt:
$ python
>>>
Once the Python interpreter is started, you can issue any
command at the command prompt ">>>".
For example,let us print the "Hello World" statement:
>>>
print "Hello World"
>>>
Hello World
In the interactive Python interpreter the print is not
necessary:
>>>
"Hello World"
>>>
3
3
>>>
Typing an end-of-file character (Ctrl+D on Unix, Ctrl+Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command: quit().
When commands are read from a tty, the interpreter is said to be
in interactive mode. In this mode it
prompts for the next command with the primary
prompt, usually three greater-than signs (>>>); for continuation
lines it prompts with the secondary
prompt, by default three dots (...).
The interpreter prints a welcome message stating its version number and a copyright
notice before printing the first prompt:
Continuation lines are needed when entering a multi-line
construct. As an example, take a look at this if statement:
>>>
the_world_is_flat = 1
>>>
if the_world_is_flat:
... print "Be careful not to fall off!"
2. List down the rules for naming the
variable with example.
A variable is a name that refers to a value. An assignment
statement creates new variables and gives them values:
Variable names can be arbitrarily long. They can contain both
letters and numbers, but they have to begin with a letter. It is legal to use
uppercase letters, but it is a good idea to begin variable names with a
lowercase letter .
The underscore character, _, can appear in a name. It is often
used in names with multiple words, such as my_name or variable_name.
If you give a variable an illegal name, you get a syntax error:
3. What do you mean by rule of
precedence? List out the order of precedence and demonstrate in detail with
example.
When more than one operator appears in an expression, the order
of evaluation depends on the rules of precedence. For mathematical operators,
Python follows mathematical convention. The acronym PEMDAS is a useful way to
remember the rules,
•
Parentheses have the highest
precedence and can be used to force an expression to evaluate in the order you
want. Since expressions in parentheses are evaluated first,
2 * (3-1) is 4, and (1+1)**(5-2) is 8.
You can also use parentheses to make an expression easier to
read, as in (minute * 100) / 60, even if it doesn’t change the result.
•
Exponentiation has the next highest
precedence, so 2**1+1 is 3, not 4 and 3*1**3 is 3, not 27.
•
Multiplication and
Division have the
same precedence, which
is higher than
Addition and Subtraction, which
also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
•
Operators with the same precedence
are evaluated from left to right (except exponentiation).
So in the expression degrees / 2 * pi, the division happens
first and the result is multiplied by pi. To divide by 2π, you can use
parentheses or write degrees / 2 / pi.
4. Explain the role of function call
and function definition with example.
A function is a named sequence of statements that performs a
computation. When you define a function, you specify the name and the sequence
of statements. Later, you can “call” the function by name.
>>>
type(32)
<type 'int'>
The name of the function is type. The expression in parentheses
is called the argument of the function. The result, for this function, is the
type of the argument. A function “takes” an argument and “returns” a result.
The result is called the return value.
Type conversion functions
Python provides built-in functions that convert values from one
type to another. The int function takes any value and converts it to an
integer, if it can, or complains otherwise:
>>>
int('32')
32
>>>
int('Hello')
ValueError: invalid literal for int(): Hello
int can convert floating-point values to integers, but it doesn’t
round off; it chops off the fraction part:
>>>
int(3.99999)
3
>>>
int(-2.3)
-2
float converts integers and strings to floating-point numbers:
>>>
float(32)
32.0
>>>
float('3.14159') 3.14159
Finally, str converts its argument to a string:
>>>
str(32)
'32'
>>>
str(3.14159)
'3.14159'
5. How do you make use of math
functions in Python?
Math functions
Python has a math module that provides most of the familiar
mathematical functions. A module is a file that contains a collection of
related functions.
Before we can use the module, we have to import it:
>>> import math
This statement creates a module object named math. If you print
the module object, you get some information about it:
>>> print math
<module 'math' (built-in)>
The module object contains the functions and variables defined
in the module. To access one of the functions, specify the name of the module
and the name of the function, separated by a dot (also known as a period). This
format is called dot notation.
>>>
ratio = signal_power / noise_power
>>>
decibels = 10 * math.log10(ratio)
>>>
radians = 0.7
>>>
height = math.sin(radians)
The first example uses log10 to compute a signal-to-noise ratio
in decibels (assuming that signal_power and noise_power are defined). The math
module also provides log, which computes logarithms base e.
The second example finds the sine of radians. The name of the
variable is a hint that sin and the other trigonometric functions (cos, tan,
etc.) take arguments in radians. To convert from degrees to radians, divide by
360 and multiply by 2π:
>>>
degrees = 45
>>>
radians = degrees / 360.0 * 2 *
math.pi
>>>
math.sin(radians)
0.707106781187
The expression math.pi gets the variable pi from the math
module. The value of this variable is an approximation of π, accurate to about
15 digits.
>>> math.sqrt(2) / 2.0 0.707106781187
6. Write a Python program to swap two
variables.
x = 5
y = 10
# create a temporary variable and swap the values temp = x
x = y
y = temp
print("The value of x after swapping:”,x))
print("The value of y after swapping:”,y))
7. Write a Python program to check
whether a given year is a leap year or not.
# To get year (integer input) from the user
year = int(input("Enter a year"))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("%d is a leap year"%year)
else:
print("%d is not a leap year"%d)
else:
print("%d is a leap year"%year)
else:
print("%d is not a leap year"%year)
8. Write a Python program to convert
celsius to fahrenheit .
(Formula: celsius * 1.8 = fahrenheit
–32).
#Python
Program to convert temperature in celsius to fahrenheit
#change
this value for a different result
celsius =
37.5
#calculate
fahrenheit
fahrenheit
= (celsius * 1.8) + 32
print('%0.1f
degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
Part B
1.
Explain in
detail about various data types in Python with an example?
2.
Explain the
different types of operators in python with an example.
3.
Discuss the need
and importance of function in python.
4.
Explain in
details about function prototypes in python.
5.
Discuss
about the various type of arguments in python.
6.
Explain the
flow of execution in user defined function with example.
7.
Illustrate
a program to display different data types using variables and literal
constants.
8.
Show how an
input and output function is performed in python with an example.
9.
Explain in
detail about the various operators in python with suitable examples.
10.
Discuss the
difference between tuples and list
11.
Discuss the
various operation that can be performed on a tuple and Lists (minimum 5)with an
example program
12.
What is
membership and identity operators.
13.
Write a program
to perform addition, subtraction, multiplication, integer division, floor
division and modulo division on two integer and float.
14.
Write a program
to convert degree Fahrenheit to Celsius
15.
Discuss the need
and importance of function in python.
16.
Illustrate a
program to exchange the value of two variables with temporary variables
17.
Briefly discuss
in detail about function prototyping in python. With suitable example program
18.
Analyze the
difference between local and global variables.
19.
Explain with an
example program to circulate the values of n variables
20.
Analyze with a
program to find out the distance between two points using python.
21.
Do the Case
study and perform the following operation in tuples i) Maxima minima iii)sum of
two tuples iv) duplicate a tuple v)slicing operator vi) obtaining a list from a
tuple vii) Compare two tuples viii)printing two tuples of different data types
22.
Write a program
to find out the square root of two numbers.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.