0% found this document useful (0 votes)
29 views6 pages

Lab 9

Uploaded by

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

Lab 9

Uploaded by

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

Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

Experiment- 9

To perform basic operations in Python..

1) Write a Python program to Create Bar chart using matplotlib

CODE:
import numpy as np
import matplotlib.pyplot as plt
data = {'C':21, 'C++':15, 'Java':30, 'Python':39}
courses = list(data.keys())
values = list(data.values())
fig = plt.figure(figsize = (9, 5))
plt.bar(courses, values, color ='maroon', width = 0.3)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.show()

OUTPUT:
Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

2) Write a Python program to Create pie chart using matplotlib.

CODE:
from matplotlib import pyplot as plt
import numpy as np
cars = ['McLaren', 'BMW', 'Ferrari','Rolls Royace', 'Aston Martin', 'Porche']
mycolors = ["black", "hotpink", "b", "#4CAF50","orange","maroon"]
data = [23, 17, 35, 29, 12, 41]
fig = plt.figure(figsize=(10, 7))
plt.pie(data, labels=cars, colors = mycolors)
plt.show()

OUTPUT:
Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

3) Write a tkinter code to Create a dropdown list to select a city from the given list of cities.

CODE:

import tkinter as tk
from tkinter import ttk

cities = ["New York", "Los Angeles", "Chicago", "San Francisco", "Miami"]


def city_selected(event):
selected_city = city_var.get()
print(f"Selected city: {selected_city}")
root = tk.Tk()
root.title("City Selector")
city_var = tk.StringVar()
city_dropdown = ttk.Combobox(root, textvariable=city_var, values=cities)
city_dropdown.set("Select a city")
city_dropdown.bind("<<ComboboxSelected>>", city_selected)
city_dropdown.pack(pady=10)
root.mainloop()

OUTPUT:

4) Write a tkinter code to place an image/picture in the window.

CODE:

import tkinter as tk
from tkinter import PhotoImage
root = tk.Tk()
root.title("Image Display")
Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

image_path = "sampleimg.png"
try:
img = PhotoImage(file=image_path)
except tk.TclError:
print(f"Error loading image from {image_path}")
img = None
image_label = tk.Label(root, image=img)
image_label.pack()
root.mainloop()

OUTPUT:
Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

5) Write a tkinter code to Create Registration window.

CODE:

import tkinter as tk
from tkinter import messagebox
def register_user():
username = entry_username.get()
password = entry_password.get()
messagebox.showinfo("Registration Successful", f"Welcome, {username}!")
root = tk.Tk()
root.title("User Registration")
label_username = tk.Label(root, text="Username:")
label_password = tk.Label(root, text="Password:")
entry_username = tk.Entry(root)
entry_password = tk.Entry(root, show="*") # Mask the password
register_button = tk.Button(root, text="Register", command=register_user)
label_username.pack()
entry_username.pack()
label_password.pack()
entry_password.pack()
register_button.pack()
root.mainloop()

OUTPUT:
Branch: EC/EL/ICT Enrollment no: IU2241240033

EC0525 /ICT0503- PROBLEM SOLVING WITH PROGRAMMING (WITH


PYTHON)
EL0527- SCIENTIFIC COMPUTING IN ENGINEERING

6) Draw Square and star using Turtle with attributes of colors

CODE:

import turtle
screen = turtle.Screen()
screen.bgcolor("white") # Background color
artist = turtle.Turtle()
artist.penup()
artist.goto(-50, -50) # Position the turtle
artist.pendown()
artist.color("red") # Square color
for _ in range(4):
artist.forward(100) # Move forward
artist.right(90) # Turn right
artist.penup()
artist.goto(0, 50) # Position for the star
artist.pendown()
artist.color("blue") # Star color
for _ in range(5):
artist.forward(100) # Move forward
artist.right(144) # Turn right
artist.hideturtle()
turtle.done()

OUTPUT:

You might also like