WWW Iitkirba Xyz
WWW Iitkirba Xyz
xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
www.iitkirba.xyz
Packages and Date/Time in
Python
www.iitkirba.xyz
Introduction to Python Packages
Code Reusability Organized Structure Extensive Functionality
Packages are collections of modules They help to organize and structure Packages offer a vast range of
that provide reusable code. your Python projects. functions and classes.
www.iitkirba.xyz
qf "'j ' ...,.... , • ..,,. Y rn 1 v 1 • <!,.' '-dtu yt :
=
' J Q r tJ.;JUCJ V I UHC I
random
json
Generating random numbers and sequences.
Working with JSON data (encoding, decoding).
www.iitkirba.xyz
•• •• Installing Third-Party
Packages
)_ @_
.
pip Package Index
Python's package installer. Repository of available packages
(PyPI).
www.iitkirba.xyz
Working with the datetime
Module
Current Time
Get the current date and time.
33:15
2 Date Components
Extract year, month, day, etc.
Time Components
3
Extract hour, minute, second, etc.
1
4- Date and Time Formatting
Customize output string format.
1
www.iitkirba.xyz
Date and Time Objects
datetime.date datetime.time datetime.datetime
Represents a date (year, month, day). Represents a time (hour, minute, Combines date and time information.
second).
www.iitkirba.xyz
Time Delta Calculations
Duration
1 Calculate the difference between two dates or times.
Adding Time
2 Add a time delta to a date or time object.
Subtracting Time
3 Subtract a time delta from a date or time object.
www.iitkirba.xyz
Formatting Dates and Times
strftime( )
Convert datetime objects to strings.
strptime( )
Parse date/time strings into datetime objects.
Custom Formats
Use format codes to create specific output.
www.iitkirba.xyz
Time Zones and Localization
1
Timezone
Represent time zones and conversions.
Localization
2
Adjust date and time formats for different regions.
www.iitkirba.xyz
Sample Code: Working
with Dates and Times
import datetime
www.iitkirba.xyz
CONTENTS
• ARRAYS AND ITS OPERATIONS
• HANDLING STRINGS AND CHARACTERS
• LIST OPERATIONS
www.iitkirba.xyz
• WHAT IS AN ARRAY ?
An array is a collection of items stored at contiguous
locations. In other words an array is a special variable
which can hold more than one value at a time.
ARRAY AND
ITS
OPERATIONS
www.iitkirba.xyz
DIFFERENT OPERATIONS IN ARRAYS :
1. append( )-Adds an element at the end of the list
2. clear( )-Removes all the elements from the list.
3. copy( )-Returns a copy of the list.
4. count( )-Returns the number of elements with the specified value.
5. extend( )-Adds the element to the end of the current list.
6. index( )-Returns the index of the first element with the specified value.
7. insert( )-Adds an element at the specified position.
8. pop( )-Removes the element at the specified position.
9. remove( )-Removes the first item with the specified value.
10. reverse( )-Reverses the order of the list.
11. sort( )-Sorts the lists.
www.iitkirba.xyz
HANDLING STRINGS AND CHARACTERS
• WHAT IS A STRING ?
A string is a sequence of characters enclosed in either single quotes ‘.’ or double quotes “.”. It is used
for representing textual data.
CREATING A STRING:
Strings can be created using either single quotes or double quotes.
For Example- s1=‘geeks for geeks’
s2=“geeks for geeks”
Multi-line Strings: If we need to span multiple lines then we can use triple quotes.
For Examples: s= “ “ “ I am learning
coding from youtube ” ” ”
www.iitkirba.xyz
OPERATIONS IN STRING:
Let us consider a string s=“HELLO”
Access Characters: s[0] gives ‘H’.
String Slicing: s[1:4] gives ‘ell’.
Concatenation: “Hi” + “there” gives “Hi there”.
METHODS: We have a string s,
s.upper( )- Converts to uppercase.
s.lower( )- Converts to lowercase
s.spilt(“ ”)- Spilts by spaces into a list.
“ ”.join([‘Hi’, ‘there’])- Joins list into a string.
www.iitkirba.xyz
LIST
OPERATIONS
• WHAT IS A LIST ?
A list is a built-in dynamic sized array that is
used to store an ordered collection of items.
We can store all types of items(including
another list) in a list. A list may contain mixed
type of items , this is possible because a list
mainly stores references at contiguous
locations and actual items maybe stored at
different location.
www.iitkirba.xyz
OPERATIONS IN LIST:
www.iitkirba.xyz
LIST METHODS :
1. append( )-Adds an element to the end of the list.
2. copy( )-Returns a shallow copy of the list.
3. clear( )-Removes all elements from the list.
4. count( )-Returns the number of times a specified element appears in the list.
5. extend( )-Adds elements from another list to the end of the current list.
6. index( )-Returns the index of the first occurrence of a specified element.
7. insert( )-Inserts an element at a specified position.
8. pop( )-Removes and returns the elements at the specified position(or the last element if no index is
specified).
1. remove( )-Removes the first occurrence of a specified element.
2. reverse( )-Reverses the order of the element in the list.
3. sort( )-Sorts the list in ascending order(by default).
www.iitkirba.xyz
ADDING ELEMENT
&
MUTABILITY IN
PYTHON
www.iitkirba.xyz
MAP()
The map() function applies a given function to every item in a list (or any iterable)
and returns a map object (aniterator) .
SYNTAX :
map(function, iterable)
EXAMPLE :
numbers =[ 1,2,3,4]
squared =map(lambda x:x **2,numbers) print(list(squared))
Output :[1,4,9,16]
www.iitkirba.xyz
FILTER()
The filter() function is used to filter items from an iterable based on a
condition (function that returns True or False) .
SYNTAX :
filter(function, iterable)
EXAMPLE :
• numbers =[1,2,3,4,5,6]
• even =filter(lambda x:x %2 ==0, numbers) print(list(even))
• Output :[2,4,6]
www.iitkirba.xyz
APPEND()
The append() method in Python is used to add a single item to the end of list. This method modifies the original list
and does not return a new list.
Parameter :
Element: The item to be appended to the list. This can be of any data type(integer, string, list, etc.) ,the parameter is
mandatory and omitting it can cause an error.
Return Type :
The append() method does not return any value, it just modifies the original list in place.
www.iitkirba.xyz
EXTEND()
The Python List extend() method adds items of an iterable (list, tuple, dictionary, etc) at the end of a list.
Return Type :
Python list sort() returns none.
www.iitkirba.xyz
COUNT()
The count() method is used to find the number of times a specific element occurs in a list. It is very useful in scenarios where
we need to perform frequency analysis on the data.
Syntax of count() method :
list_name.count(value)
Parameter :
list_name: The list object where we want to count an element.
value: The element whose occurrences need to be counted.
Return Type :
The count() method returns an integer value, which represents the number of times the specified element appears in the list.
www.iitkirba.xyz
INDEX()
List index() method searches for a given element from the start of the list and returns the
position of the first occurrence.
Syntax of count() method :
list_name.index(element, start, end)
Parameter :
element – The element whose lowest index will be returned.
start (Optional) – The position from where the search begins.
end (Optional) – The position from where the search ends.
Return Type :
Returns the lowest index where the element appears.
www.iitkirba.xyz
INSERT()
Python List insert() method inserts an item at a specific index in a list.
Return Type :
The insert() method returns None. It only updates the current list.
www.iitkirba.xyz
SORT()
The sort() method in Python is a built-in function that allows us to sort the elements of a list in ascending or
descending order and it modifies the list in place which means there is no new list created.
Parameter :
Key (Optional): This is an optional parameter that allows we to specify a function to be used for sorting. For
example, we can use the len() function to sort a list of strings based on their length.
Reverse (Optional): This is an optional Boolean parameter. By default, it is set to False to sort in ascending
order. If we set reverse=True, the list will be sorted in descending order.
www.iitkirba.xyz
REVERSE()
The reverse() method is an inbuilt method in Python that reverses the order of elements in a list.
Parameter :
It doesn’t take any parameters.
Return Type :
It doesn’t return any value.
www.iitkirba.xyz
REMOVE()
Python list remove() function removes the first occurrence of a given item from list.
Return Type :
The method does not return any value but removes the given object from the list.
www.iitkirba.xyz
CLEAR()
This method modifies the list in place and removes all its elements
Parameter :
The clear() method doesn't take any parameters.
Return Type :
The clear() method only empties the given list. It doesn't return any value.
www.iitkirba.xyz
POP()
The list pop() method removes the item at the specified index. The method also returns the removed item.
Syntax of reverse() method :
list_name.pop(index)
Parameter :
The pop() method takes a single argument (index).
The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).
If the index passed to the method is not in range, it throws IndexError: pop index out of range exception.
Return Type :
The pop() method returns the item present at the given index. This item is also removed from the list.
www.iitkirba.xyz