0% found this document useful (0 votes)
1 views

Python Programming Lab 1-10

The document provides a comprehensive guide to basic Python programming concepts, including data types, list and tuple methods, dictionary operations, and simple arithmetic operations through a menu-driven program. It also covers conditional statements, filtering even numbers from a list, and working with date and time. Additionally, it demonstrates how to count characters in a string and store the counts in a dictionary.

Uploaded by

ameershaikh0222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python Programming Lab 1-10

The document provides a comprehensive guide to basic Python programming concepts, including data types, list and tuple methods, dictionary operations, and simple arithmetic operations through a menu-driven program. It also covers conditional statements, filtering even numbers from a list, and working with date and time. Additionally, it demonstrates how to count characters in a string and store the counts in a dictionary.

Uploaded by

ameershaikh0222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PYTHON PROGRAMMING

1. Write a program to demonstrate basic datatype in python


a = 10 #integer(int)
print(a)
b = 2.5 #floating point(float)
print(b)
c = 1 + 3.1j #complex number(complex)
print( c)
d = "Hello World!" #string(str)
print(d)
e = [1,2,"3",4,5.0] #list(list)(like arrays but supports multiple datatypes)
print(e)
f = (1,2,"3",4,5.0) #tuple(tuple)(like lists but immutable)
print(f)
g = {'a':5, 2: 'b'} #dictionary(dict)
print(g)
h = True #boolean(bool)
print(h)

2. Create a list and perform the following methods


insert()
remove()
append()
len()
pop()
clear()
-------------------------------------------------------------------------------
li = [1,2,4,5,6,7,8]
li.insert(2, 3)
print(li)

li.remove(3)
print(li)

li.append(9)
print(li)

n = len(li)
print("The length of list is ",n)

poped_ele = li.pop() #removes the last element in list and returns it


print(li)
print("The popped element is ", poped_ele)

li.clear()
print(li) #empty list
3. Create a tuple and perform the following methods
add items
len()
check for item in tuple
access items
-----------------------------------------------------------
tu = (1,2,3,4,5,6)
print("The original tuple is ", tu)
tu = tu+(7,8)
print(tu)
n = len(tu)
print("The length of tuple is ", n)
pos = tu.index(4)
print("The position of 4 in tuple is ", pos)
print(tu[4]) #Print element at index 4
print(tu[2]) #Print element at index 2

4. Create a dictionary and perform the following methods


print the dictionary items
access items
use get()
change values
len()
d = {1:'a', 2:'b', 3:'c', 4:'d', 'apple':'fruit'}
print(d)
print( )
print(d['apple'])
print(d[1])
print(d[3])
print()
print(d.get('apple'))
print()
d[3] = 'f '
print(d)
print()
n = len(d)
print("The length of dictionary is ", n)

5. Write a program to create a menu with the following options


1. TO PERFORM ADDITION
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPLICATION
4. TO PERFORM DIVISION
Accept users input and perform the operation accordingly. Use functions with
arguments.
def add(a, b):
s = a+b
print("The sum is ", s)
def subtract(a, b):
d = a-b
print("The difference is ", d)
def multiply(a, b):
p = a*b
print("The product is ", p)
def divide(a, b):
q = a//b
print("The quotient is ", q)
q='y'
while(q!='q'):
print("1. TO PERFORM ADDITITON")
print("2. TO PERFORM SUBTRACTION")
print("3. TO PERFORM MULTIPLICATION")
print("4. TO PERFORM DIVISION")
choice = int(input("Enter your choice: "))
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if choice == 1:
add(a, b)
elif choice == 2:
subtract(a, b)
elif choice == 3:
multiply(a, b)
elif choice == 4:
divide(a, b)
print("Press q to quit")
q = input("Do you want to continue? ")

6. Write a program to print a number is positive or negative using if-else


n = int(input("Enter a number: "))
if n>=0:
print("Positive number")
else:
print("Negative number")
7. Write a program for filter() to filter only even numbers from a given list
def even(value):
if value % 2 == 0:
return True
else:
return False
li = eval(input("Enter a list: "))
filtered = filter(even, li)
for i in filtered:
print(i)

8. Write a program to print date, time for today and now


import datetime
curr_time = datetime.datetime.now().time()
curr_date = datetime.datetime.now().date()
print("The current time is ", curr_time)
print("The current date is ", curr_date)

9. Write a python program to add some days to your present date and print
the date added
import datetime
date = datetime.datetime.now().date() #present day
date += datetime.timedelta(days=5) #adding days
print(date)
10.Write a program to count the number of characters in the string and store
them in a dictionary data structure
s = input("Enter a string: ")
d = {}
for i in s:
if i in d:
d[i]+=1
else:
d[i]=1
print(d)

You might also like