PDS Lab Manual
PDS Lab Manual
INTRODUCTION
Theory:
Python is a language with a simple syntax, and a powerful set of libraries. It is an interpreted
language, with a rich programming environment, including a robust debugger and profiler.
While it is easy for beginners to learn, it is widely used in many scientific areas for data
exploration. We cover data types, control flow, object-oriented programming, and graphical
user interface-driven applications. The examples and problems used in this course are drawn
from diverse areas such as text processing, simple graphics creation and image manipulation,
HTML and web programming, and genomics.
Using IDLE
It has a Python shell window, which gives you access to the Python interactive mode. It also
has a file editor that lets you create and edit existing Python source files.
During the following discussion of IDLE's features, instead of passively reading along, you
should start IDLE and try to replicate the screenshots.
When you start up IDLE, a window with an interactive Python shell will pop up:
You can type Python code directly into this shell, at the '>>>' prompt. Whenever you enter a
complete code fragment, it will be executed. For instance, typing:
hello world
Try typing an underscore ( _). Can you see it? On some operating systems, the bottoms of
hanging letters such as 'g' or 'y', as well as underscores, cannot be seen in IDLE. If this is the
case for you, go to Options -> Configure IDLE, and change the size of the default font to 9 or
11. This will fix the problem!
> 4+4
8
> 8**3
512
Addition (+), subtraction (-), multiplication (*), division (/), modulo (%) and power (**)
operators are built into the Python language. This means you can use them right away. If you
want to use a square root in your calculation, you can either raise something to the power of
0.5 or you can import the math module. Below are two examples of square root calculation:
>>> 16**0.5
4.0
>>> import math
>>> math.sqrt(16)
4.0
> math.log(16, 2)
4.0
> math.cos( 0 )
1.0
Note that you only need to execute the import command once after you start IDLE; however,
you will need to execute it again if you restart the shell, as restarting resets everything back to
how it was when you opened IDLE. Creating scripts
Exercise
a) Given Base and height of the triangle. Take input from user.
b) Given Three sides of a triangle (Make sure that it forms a triangle). Take input from user.
AIM:-
Demonstrate the
working of ‘id’
and ‘type’
functions
THEORY:-
As we can see
the function
accepts a single
parameter and is
used to return
the identity of an
object. This
identity has to
be unique and
constant for
this object
during the
lifetime. Two
objects with
non-overlapping
lifetimes may
have the same
id() value. If we
relate this to C,
then they are
actually the
memory address,
Examples:
The output is the identity of the object passed. This is random but
when running in the same program, it generates unique and same
identity.
Input:
id(1025)
Output: 2698213093488
Output varies with different runs
Input :
id("geek")
Output: 2698213126704
str2 = "geek"
print(id(str2))
# This will
return True
print(id(str1)
== id(str2))
# Use in Lists
The id() function returns identity of the object. This is an integer which is unique for the
given object and remains constant during its lifetime.
Example:
print('id of 5 =',id(5))
a=5
print('id of a =',id(a))
b=a
print('id of b =',id(b))
c = 5.0
print('id of c =',id(c))
Python have a built-in method called as type which generally come in handy while figuring
out the type of variable used in the program in the runtime.
The type function returns the datatype of any arbitrary object. The possible types are listed
in the types module. This is useful for helper functions that can handle several types of
data.
>>> type(1)
<class 'int'>
> li = []
> type(li)
<class 'list'>
> import odbc
> type(odbc)
<class 'module'>
> import types
> type(odbc) == typesduleType
True
Type takes anything -- and I mean anything -- and returns its data type. Integers, strings, lists,
dictionaries, tuples, functions, classes, modules, even types are acceptable.
• type can take a variable and return its datatype.
• type also works on modules.
You can use the constants in the types module to compare types of objects. This is what
the info function does, as you'll see shortly.
Exercises:
Review Questions:
THEORY:-
Prime numbers:
A prime number is a natural number greater than 1 and having no positive divisor other than
1 and itself.
Composite number:
Other natural numbers that are not prime numbers are called composite numbers.
Here is source code of the Python Program to check if a number is a prime number.
Exercise:
Output:-
Review Questions:
THEORY: The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1,
2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8.... The first two terms are 0
and 1.
All other terms are obtained by adding the preceding two terms.
This means to say the nth term is the sum of (n-1)th and (n-2)th term. In mathematical terms,
the sequence Fn of Fibonacci numbers is defined by the recurrence relation :
Output:
Review Questions:
EXAMPLE:
We use the slice notation on strings. In this example, we omit the first index to start at
the beginning, and then consume four characters total. We extract the first four letters.
Copy with slicing, we can copy sequences like lists. We assign a new list variable to a
slice with no specified start or stop values. So the slice copies the entire list and
returns it.
To extract substring from the whole string then we use the syntax like
string_name[beginning: end : step]
end denotes the end index of string which is not inclusive, steps denote the distance
between the two words. o beginning represents the starting index of string.
Exercise:
1. Accept a string from the user and display the string with first character of each
word Capital. (Without using inbuilt function)
2. Write a program to accept a string from the user and display n characters from the
left of the string. (Accept n from the user)
4. Write a program to accept a string from the user and display n characters from the
right of the string. (Accept n from the user)
5. Accept a String from the user and display first two characters of each word in
same line separated by space.
Review Questions: