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

Aim Do Following

The document contains code snippets for several Python programming practical exercises: 1. The first code defines a function to check if a number is even or odd and categorizes numbers into even and odd lists. It then prints the total of even and odd numbers. 2. The second code creates a GUI with Tkinter to display a dropdown list of cities and show the selected city. 3. The third code uses the Turtle module to draw shapes like square, rectangle and star on a graphical window, demonstrating turtle attributes like color, pensize etc.

Uploaded by

Slay Tyrant
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)
20 views

Aim Do Following

The document contains code snippets for several Python programming practical exercises: 1. The first code defines a function to check if a number is even or odd and categorizes numbers into even and odd lists. It then prints the total of even and odd numbers. 2. The second code creates a GUI with Tkinter to display a dropdown list of cities and show the selected city. 3. The third code uses the Turtle module to draw shapes like square, rectangle and star on a graphical window, demonstrating turtle attributes like color, pensize etc.

Uploaded by

Slay Tyrant
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/ 18

PSC(CE0525) IU2141220108

Practical-38

Aim: Do following:

1) Define and Call user defined function for n numbers one by one.

2) Check if each element is Even or Odd and Print it. (Use Class Variable) 3)

Print List of even numbers and odd numbers.

Code1:

def process_number(number):

# Replace this with the actual task you want to perform on each number

result = number * 2

return result

numbers = [1, 2, 3, 4, 5]

for number in numbers:

output = process_number(number)

print(f"Input: {number}, Output: {output}")

Output:

Code2:

class NumberChecker:

# Class variable to store even and odd numbers

even_numbers = []

odd_numbers = []

5 IT-B(IITE)
PSC(CE0525) IU2141220108

@classmethod

def check_and_categorize(cls, number):

if number % 2 == 0:

cls.even_numbers.append(number)

print(f"{number} is even.")

else:

cls.odd_numbers.append(number)

print(f"{number} is odd.")

# Create a list of numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Initialize the class variable

NumberChecker.even_numbers = []

NumberChecker.odd_numbers = []

# Check and categorize each number

for number in numbers:

NumberChecker.check_and_categorize(number)

# Print the list of even and odd numbers

print("Even numbers:", NumberChecker.even_numbers)

print("Odd numbers:", NumberChecker.odd_numbers)

5 IT-B(IITE)
PSC(CE0525) IU2141220108

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Output:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical – 39

Aim: Print total number of Even numbers and Total numbers of Odd Numbers.

Example: 1) function (21), function (22), function (35), function (36), function (40).

2) 21 is odd, 22 is even, 35 is odd,36 is even, 40 is even.

3) Even= {22,36,40} Odd= {21,35}

total number of even = 3, total number of odd=2

Code1:

class NumberChecker:

# Class variable to store even and odd numbers

even_numbers = []

odd_numbers = []

@classmethod

def check_and_categorize(cls, number):

if number % 2 == 0:

cls.even_numbers.append(number)

print(f"{number} is even.")

else:

cls.odd_numbers.append(number)

print(f"{number} is odd.")

# Create a list of numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Initialize the class variable

NumberChecker.even_numbers = []

NumberChecker.odd_numbers = []

# Check and categorize each number

5 IT-B(IITE)
PSC(CE0525) IU2141220108

for number in numbers:

NumberChecker.check_and_categorize(number)

# Print the list of even and odd numbers

print("Even numbers:", NumberChecker.even_numbers)

print("Odd numbers:", NumberChecker.odd_numbers)

# Total number of even numbers

total_even = len(NumberChecker.even_numbers)

# Total number of odd numbers

total_odd = len(NumberChecker.odd_numbers)

print("Total number of even numbers:", total_even)

print("Total number of odd numbers:", total_odd)

Output:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-49

Aim: Create a dropdown list to select a city from the given list of cities.

Code:

import tkinter as tk

from tkinter import ttk

# List of cities

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

# Function to display the selected city

def show_selected_city():

selected_city = city_var.get()

