Você está na página 1de 25

VAAGESWARI COLLEGE OF ENGINEERING

(affiliated to JNTUH Approved by A.I.C.T.E)


Beside L.M.D Police Station,
Thimmapur, Karimnagar.

SCRIPTING
(PYTHON)
PROGRAMMING LAB
MCA-I Year – II Sem LAB MANUAL

Prepared by:
Mr. M. Murali Mohan Reddy
Assistant Professor
Computer Science Department
SCRIPTING (PYTHON) PROGRAMMING LAB 2017
1) Write a program to demonstrate different number data types in Python.

Choose Start menu on windows and select “Python 3.6.4 IDLE” then it will display a Python
3.6.4 Shell –window

Click on File menu and choose “New File” option for creating a new shell script python file

Save the above program as “first_experiment.py”

By Mr. M. Murali Mohan Reddy Page 2


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
To run the program choose “Run” ->”Run Module –F5”

Out Put will be displayed in Python 3.6.4 Shell –window

By Mr. M. Murali Mohan Reddy Page 3


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
2) Write a Program to perform different Arithmetic operations on numbers in Python.

Output

By Mr. M. Murali Mohan Reddy Page 4


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
3) Write a program to create, concatenate and print a string and accessing sub-string from a
given string.

Output

By Mr. M. Murali Mohan Reddy Page 5


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
4) Write a python script to print the current date in the following format “Sun May 29 02:26:23
IST 2017”

Output

By Mr. M. Murali Mohan Reddy Page 6


SCRIPTING (PYTHON) PROGRAMMING LAB 2017

5) Write a program to create, append, and remove lists in python.

Output

By Mr. M. Murali Mohan Reddy Page 7


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
6) Write a program to demonstrate working with tuples in python.

Output

By Mr. M. Murali Mohan Reddy Page 8


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
7) Write a program to demonstrate working with dictionaries in python.

Seventh_experiment.py (code)

#creating and access elements from a dictionary

my_dict = {'name':'Jack', 'age': 26}

print(my_dict['name'])

print(my_dict.get('age'))

#How to change or add elements in a dictionary

# update value

my_dict['age'] = 27

#Output: {'age': 27, 'name': 'Jack'}

print(my_dict)

# add item

my_dict['address'] = 'Downtown'

print(my_dict)

#to delete or remove elements from a dictionary

# create a dictionary

squares = {1:1, 2:4, 3:9, 4:16, 5:25}

# remove a particular item

print(squares.pop(4)) # Output: 16

print(squares)# Output: {1: 1, 2: 4, 3: 9, 5: 25}

# remove an arbitrary item

print(squares.popitem())# Output: (5, 25)

print(squares)# Output: {1: 1, 2: 4, 3: 9}

# delete a particular item

del squares[1]

By Mr. M. Murali Mohan Reddy Page 9


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
print(squares)# Output: {2: 4, 3: 9}

# remove all items

squares.clear()

print(squares)# Output: {}

# delete the dictionary itself

del squares

# print(squares)# Throws Error

marks = {}.fromkeys(['Math','English','Science'], 0)

# Output: {'English': 0, 'Math': 0, 'Science': 0}

print(marks)

for item in marks.items(): print(item)

# Output: ['English', 'Math', 'Science']

print(list(sorted(marks.keys())))

Output

By Mr. M. Murali Mohan Reddy Page 10


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
8) Write a python program to find largest of three numbers.

Output

By Mr. M. Murali Mohan Reddy Page 11


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
9) Write a Python program to convert temperatures to and from Celsius, Fahrentheit. [Formula :
c/5=f-32/9]

By Mr. M. Murali Mohan Reddy Page 12


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
Output

By Mr. M. Murali Mohan Reddy Page 13


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
10) Write a python program to construct the following pattern, using a nested for loop

Output

By Mr. M. Murali Mohan Reddy Page 14


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
11) Write a Python script that prints prime numbers less than 20.

Output

By Mr. M. Murali Mohan Reddy Page 15


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
12) Write a python program to find factorial of a number using Recursion.

Output

By Mr. M. Murali Mohan Reddy Page 16


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
13) Write a program that accepts the lengths of three sides of a triangle as inputs. The program
output should indicate whether or not the triangle is a right triangle (Recall from the
Pythagorean Theorem that in a right triangle, the square of one side equals the sum of
squares of the other two sides).

Output

By Mr. M. Murali Mohan Reddy Page 17


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
14) Write a python program to define a module to find Fibonacci Numbers and import the module
to another program.

fibo.py

fourteenth_experiment.py

Output

By Mr. M. Murali Mohan Reddy Page 18


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
15) Write a python program to define a module and import a specific function in that module to
another program.

fibo1.py

fifteenth_experiment.py

Output

By Mr. M. Murali Mohan Reddy Page 19


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
16) Write a script named “copyfile.py”. This script should prompt the user for the names of two
text files. The contents of the first file should be input and written to the second file.

input.txt

copyfile.py

By Mr. M. Murali Mohan Reddy Page 20


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
Output

output.txt

By Mr. M. Murali Mohan Reddy Page 21


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
17) Write a program that inputs a text file. The program should print all of the unique words in
the file in alphabetical order.

input1.txt

seventeenth_experiment.py

Output

By Mr. M. Murali Mohan Reddy Page 22


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
18) Write a Python class to convert an integer to a roman numeral.

Output

By Mr. M. Murali Mohan Reddy Page 23


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
19) Write a Python class to implement pow(x, n).

Output

By Mr. M. Murali Mohan Reddy Page 24


SCRIPTING (PYTHON) PROGRAMMING LAB 2017
20) Write a Python class to reverse a string word by word.

Output

By Mr. M. Murali Mohan Reddy Page 25

Você também pode gostar