RAC LA1 Python
RAC LA1 Python
Approved by UGC/AICTE/Govt of Karnataka, accredited by NAAC - A+ Grade, Programs Accredited by NBA (Tier-1)
On
PYTHON PROGRAMMING
Submitted by
Lepakshi V I Sem
Dr. Kiran M C
INDEX
1. Introduction 2
2. Python Features 3
3. Arithmetic Operations 4
4. Built-in Functions 5
5. Loops 6
6. Data types 7
7. Strings 8
9. Built-in Modules 9
13. Conclusion 13
2
INTRODUCTION
There are two major Python versions: Python 2 and Python 3. Both are quite
different.
Python claims to strive for a simpler, less-cluttered syntax and grammar while
giving developers a choice in their coding methodology. In contrast to Perl's
"there is more than one way to do it" motto, Python embraces a "there should be
one—and preferably only one obvious way to do it." philosophy. In practice,
however, Python provides many ways to achieve the same task. There are, for
example, at least three ways to format a string literal, with no certainty as to
which one a programmer should use. Alex Martelli, a Fellow at the Python
Software Foundation and Python book author, wrote: "To describe something as
'clever' is not considered a compliment in the Python culture."
Python 3.11 claims to be between 10 and 60% faster than Python 3.10, and
Python 3.12 adds another 5% on top of that. It also has improved error
messages, and many other changes.
3
Python Features
1. Simple and Easy to Learn: Python has a simple syntax, which makes it
easy to learn and read. It’s a great language for beginners who are new to
programming.
2. Interpreted: Python is an interpreted language, which means that the Python
code is executed line by line. This makes it easy to test and debug code.
3. High-Level: Python is a high-level language, which means that it abstracts
away low-level details like memory management and hardware interaction. This
makes it easier to write and understand code.
4. Dynamic Typing: Python is dynamically typed, which means that you don’t
need to declare the data type of a variable explicitly. Python will automatically
infer the data type based on the value assigned to the variable.
5. Strong Typing: Python is strongly typed, which means that the data type of
a variable is enforced at runtime. This helps prevent errors and makes the code
more robust.
6. Extensive Standard Library: Python comes with a large standard library
that provides tools and modules for various tasks, such as file I/O, networking,
and more. This makes it easy to build complex applications without having to
write everything from scratch.
7. Cross-Platform: Python is a cross-platform language, which means that
Python code can run on different operating systems without modification. This
makes it easy to develop and deploy Python applications on different platforms.
8. Community and Ecosystem: Python has a large and active community,
which contributes to its ecosystem. There are many third-party libraries and
frameworks available for various purposes, making Python a versatile language
for many applications.
9. Versatile: Python is a versatile language that can be used for various
purposes, including web development, data science, artificial intelligence, game
development, and more.
4
Arithmetic Operations
Subtraction Operator ( - )
The subtraction operator is used to subtract two numbers. It is placed between
two numbers that are to be subtracted. The right placed number is subtracted
from the one that is placed at left.
Syntax : number1 - number2
Multiplication Operator ( * )
The multiplication operator is used to multiply two numbers. It is also
placed between the two numbers that are to be operated. Syntax : number1 *
number2
Division Operator ( / )
The division operator is used to divide two numbers. It is used between the
numbers that are to be operated.
Syntax : number1 / number2
Modulus Operation ( % )
It is used to give out the remainder of a division operation. It is also placed
between numbers. The right placed number divides the one on the left and the
remainder is given as output.
Syntax : number1 % number2
Exponent Operation ( ** )
It is used to perform exponential calculations. The right placed number acts
as the power.
Syntax : number1* *number2
Built-in Functions
'id' function :
This function returns the identity of an object. A identity has to be unique
and constant for a particular object during the lifetime. Syntax : id(object)
Program:
Output
1750812191152
'Type' function :
This function returns the data type of an object. It returns the following
data types :
i. Integer
ii. String
iii. Float
type() method returns class type of the argument(object) passed as parameter.
type() function is mostly used for debugging purposes.
Syntax : type(object)
Program:
6
Output:
<class 'dict'>
Loops
'For' loop :
Syntax :
'While' loop :
Syntax :
while expression:
statements
Data Types
Data Types
Before starting with arrays you must know the concept of mutability. An
object may be classified into two categories :
i. Mutable
ii. Immutable
Mutable are those objects whose value can be altered after assigning a
particular value. Immutable are those objects whose value can not be altered
after assigning of a value.
List And Dictionary are Mutable.
Tuples are Immutable.
i.List Mutable
ii.Tuple Immutable
iii.Dictionary Mutable
LIST
Tuple
Dictionary
String
String
Program:
Output:
Hello
Hello
9
Syntax
class class_name
statements
Syntax:
Object_name.class_name()
Modules in Python
To perform specific functions like add, subtract, power, square root, we need to
write two three lines of logic to make our task or function work properly. So we
have built in functions inside those modules which can straight away perform
those task by passing the values to calculate.
Some of the modules that are used in Python are : math, datetime, decimal,
operator, test, user, sets, etc.
10
We can use any Python source file as a module by executing an import statement
in some other Python source file. When the interpreter encounters an import
statement, it imports the module if the module is present in the search path. For
example, to import the module calculate.py, we need to put the following
command at the top of the script :
Program
Output
12
Inheritance is a feature that says if you define a new class giving a reference
of some other class then due to inheriting property of python your new class
will inherit all attributes and behavior of the parent class.
11
A Constructor is a special kind of method that have same name as the class in
python self variable do the same. It can be used to set the values of the
members of an object.
Syntax:
File Operation
File operations in any programming is very essential. Files are used to save and
transfer data. They can be of any form, a text file or a media file. They all
serve the same purpose. Here you will learn about the various methods of file
operations in Python programming language.
Python provides us with various file operations. A file can be created,
edited, read and even copied.
Creating a file
To create a file, we need an object to store the file in. Here the role of object
is to create a reference of the file.Syntax : object = open('file_name', 'mode')
After all the desired operations are done, you need to close the file as well.
This is done to save the file without properly. Syntax :
object.close()
12
While creating or opening a file, we need to enter the mode that we want it
to open in. Some of the modes are :
Read R
Write W
Append A
Example :
Python Applications
Python is known for its general-purpose nature that makes it applicable in
almost every domain of software development. Python makes its presence in
every emerging field. It is the fastest-growing programming language and can
develop any application.
13
CONCLUSION