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

Python Syllabus Detailed Explanation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Syllabus Detailed Explanation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Programming: Detailed Explanation

UNIT III

Dictionaries in Python

Dictionaries are collections of key-value pairs where keys must be unique and immutable. They are

unordered and accessed via keys, not indices.

# Example of dictionary usage

students = {"Alice": 90, "Bob": 85, "Charlie": 88}

print(students["Alice"]) # Output: 90

# Looping through a dictionary

for name, grade in students.items():

print(f"{name} scored {grade}")

# Conditional statements with dictionaries

if "Alice" in students:

print("Alice's grade is recorded.")

Modules in Python

Modules allow you to organize code into reusable files. You can execute modules as scripts, define reusable

functions, and manage dependencies effectively.

# Example of creating and using a module

# my_module.py

def greet(name):

return f"Hello, {name}!"


Python Programming: Detailed Explanation

# Importing and using the module in another file

import my_module

print(my_module.greet("Alice")) # Output: Hello, Alice!

# Executing modules as scripts

if __name__ == "__main__":

print("This script is executed directly.")

Error and Exception Handling

Python provides robust error and exception handling using try-except blocks. This ensures the program can

handle unexpected situations gracefully.

try:

result = 10 / 0

except ZeroDivisionError as e:

print(f"Error occurred: {e}")

finally:

print("Execution completed.")

UNIT IV

Classes and Objects

Classes are blueprints for creating objects. They define attributes and methods shared by all objects created

from them. Objects are instances of classes with unique data.

class Car:
Python Programming: Detailed Explanation

def __init__(self, make, model):

self.make = make

self.model = model

def drive(self):

print(f"{self.make} {self.model} is driving!")

car = Car("Toyota", "Corolla")

car.drive() # Output: Toyota Corolla is driving!

Inheritance

Inheritance allows a class to inherit attributes and methods from a parent class. This promotes code reuse

and organization.

class Parent:

def greet(self):

print("Hello from Parent!")

class Child(Parent):

def greet(self):

super().greet()

print("Hello from Child!")

child = Child()

child.greet()

# Output:

# Hello from Parent!

# Hello from Child!


Python Programming: Detailed Explanation

UNIT V

GUI Programming with Tkinter

Tkinter is Python's built-in library for creating graphical user interfaces (GUIs). It provides widgets like Labels,

Buttons, and Entry fields to build interactive applications.

from tkinter import Label, Button, Tk

def on_click():

print("Button clicked!")

root = Tk()

Label(root, text="Welcome to Tkinter!").pack()

Button(root, text="Click Me", command=on_click).pack()

root.mainloop()

Turtle Graphics

Turtle is a module for creating simple graphics, often used for educational purposes. It allows you to draw

shapes, lines, and patterns interactively.

from turtle import Turtle, Screen

t = Turtle()

s = Screen()

# Drawing a square

for _ in range(4):
Python Programming: Detailed Explanation

t.forward(100)

t.right(90)

s.mainloop()

You might also like