Unit - 1: Write A Program To Demosnstrate Basic Data Type in Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Unit – 1

Write a program to demosnstrate basic data type in Python.

Data type defines the kind of a value i.e the type of value whether it is int, string, float etc.
Since everything is an object in Python programming, data types are actually classes and
variables are instance (object) of these classes.
We don't need to define data types in python because python is dynamically typed
language, it automatically detects data types.
Python enables us to check the type of the variable used in the program. Python provides
us the type() function, which returns the type of the variable passed.
What are the basic data types in Python? What are the supported data type in
Python? We are having the following inbuilt data types in Python:

Numbers: Represent numeral values like int, float, complex

1. Int : It contains positive or negative whole numbers. eg: 1,2,3,-1.....


2. Float : Contains real floating point representation . eg: 2.0,1.0,8.02....
3. Complex : Numbers of form Ai+Bj . eg: 2i+3j

DEEPAK RANJAN (G.P KAIMUR) Page 1


Sequence Type:

String : A string is defined as a collection of one or more characters. It is putted in


single quotes or double quotes or triple quotes i.e (' ') , (" ") or (' ' ' ' ' ') . for eg:
s="GoEduHub.com"

 List : It is as same as arrays, it contains heterogeneous datatypes. eg: a=[1,2,"a"]

 Tuple : Tuples are created by ( ) . it contains heterogeneous datatypes . eg: t=(1,2,3)

Dictionary: Dictionary is an unordered collection of data. It has key:value pair which is


separated by a colon :, and all keys are separated by a ‘,’. eg : d={'a':1, 'b':2, 'c':3}

Boolean: Booleans represent one of two values: True or False.

DEEPAK RANJAN (G.P KAIMUR) Page 2


Set : Set is an unordered and unique collection of data types that mutable and has
distinct elements. eg: s={1,2,3,4,5}

1. Frozen set : Frozen sets are the sets that are immutable .

DEEPAK RANJAN (G.P KAIMUR) Page 3


Unit – 2
Write a program to computedistance between two points
taking input from the user (Pythagorean Theorem).
Distance can be calculated using the two points (x1, y1) and (x2, y2), the
distance d between these points is given by the formula:

for e.g : let x1 , y1=10,9 and x2 , y2=4,1 then (x2-x1)2=(10-4)2 = 62 = 36 and (y2-
y1)2= (9-1)2 = 82 = 64 now 64 + 36 =100 and 100 is square root of 10 sp distance
between (10,9) and (4,1) is 10 .

Program – 1

Output –
enter x1 : 4
enter x2 : 6
enter y1 : 0
enter y2 : 6

distance between (4, 6) and (0, 6) is : 6.324555320336759

DEEPAK RANJAN (G.P KAIMUR) Page 4


Program – 2

Output –
enter first coordinate: 4,0
enter second coordinate : 6,6

distance between 4,0 and 6,6 is 6.324555320336759

DEEPAK RANJAN (G.P KAIMUR) Page 5


Unit – 3
Write a python program Using for loop, write a program that
prints out the decimal equivalent of 1+½+1/3….1/n.

This can be done using a for loop statement , here we are having series of form (1/i)
where i is in range 1,2,3,4................

Program -1

Output –
0.5 0.3333333333333333 0.25 0.2 0.16666666666666666

0.14285714285714285 0.125 0.1111111111111111 0.1

Program-2

Output –
[0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666,

0.14285714285714285, 0.125, 0.1111111111111111, 0.1]

DEEPAK RANJAN (G.P KAIMUR) Page 6


Unit – 4
Write a Python program to find first n prime numbers.

Algorithm:
◉ First, take the number N as input.
◉ Then use a for loop to iterate the numbers from 1 to N.
◉ Then check for each number to be a prime number. If it is a prime
number, print it.

Coding –

Output:
Enter N:10
First 10 Prime numbers are:2 3 5 7 11 13 17 19 23 29.

DEEPAK RANJAN (G.P KAIMUR) Page 7


Write a program to demonstrate list and tuple in
python.

List and Tuple are built-in container types defined in Python. Objects of both
these types can store different other objects that are accessible by index. List as
well as tuple is a sequence data type, just as string. List as well as tuple can store
objects which need not be of same type.

List :
A List is an ordered collection of items (which may be of same or different types)
separated by comma and enclosed in square brackets.

In above list, each item is of different type. Further, each item is accessible by
positional index starting from 0. Hence L1[2] will return 25.5

DEEPAK RANJAN (G.P KAIMUR) Page 8


Tuple:
Tuple looks similar to list. The only difference is that comma separated items of
same or different type are enclosed in parentheses. Individual items follow zero
based index, as in list or string.

DEEPAK RANJAN (G.P KAIMUR) Page 9


Unit – 5
Write a program using a for loop that loops over a sequence.

For loops iterate over a given sequence.In sequence either (dictionary,


tuple, set, list and string )
for loop syntax (for variable in sequence or range)
for loop concept

Examples -

DEEPAK RANJAN (G.P KAIMUR) Page 10


Output -
For loop with range() Function
range() syntax -
range(a) - Generates a sequence started from 0 to a , where a is excluded
range(a,b)- Generates a sequence started from a ,end at b , where b is excluded
range(a,b,c)- Generates a sequence started from a, end at (b-1) and c is
difference (steps)
Note:-By default range() function take difference of 1, if difference is not given.

Example -

Output -
red green white purple pink marron

7 10 13
DEEPAK RANJAN (G.P KAIMUR) Page 11
sum is= 35

DEEPAK RANJAN (G.P KAIMUR) Page 12


Write a program using a while loop that asks the user for a
number and prints a countdown from that number to zero.

