Python Key
Python Key
ob = Demo()
ob.show()
output:
using self variable
F) State any three mathematical functions in python.
Ans)
Math module is used to define mathematical functions, trigonometric functions,
logarithmic functions, angle conversion functions. If is a predefined module. Module can
be used by using “import” statement.
Ex:
import math
print(math.pi) //prints mathematical constant value
print(math.pow(2,4)) // performs 2^4
print(math.sqrt(100)) //square_root of 100
output:
3.141592653589793
16.0
10.0
PART-B
1. A) Elaborate the need of python programming in detail and python script
Ans)
Internet scripting
Database Programming
Software quality
Developer productivity
Component integration
It's Object-Oriented
Python is an object-oriented language. Its class model supports advanced
notions such as polymorphism, operator overloading, and multiple inheritance. Object-
oriented programming (OOP) is easy to apply. Python programs can subclass classes
implemented in C++ or Java.
Enjoyment
Python's built-in toolset, learning of python programming is more easy for
users.
It's Free
Python is freeware also called as open source software. There are no
restrictions on copying it, embedding it in your systems, or shipping it with your
products.
It’s Portable
Python is written in portable ANSI C. it runs on UNIX systems, Linux, MS-
DOS, MS-Windows (95, 98, NT), and more. Python programs are automatically
compiled to portable byte code, which runs the same on any platform with a compatible
version of Python installed.
It's Powerful
Python is a hybrid means its tool is set places in between traditional scripting
languages (such as Tcl, Scheme, and Perl), and systems languages (such as C, C++, and
Java). Python provides all the simplicity and ease of use of a scripting language, along
with more advanced programming tools.
Automatic memory management
Python automatically allocates and reclaims ("garbage collects") objects when
no longer used, and most grow and shrink on demand.
It's Mixable
Python programs can be easily "glued" to components written in other
languages. Python programs can be both extended by components written in C or C++,
and embedded in C or C++ programs.
It's Easy to Use
To run a Python program, simply type it and run it. There are no intermediate
compile and link steps (as when using languages such as C or C++). Python programs
are compiled (translated) to an intermediate form called bytecode, which is then run by
the interpreter.
It's Easy to Learn
compared to other programming languages python language is easy to learn.
Support libraries
Python comes with a large collection of prebuilt and portable functionality,
known as the standard library.
1. B) Write a python program to read number ‘n’ and find the sum of odd
numbers till the given number ‘n’.
Ans)
Program:
n=10
sum=0
for i in range(1,n+1):
if(i%2==1):
print(i)
sum=sum+i
print(“sum is:”,sum)
Output:
sum is:25
2. A) Write short notes on
i) Identity operators
ii) If-elif-else structure
iii) For loop & For each
iv) Range function
Ans)
i) Identity operator: Identity operators compare the memory locations of two objects.
There are two Identity operators
Is: Evaluates to true if the variables on either side of the operator point to the
same object and false otherwise.
is not: Evaluates to false if the variables on either side of the operator point to
the same object and true otherwise.
Example:
a = 20
b = 20
if ( a is b ):
print("a and b have same identity")
else:
print(" a and b do not have same identity")
if ( id(a) == id(b) ):
print(" a and b have same identity")
else:
print("a and b do not have same identity")
Output
a and b have same identity
a and b have same identity
ii) If-elif-else structure:
If the condition for if is False, it checks the condition of the next elif block and so on. If
all the conditions are False, body of else is executed. Only one block among the several
if...elif...else blocks is executed according to the condition. The if block can have only one
else block. But it can have multiple elif blocks.
Syntax:
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
Flowchart:
Program:
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive number
iii) For loop & For each: It has the ability to iterate over the items of any sequence, such as a
list, tuple or a string. For each is nothing but for loop iterate on sequence.
Syntax:
for iterating_var in sequence:
statements(s)
Flowchart:
Program:
for i in 'Python':
print(i)
iv) Range function: range() allows user to generate a series of numbers within a given range.
Depending on how many arguments user is passing to the function, user can decide where
that series of numbers will begin and end as well as how big the difference will be between
one number and the next. range() takes mainly three arguments.
start: integer starting from which the sequence of integers is to be returned
stop: integer before which the sequence of integers is to be returned.
The range of integers end at stop – 1.
step: integer value which determines the increment between each integer in the
sequence
Example:
for i in range(10):
print(i, end =" ")
print()
sum = 0
for i in range(1, 10):
sum = sum + i
print("Sum of first 10 natural number :", sum)
Output:
0123456789
10 20 30 40
Sum of first 10 natural number: 45
2 B) Write a python program to generate the first 30 multiples of 5.
Ans)
Program:
n=int(input("enter value:"))
print("first 30 multiples of 5")
for i in range(1,n+1):
if(i%5==0):
print(i,end=" ")
Output:
enter value:150
first 30 multiples of 5
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135
140 145 150
Output:
Empty Dictionary:
{}
Dict[0] = 'python'
Dict[2] = 'ds'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Output:
Empty Dictionary:
{}