0% found this document useful (0 votes)
17 views2 pages

Python Programming: Datetime, Namespaces, and Functions

The document outlines key concepts in Python programming for a B.Sc. Computer Science course, focusing on the datetime module, namespaces and scope, the dir() function, and the reload() function. It includes definitions, examples, and practical applications of these concepts. Additionally, it provides exam tips for short and long answer questions related to the topics covered.

Uploaded by

babysmitha14
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)
17 views2 pages

Python Programming: Datetime, Namespaces, and Functions

The document outlines key concepts in Python programming for a B.Sc. Computer Science course, focusing on the datetime module, namespaces and scope, the dir() function, and the reload() function. It includes definitions, examples, and practical applications of these concepts. Additionally, it provides exam tips for short and long answer questions related to the topics covered.

Uploaded by

babysmitha14
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

Kerala University – [Link].

Computer Science (5th


Semester)
Paper: CS1543 – Programming in Python

1. datetime Module
The datetime module provides classes for manipulating dates and times. It helps in getting the
current date/time, formatting, comparing, and performing arithmetic on date and time values.

Important Classes:

1 date – Represents date (year, month, day)

2 time – Represents time (hour, minute, second, microsecond)

3 datetime – Combines date and time

4 timedelta – Represents difference (duration) between two dates/times

Example:

from datetime import datetime, timedelta now = [Link]() print("Now:", now)


print([Link]("%d-%m-%Y %H:%M:%S")) future = now + timedelta(days=5) print("After 5
days:", future)

Uses: Logging, scheduling, duration calculation, timestamping.

2. Namespaces and Scope


A namespace is a collection of names (identifiers) mapped to objects (variables, functions, etc.). It
ensures that names are unique and prevent conflicts.
Types of Namespaces:

1 Built-in – Contains names like print(), len()

2 Global – Variables defined at top level of a module

3 Enclosing – Scope of outer function in nested functions

4 Local – Variables inside a function

Scope: Scope defines where a variable is accessible. Python follows LEGB rule: Local →
Enclosing → Global → Built-in.

Example:

x = 10 # Global def outer(): y = 20 # Enclosing def inner(): z = 30 # Local print(x, y, z) inner() outer()

3. dir() Function
The dir() function returns a list of names (variables, functions, classes) defined in the current scope
or a specified module/object.
Syntax: dir([object])

Example:

print(dir()) import math print(dir(math))

Use: To explore contents of modules and for debugging or introspection.

4. reload() Function
The reload() function is used to reload a previously imported module without restarting the Python
interpreter. Useful after editing the module file.

Syntax: from importlib import reload

Example:

import mymodule from importlib import reload reload(mymodule)

Use: Helpful during debugging and module development.

✍■ Exam Tips:
1 Short answers: definitions and one example each for dir(), reload(), datetime.

2 Long answer: explain Namespaces and Scope with LEGB rule and example.

3 Practical viva: expected questions on dir() and reload().

You might also like