number = int(input('Enter number: '))


while number >= 0:
print(number)
number -= 1

Output :
Enter your number 10

10

DEEPAK RANJAN (G.P KAIMUR) Page 13


1

DEEPAK RANJAN (G.P KAIMUR) Page 14


DEEPAK RANJAN (G.P KAIMUR) Page 15
Unit – 6
Write a Python Program to add matrices.
In Python, we can implement a matrix as a nested list (list inside a list). We can
treat each element as a row of the matrix.

For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix. First row can
be selected as X[0] and the element in first row, first column can be selected
as X[0][0].

Output -

In this program we have used nested for loops to iterate through each row and

DEEPAK RANJAN (G.P KAIMUR) Page 16


each column. At each point, we add the corresponding elements in the two
matrices and store it in the result.

DEEPAK RANJAN (G.P KAIMUR) Page 17


Write a Python program to multiply matrices.

In Python, we can implement a matrix as nested list (list inside a list).

We can treat each element as a row of the matrix.

For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix.
The first row can be selected as X[0]. And, the element in first row, first column
can be selected as X[0][0].
Multiplication of two matrices X and Y is defined only if the number of columns
in X is equal to the number of rows Y.
If X is a n x m matrix and Y is a m x l matrix then, XY is defined and has the
dimension n x l (but YX is not defined). Here are a couple of ways to implement
matrix multiplication in Python.

DEEPAK RANJAN (G.P KAIMUR) Page 18


Output -

In this program, we have used nested for loops to iterate through each row and
each column. We accumulate the sum of products in the result.

DEEPAK RANJAN (G.P KAIMUR) Page 19


Unit – 7
Write a Python program to check if a string is
palindrome or not.
A palindrome is a string that is the same read forward or backward.

For example, "dad" is the same in forward or reverse direction. Another example
is "aibohphobia", which literally means, an irritable fear of palindromes.

Output -

Note: To test the program, change the value of my_str in the program.
In this program, we have taken a string stored in my_str.

Using the method casefold() we make it suitable for caseless comparisons.


Basically, this method returns a lowercased version of the string.

DEEPAK RANJAN (G.P KAIMUR) Page 20


Unit – 8
Write a Python program to Extract Unique values
dictionary values.

When it is required to extract unique values from a dictionary, a dictionary is


created, and the ‘sorted’ method and dictionary comprehension is used.
Below is a demonstration for the same −
Example -

Output -
The dictionary is :
{'hi': [5, 3, 8, 0], 'there': [22, 51, 63, 77], 'how': [7, 0, 22], 'are':
[12, 11, 45], 'you': [56, 31, 89, 90]}
The unique values are :
[0, 3, 5, 7, 8, 11, 12, 22, 31, 45, 51, 56, 63, 77, 89, 90]

Explanation -
 A dictionary is defined, and is displayed on the console.
 The values of the dictionary are accessed using the ‘.values’ method.
 It is converted into a list, and is assigned to a variable.

DEEPAK RANJAN (G.P KAIMUR) Page 21


 This is displayed as output on the console.

DEEPAK RANJAN (G.P KAIMUR) Page 22


Unit – 9
Write a Python program to read file word by word.
Introduction -
The task is to open a file and read all the words of that file.

Program -

Output -

Explanation
Approach:
To open a file in python, in-built function open() is used, it takes two arguments:
the file name or path and mode (i.e. ‘r’ for read, ‘w’ for write etc).

DEEPAK RANJAN (G.P KAIMUR) Page 23


Read the lines of file and words using for loop and print the words.

DEEPAK RANJAN (G.P KAIMUR) Page 24


Write a Python program to Get number of characters, words.
Input: Tutorial point is best Computer Science Portal
Output:
The number Of Words are : 6
The Number Of Characters are : 45

Input: Hello World!!!


Output:
The original string is : Hello World!!!
The number of words in string are : 2
The number of words in string are : 14
Count The Number Of Characters present in a string using len() function. You can also
use a for loop for counting characters
char=0
for i in string:
char=char+1
For Counting
Method 1: Using split()

The split function is quite useful and usually quite a generic method to get words out of
the list, but this approach fails once we introduce special characters in the list.

Output:

The original string is : Spoken tutorial is best Computer Science Portal


DEEPAK RANJAN (G.P KAIMUR) Page 25
The number of words in string are : 6
The number of words in string are : 45

DEEPAK RANJAN (G.P KAIMUR) Page 26


Method 2: Using regex module

Here findall() function is used to count the number of words in the sentence available in a
regex module.

Output:

The original string is : Spoken Tutorial is a learning platform


The number of words in string are : 5
The number of Characters in string are : 36
Method 3: Using sum()+ strip()+ split() function

Here we first check all the words in the given sentence and add them using the sum()
function.

Output:

The original string is: Spoken tutorial is a learning platform


DEEPAK RANJAN (G.P KAIMUR) Page 27
The number of words in string are : 5
The number of characters in string are : 36

DEEPAK RANJAN (G.P KAIMUR) Page 28


DEEPAK RANJAN (G.P KAIMUR) Page 29
Unit – 10
Write a Python program for Linear Search.
Linear search :-
Linear search is a sequential searching algorithm where we start from one end and check
every element of the list until the desired element is found. It is the simplest searching
algorithm.

How Linear Search Works?


The following steps are followed to search for an element k = 1 in the list below.

1. Start from the first element, compare k with each element x.

DEEPAK RANJAN (G.P KAIMUR) Page 30


2. If x == k, return the index.

3. Else, return not found.

Linear Search Algorithm

DEEPAK RANJAN (G.P KAIMUR) Page 31

You might also like