result_label.config(text=f"Selected city: {selected_city}")

# Create the main window

5 IT-B(IITE)
PSC(CE0525) IU2141220108

root = tk.Tk()

root.title("City Selection")

# Create a label

label = tk.Label(root, text="Select a city:")

label.pack()

# Create a variable to store the selected city

city_var = tk.StringVar()

# Create a dropdown list

city_dropdown = ttk.Combobox(root, textvariable=city_var, values=cities)

city_dropdown.pack()

# Set a default value for the dropdown

city_dropdown.set("Select a city")

# Create a button to show the selected city

show_button = tk.Button(root, text="Show City", command=show_selected_city)

show_button.pack()

# Create a label to display the selected city

result_label = tk.Label(root, text="")

result_label.pack()

# Start the GUI main loop

root.mainloop()

Output:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-50

Aim: Write a tkinter code to place an image/picture in the window.

Code:

import tkinter as tk

from tkinter import ttk

from PIL import Image, ImageTk

# Create the main window

root = tk.Tk()

root.title("Image Display")

# Load and display an image

5 IT-B(IITE)
PSC(CE0525) IU2141220108

image = Image.open("your_image.jpg") # Replace "your_image.jpg" with the path to your image file

photo = ImageTk.PhotoImage(image)

label = ttk.Label(root, image=photo)

label.pack()

# Start the GUI main loop

root.mainloop()

Output:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-52

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Aim: Using the Turtle module in python, Draw Square, Rectangle, Star and explore various

attributes with it (ex: background color, line fill, border color, etc...)

Code:

import turtle

# Create a Turtle screen

screen = turtle.Screen()

screen.bgcolor("lightblue") # Set the background color

# Create a Turtle object

pen = turtle.Turtle()

# Function to set pen attributes

def set_pen_attributes(color, fill_color, pen_size):

pen.pencolor(color) # Set pen (border) color

pen.fillcolor(fill_color) # Set fill color

pen.pensize(pen_size) # Set pen size

pen.begin_fill()

# Function to draw a square

def draw_square(size):

set_pen_attributes("red", "yellow", 2) # Example attributes

for _ in range(4):

pen.forward(size)

pen.right(90)

pen.end_fill()

# Function to draw a rectangle

5 IT-B(IITE)
PSC(CE0525) IU2141220108

def draw_rectangle(width, height):

set_pen_attributes("green", "blue", 3) # Example attributes

for _ in range(2):

pen.forward(width)

pen.left(90)

pen.forward(height)

pen.left(90)

pen.end_fill()

# Function to draw a star

def draw_star(size):

set_pen_attributes("purple", "orange", 2) # Example attributes

for _ in range(5):

pen.forward(size)

pen.right(144)

pen.end_fill()

# Set the starting position of the Turtle

pen.penup()

pen.goto(-100, 0)

pen.pendown()

# Draw a square

draw_square(100)

# Move to the next position

pen.penup()

5 IT-B(IITE)
PSC(CE0525) IU2141220108

pen.goto(0, 0)

pen.pendown()

# Draw a rectangle

draw_rectangle(120, 80)

# Move to the next position

pen.penup()

pen.goto(100, 0)

pen.pendown()

# Draw a star

draw_star(100)

# Close the drawing window when clicked

screen.exitonclick()

Code:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-53

Aim:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-55

Aim:

Code:

5 IT-B(IITE)
PSC(CE0525) IU2141220108

Practical-56

Aim: Create a variable ingredients with a Series that looks like:

Flour 4 cups

Milk 1 cup

Eggs 2 large

Spam 1 can

Name: Dinner, dtype: object

Code:

import pandas as pd

data = {

'Flour': '4 cups',

'Milk': '1 cup',

'Eggs': '2 large',

5 IT-B(IITE)
PSC(CE0525) IU2141220108

'Spam': '1 can'

ingredients = pd.Series(data, name='Dinner')

print(ingredients)

Output:

5 IT-B(IITE)

You might also like