Nothing Special   »   [go: up one dir, main page]

RAC LA1 Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

An Autonomous Institution Affiliated to Visvesvaraya Technological University, Belagavi

Approved by UGC/AICTE/Govt of Karnataka, accredited by NAAC - A+ Grade, Programs Accredited by NBA (Tier-1)

DEPARTMENT OF ROBOTICS AND ARTIFICIAL INTELLIGENCE

Project Report for LA1

On

PYTHON PROGRAMMING
Submitted by

Lepakshi V I Sem

Under the guidance of

Dr. Kiran M C

Department of Robotics and AI


NMIT, Bangalore

Date: 3rd May 2024


1

INDEX

Chapter Title Page no.

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

8. Classes and Objects 9

9. Built-in Modules 9

10. Constructor and Inheritance 10

11. File Operators 11

12. Applications in real world 12

13. Conclusion 13
2

INTRODUCTION

Python is a popular programming language. It was created by Guido van


Rossum, and released in 1991 and further developed by the Python Software
Foundation. It was designed with an emphasis on code readability, and its
syntax allows programmers to express their concepts in fewer lines of
code.Python is a programming language that lets you work quickly and integrate
systems more efficiently.

There are two major Python versions: Python 2 and Python 3. Both are quite
different.

Python emphasizes code readability and simplicity, making it an excellent


language for beginners and experienced developers. Getting Started with
Python is easy due to its straightforward syntax and extensive documentation,
which provides ample resources for learning and development.

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

An operator is a symbol that tells the compiler that either a mathematical or a


logical manipulation has to be done. In this lab you will be studying about
the Arithmetic Operations.
They are of the following types :
Addition Operator ( + )
The addition operator is used to add two numbers. It is placed between two
numbers that are to be added.
Syntax : number1 + number2

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

Floor Division Operator ( // )


It is used to perform floor division. This gives the result in int
format. Syntax : number1 // number2
5

Built-in Functions

An executable program in a programming language contains multiple lines. To


simplify this code, various functions are used. They can be built-in functions or
user defined functions.A function is basically a chunk or module of code that
takes in some input from the user and may or may not give any output. The
function may provide some alterations to the input values.

'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

The execution of programming language codes is done by a compiler. A


compiler is given a set of codes or rather a sequence of codes that perform a
desired task. The task may or may not be repetitive but the compiler is smart
enough to process it. Such repetitive code is known as a 'loop'.

Loop is a sequential set of instructions which gets executed multiple times


to reduce minimize the repetition of code. In Python, we have two types of
loops :
i. for loop
ii. while loop

'For' loop :

Syntax :

for object in range(initialization, limit, update ):


statements
7

'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.

Data Type Character

i.List Mutable
ii.Tuple Immutable
iii.Dictionary Mutable

LIST

It is a collection of data in which date is stored in ordered form.


Syntax : object_name = [ elements ]
The above mentioned syntax is of list where the elements are mentioned
in square brackets.
8

Tuple

A tuple is a collection of data which is mostly similar to a list except that it is


immutable i.e. the value once stored in a tuple cannot be changed. Though
tuple allows concatenation. Syntax : object_name = ( elements )
This is the syntax of a tuple, where elements are placed under parenthesis.

Dictionary

Dictionary is a collection of data which is not sequential like a list,


thus indexing is not possible. It consists of a key and its value.
Example : dict1 = {1 : "Red", 2 : "Blue", 3 : "Green"}
The above syntax is for a dictionary where the elements are placed under curly
brackets.

String

String

We can access a string using indexing. In a string each character is assigned


with a unique index value which starts from 0. A string can be written in
both single quotes and double quotes. Example : 'Hello World' "Hello
World"

Program:

Output:

Hello
Hello
9

Classes And Objects

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

Syntax
class class_name
statements

How to define class objects

An Object is an instance of a Class. A class is like a blueprint while an


instance is a copy of the class with actual values. An object consists of :

State: It is represented by the attributes of an object. It also reflects


the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the
response of an object to other objects.
Identity: It gives a unique name to an object and enables one object to
interact with other objects

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

THE IMPORT STATEMENT

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

Constructor and Inheritance

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 :

Name of mode Keywords

Read R

Write W

Append A

Example :

Output : Hello!, My name is Jake.

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

In conclusion, Python is a popular programming language. It's used for many


things like web development, data science and scientific computing. It's easy to
learn and has many resources available.

You might also like