Python_Programming_Lab_Exercise[1]
Python_Programming_Lab_Exercise[1]
Exercise 1.1: Open the Python interpreter (console). What happens when you input the
following statements:
(a) 3 + 1
(b) 3 * 3
(c) 2 ** 3
(d) "Hello, world!"
Exercise 1.2: Now copy the above to a script, and save it as script1.py. run the script?
Exercise 1.3: Explain the output of the following statements:
(a) 'py' + 'thon'
(b) 'py' * 3 + 'thon'
(c) 'py' - 'py'
(d) '3' + 3
(e) 3 * '3'
(f) a
(g) a = 3
(h) a
Exercise 1.4: Explain the output of the following statements:
(a) 1 == 1
(b) 1 == True
(c) 0 == True
(d) 0 == False
(e) 3 == 1 * 3
(f) (3 == 1) * 3
(g) (3 == 3) * 4 + 3 == 1
(h) 3**5 >= 4**4
Exercise 1.5: Explain the output of the following statements:
(a) 5 / 3
(b) 5 % 3
(c) 5.0 / 3
(d) 5 / 3.0
(e) 5.2 % 3
(f) 2001 ** 200
Exercise 1.6: Explain the output of the following statements:
(a) 2000.3 ** 200
(b) 1.0 + 1.0 - 1.0
(c) 1.0 + 1.0e20 - 1.0e20
Exercise 1.7: Write a script where the variable name holds a string with your name.
Then, print the variable output.
Exercise 1.8: Type casting
Run the following and explain the output
(a) float(123)
(b) float('123')
(c) float('123.23')
(d) int(123.23)
(e) int('123.23')
(f) int(float('123.23'))
(g) str(12)
(h) str(12.2)
(i) bool('a')
(j) bool(0)
(k) bool(0.1)
Exercise 1.9: Write a function for odd and even then determining if a number is even or
odd using assert command.
Control Flow
Exercise 2.1: Open the Python interpreter (console). Print values from 0 to 50 using
range.
Exercise 2.2: Use loop to:
(a) Print the numbers 0 to 100
(b) Print the numbers 0 to 100 that are divisible by 7
(c) Print the numbers 1 to 100 that are divisible by 5 but not by 3
Exercise 2.3: Write a function that evaluates the polynomial 3x2 - x + 2.
Exercise 2.4:
(a) Write a function hello_world that prints 'Hello, world!'
(b) Write a function hello_name(name) that prints 'Hello, name!' where name is a string.
(c) Repeat above using return instead of print.
Exercise 2.5: Write a function max_num(x,y) that returns the maximum of x and y in
following two ways:
(a) Use both if and else.
(b) Use if but not else (nor elif).
Exercise 2.6: Write a python program to check if the parenthesis in given expression is
balanced or not.
Exercise 2.7: Write a python code to to Find ASCII Value of Character.
Exercise 2.8: Write a python code to text wrap given input string.
Exercise 2.9: Write a python program that mimics a google like search providing
suggestions based on what user type.
3
Python Programming Lab Exercise
Exercise 4.1: WAP that takes length of three sides of triangle as input and determine
whether it is equilateral, isosceles or scalene.
Exercise 4.2: WAP that takes 2 digit as an input and generate 2D array. The element
value of ith row and jth column should be i*j
Exercise 4.3: Write code that takes any number of student attributes name, age, course,
and grade as keyword and print output using kwargs and args.
Exercise 4.4: Write function that takes string as input and returns the vowels count.
Exercise 4.5: Write function update_profile (**kwargs) that starts with default
dictionary name, age and location. Use **kwargs to update field dynamically and
update profile
Exercise 4.6: Code to multiply all given numbers using args
Python Programming Lab Exercises 5
Exercise 5.1: WAP to convert decimal number into binary, octal and hexadecimal.
Exercise 5.2: WAP to make calculator implementing basic arithmetic operations
Exercise 5.3: Write code that takes list as an input (integers, string and mixed) and find
length, maximum and minimum value from the list.
Exercise 5.4: Write code that takes list as an input (integers, string and mixed) and sort
them in ascending and descending order.
Exercise 5.5: Write to check if the given number is happy number or not.
Python Programming Lab Exercises 6
Exercise 1: Read Total profit of all months and show it using a line plot
Total profit data provided for each month. Generated line plot must include the following properties:
• Line Style dotted and Line-color should be red Show legend at the lower right location.
• X label name = Month Number Y label name = Sold units number Add a circle marker.
• Line marker color as read
• Line width should be 3
The line plot graph should look like this.
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_excel('company_sales.xlsx',index_col=0)
df[['total_profit']]
plt.plot(df['total_profit'],
color='red',linestyle=':',linewidth=3,marker='o',markerfacecolor='black',label='Units Sold')
plt.xlabel('Month Number')
plt.ylabel('Sold units number')
plt.legend(loc='lower right')
Exercise 3: Read all product sales data and show it using a multiline plot
Display the number of units sold per month for each product using multiline plots. (i.e., Separate
Plotline for each product ).The graph should look like this.
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_excel('company_sales.xlsx',index_col=0)
df.plot(linewidth=3,marker='o');
plt.title("Company Profit Per Month")
plt.xlabel('Month Number')
plt.ylabel('Total Profit')
plt.legend(loc='upper left')
plt.title("Monthly Sold Units")
Exercise 4: Read face cream and facewash product sales data and show it using the bar chart
The bar chart should display the number of units sold per month for each product. Add a separate
bar for each product in the same chart.
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_excel('company_sales.xlsx',index_col=0)
total_sales = df.drop(columns='total_profit').sum()
plt.figure(figsize=(12, 7))
plt.pie(total_sales, labels=total_sales.index, autopct='%1.1f%%', startangle=10)
plt.title('Sales Data')
plt.axis('equal')
plt.legend(loc='lower right')
plt.show()