DOC-20241219-WA0001.
DOC-20241219-WA0001.
DOC-20241219-WA0001.
PYTHON
PROGRAMMING
Submitted by- TUSHAR KUMAR Submitted to- DR. NEETU
College ID- 22IT53 SHARMA
(Mentor)
University Roll No- 22EEAIT054
Batch- I2 (III Semester)
Department of Computer Science and
Information Technology
OVERVIEW
• YOUNITY had organized an Industrial Training for the students of
Computer Science and engineering department branch Information
Technology to develop a sense of programming in python language.
• The training continued for 16 days consisting of a 1.5 hours session
everyday. The teachers were highly supportive and guided the students
throughout the training period.
• After the completion of training, a quiz was conducted to test the
knowledge of the students. The quiz was based on the topics covered
during the training.
CONTENTS
• Fundamentals of • Method and Inheritance
Language Constructors
• Basics of Python I • Polymorphism & Data
Abstraction
• Basics of Python II • Encapsulation
• Conditional Execution • Python Set Operations I
• Functions of programming • Python Set Operations II
• Iteration • File Operations
• Class & Object
DAY 1
Fundamentals of Programming
Language
Programming languages vary in the level of abstraction they
provide from the hardware. They can be classified into the
following categories:
• Low-level language
• Middle-level language
• High-level language
DAY 2
Introduction to Python
• Python is a versatile and popular high-level programming language known for
its simplicity, readability, and wide range of applications. It was created by
Guido van Rossum and first released in 1991. Python is an open-source
language, which means it's free to use and has a large and active community
of developers and users.
• It is a general purpose language, means can be used for a wide variety of task
from Web Development to Data analysis etc.
• Interpreted language:- : Python is an interpreted language, which means we
don't need to compile our code before running it.
• High-Level Language: Python's high-level nature abstracts many low-level
details, making it more user-friendly and productive for developers.
DAY 3
CONDITIONAL EXECUTION
1. if- Statement:- is used to execute a block of code only if a specified condition is True.
Example- if condition
#code to execute when the statement is true;
2. elif Statement:- allows us to execute one block of code when the condition is true and another block
whenever the condition is false.
Example- if condition
#code to execute when the statement is true;
else condition;
#code to execute when the statement is false;
3. if-elif-else Statement:- is used to check multiple conditions.
Example-if condition1:
# Code to execute when condition1 is True
elif condition2:
# Code to execute when condition2 is True
else:
# Code to execute when no conditions are True
DAY 4
Functions of Programming.
A function is a block of code that performs a specific task.
Suppose, we need to create a program to create a circle
and color it. We can create two functions to solve this
problem:
create a circle function
create a color function
Dividing a complex problem into smaller chunks makes our
program easy to understand and reuse.
• Types of function
• There are two types of function in Python programming:
• Standard library functions - These are built-in functions
in Python that are available to use.
SYNTAX FOR WRITING A FUNCTION IN
PYTHON
Nested Loops
Python programming language allows to use one loop inside another loop.
Following section shows few examples to illustrate the concept.
Output:-
# Prints all letters except 'e' and 's'
for letter in 'geeksforgeeks':
Current Letter : g Current
if letter == 'e' or letter == 's': Letter : k Current Letter : f
continue Current Letter : o Current
print('Current Letter :', letter) Letter : r Current Letter : g
Current Letter : k
Break Statement
The break statement in Python brings control out of the loop. Output:-
Current letter: e
for letter in 'geeksforgeeks':
# break the loop as soon it sees
'e'
# or 's'
if letter == 'e' or letter == 's':
break
# An empty loop
for letter in 'geeksforgeeks':
pass
print('Last Letter :', letter)
OUTPUT
LAST LETTER : S
DAY:-6
ITERATIONN IN PYTHON
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that we can traverse through all
the values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which
consist of the methods __iter__() and __next__().
ITERATOR VS ITERABLE:-
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which
we can get an iterator from.
All these objects have a iter() method which is used to get an iterator.
Example:-Get :- OUTPUT
Return an iterator from a tuple, and print each value: Apple
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple) Banana
Cherry
print(next(myit))
print(next(myit))
print(next(myit))
FOR EXAMPLE:-
STOPITERATION:-
To prevent the iteration from going on forever, we can use the StopIteration statement.
In the __next__() method, we can add a terminating condition to raise an error if the iteration is done a
specified number of times:
EXAMPLE:-
Stop after 20 iterations:
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)
DAY:-7
CLASSES AND OBJECT IN PYTHON:
CLASSES:-A class is a user-defined blueprint or prototype from which objects are created. Classes
provide a means of bundling data and functionality together. Creating a new class creates a new
type of object, allowing new instances of that type to be made. Each class instance can have
attributes attached to it for maintaining its state. Class instances can also have methods (defined by
their class) for modifying their state.
SAMPLE PROGRAM:-
ADVANTAGES
•Versatility: File handling in Python allows us to perform a wide range of operations, such as creating,
reading, writing, appending, renaming, and deleting files.
•Flexibility: File handling in Python is highly flexible, as it allows us to work with different file types (e.g. text
files, binary files, CSV files, etc.), and to perform different operations on files (e.g. read, write, append, etc.).
•User–friendly: Python provides a user-friendly interface for file handling, making it easy to create, read, and
manipulate files.
•Cross-platform: Python file-handling functions work across different platforms (e.g. Windows, Mac, Linux),
allowing for seamless integration and compatibility.
DISADVANTAGES
•Error-prone: File handling operations in Python can be prone to errors,
especially if the code is not carefully written or if there are issues with the
file system (e.g. file permissions, file locks, etc.).
•Security risks: File handling in Python can also pose security risks,
especially if the program accepts user input that can be used to access or
modify sensitive files on the system.
•Complexity: File handling in Python can be complex, especially when
working with more advanced file formats or operations. Careful attention
must be paid to the code to ensure that files are handled properly and
securely.
•Performance: File handling operations in Python can be slower than
other programming languages, especially when dealing with large files or
performing complex operations.
DAY 14:-
ACCESS MODES IN FILE HANDLING:-
In Python, there are six methods or access modes, which are:
1.Read Only ('r’): This mode opens the text files for reading only. The start of the file is where the
handle is located. It raises the I/O error if the file does not exist. This is the default mode for opening
files as well.
2.Read and Write ('r+’): This method opens the file for both reading and writing. The start of the file
is where the handle is located. If the file does not exist, an I/O error gets raised.
3.Write Only ('w’): This mode opens the file for writing only. The data in existing files are modified
and overwritten. The start of the file is where the handle is located. If the file does not already exist
in the folder, a new one gets created.
4.Write and Read ('w+’): This mode opens the file for both reading and writing. The text is
overwritten and deleted from an existing file. The start of the file is where the handle is located.
5.Append Only ('a’): This mode allows the file to be opened for writing. If the file doesn't yet exist, a
new one gets created. The handle is set at the end of the file. The newly written data will be added
at the end, following the previously written data.
6.Append and Read (‘a+’): Using this method, we can read and write in the file. If the file doesn't
already exist, one gets created. The handle is set at the end of the file. The newly written text will be
DAY 15:-
OPERATIONS IN FILE HANDLING
1.CREATING A FILE:
"x" – Create: this command will create a new file if and only if there is no file already in existence with that
name or else it will return an error.
4. Closing a file:-
It is good practice to always close the file when you are done
with it.
f = open("myfiles.txt", "r") print(f.readline()) f.close()
THANK
YOU