Unit - 1: Write A Program To Demosnstrate Basic Data Type in Python
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:
1. Frozen set : Frozen sets are the sets that are immutable .
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
Output –
enter first coordinate: 4,0
enter second coordinate : 6,6
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
Program-2
Output –
[0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666,
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.
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
Examples -
Example -
Output -
red green white purple pink marron
7 10 13
DEEPAK RANJAN (G.P KAIMUR) Page 11
sum is= 35
Output :
Enter your number 10
10
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
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.
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.
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.
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.
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).
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:
Here findall() function is used to count the number of words in the sentence available in a
regex module.
Output:
Here we first check all the words in the given sentence and add them using the sum()
function.
Output: