Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 69

SCHOOL OF ENGINEERING &TECHNOLOGY

D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Application Programming in Python


Subject Code: MCA-275

Faculty: Dr. Rajneesh Kumar Singh


Assistant Professor
Department of Computer Science
& Applications
Sharda School of Engineering &
Technology, Sharda University
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Index
 University Mission and Vision
 Department Mission and Vision
 Course Outcomes (To be discussed interactively with the students) and Program
Outcome
 Evaluation methods (As finalized in the meeting of subject coordinator (A))
 List of students
 Attendance criteria
 Syllabus of the subject (Course Credits, LTP, Reference Books and E-Material for
the subject)
 Academic Calendar
 Scope of the subject in Computer Science & Engineering, its prerequisite and other
related subjects.
 Importance of the subject in Industry/Entrepreneurial Ventures
 About yourself and your area of research
 Any other required material to be show to the students
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Vision and Mission of the University


Vision of the University
 To serve the society by being a global University of higher learning in pursuit of
academic excellence, innovation and nurturing entrepreneurship.

Mission of the University


 Transformative educational experience
 Enrichment by educational initiatives that encourage global outlook
 Develop research, support disruptive innovations and accelerate
entrepreneurship
 Seeking beyond boundaries
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Vision and Mission of the Department of Computer


Science & Applications
Vision
 To emerge as a world-class department and be recognized as the Centre of
Excellence for equipping intellectual professionals with the requisite skills as per
Industry standards.

Mission
 To promote a technical education for designing technological applications and
innovative ideas that contributes to the sustainable development of societies.
 To foster the culture of entrepreneurship and prepare students for productive careers
in industry and academia.
 To facilitate industry-academia collaboration in order to solve interdisciplinary
societal problems.
 To inculcate transformative, holistic and value-based immersive learning experiences
to solve complex technological problems of modern society.
SCHOOL OF ENGINEERING & TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

PO’s and PSO’s of Computer Science & Application


SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Evaluation methods
CA
S. No. Marks Remarks
Components
1 Quiz 10 5 Quizzes

Five Evaluations per student uniformly


distributed over the Semester as per
Classroom Academic Calendar
Participation/
2 5 Oral Questioning on understanding in
MOOC/
Assignment lecture, Group Participation on topic,
Demonstration/problem solving on
white board, Peer Reviews

Implement at least 02 case studies by


3 Case Study 10
using python programming concepts.
Total CA Marks 25
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Evaluation Method Conti..


Subject Code-MCA275
Subject CA MTE ETE
Weightage
Distribution 25% 25% 50%

• Max. 5 quizzes will be conducted


• Assignments
• Project/ Case Study/ Presentation
• Regular Class Performance
• Certification/MOOC course based Evaluation
Subject Code-MCP173

Subject CA VIVA ETE


Weightage 30% 30% 40%
Distribution

• Regular Lab Performance


• Experiment Evaluation
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Attendance criteria

Attendance Criteria
End-Term
Continuous Mid-Term Examinatio
Assessment Examination n
Mandatory – Mandatory- Mandatory-
Attendance 75% 75 % 75 %
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Attendance criteria Conti…


As you all are aware that while a minimum of 75% attendance is required in
each course to be eligible to appear for the End Semester
Examination, Students are expected to have 100% attendance.
Please find the attendance requirement rule mentioned below:
• Attendance will be monitored course-wise.
• A student will be required to have at least 75% attendance in a course, to
be eligible to appear in Mid-term Examination (MTE) and End-term
Examination (ETE).
• Irrespective of whether a student has registered late or on time,
calculation of attendance shall be based on all the classes held, and not
from the date on which the student has registered, unless otherwise
decided by the Registrar in the case of fresh entrants only.
Note: No attendance will be marked for medical cases (it will be covered
under 25% only(100-25=75), in other words 75% attendance is mandatory
for MTE & ETE).
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Syllabus
Application Programming in Python (MCA275)
Course Outcomes
After the completion of this course, students will be able to:

CO1. Understand the concepts of Python lists, tuples, dictionaries and define the
functions to construct the programs.
CO2. Demonstrate the programs by using Python libraries, frameworks, and
exception-handling.
CO3. Implement the Object-oriented programming (OOP) concepts and file
operations.
CO4. Apply the database connectivity concept and able to work with databases by
using python programming.
CO5. Understand and apply the web development concepts.
CO6. Design real-life applications by using python programming concepts.
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Detailed Syllabus: L-T-P-C----4-0-2-5


Outline syllabus CO
Mapping
Unit 1 Introduction CO1, CO6
A Introduction: History, Python architecture, Variables, Data Types,
Operators.

B Python Syntax: Comments, variables, conditional statements, loops. CO1, CO6

C Datatypes: Numbers, strings, list, tuples, sets, dictionaries. CO1, CO6


Functions: Defining a function, Calling a function, Types of functions,
Function Arguments, Anonymous functions, Global and local
variables
Unit 2 Libraries and Frameworks
A Libraries : Introduction and working with Numpy, Pandas, CO2, CO6
matlplotlib, scipy etc.
B Frameworks: Flask and Django CO2, CO6
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Outline syllabus CO
Mapping

C Exception Handling: Definition Exception, Exception handling, CO2,CO6


Except clause, Try, catch and finally clause, User Defined Exceptions

Unit 3 Object oriented programming CO3,


CO6
A OOPs concept : Class and object, Attributes, Inheritance CO3,
CO6

B Overloading, Overriding, Data hiding CO3,


CO6

C Python File Operation: Opening, Closing, Reading, Writing CO3,


operation into files. Manipulating File Pointer CO6
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Unit 4 Database Handling CO4, CO6


A Python Database Interaction: SQL Database connection using CO4, CO6
python, Creating and searching tables.
B Reading and storing config information on database. CO4, CO6

C Programming using database connections. CO4, CO6


Minor Project with 1st case study(10)

Unit 5 Modules, Email Processing CO5, CO6


A CO5,CO6
Modules: Importing module, Math module, Random module,
Matplotlib, Packages.
B CO5,CO6
Contacting User Through Emails Using Python: Installing SMTP
python module, Sending email.
C CO5,CO6
Reading from file and sending emails to all users addressing them
directly for marketing.
Minor Project with 2nd case study(10)
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Text book/s* 1. The Complete Reference Python, Martin C. Brown,


McGrwHill

Other References 1. Introduction to computing in problem solving using Python,


E Balagurusamy, McGrawHill
2. Introduction to programming using Python, Y. Daniel Liang,
Pearson
3. Mastering Python, Rick Van Hatten, Packet Publishing House
4. Starting out with Python, Tony Gaddis, Pearson
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Scope of the subject in Computer Science & Engineering, its prerequisite and other
related subjects.

 Data Science and Machine Learning: Python is widely used for data analysis,
data visualization, and machine learning tasks, due to the availability of
powerful libraries and frameworks.
 Artificial Intelligence and Deep Learning: Python is the most popular language
for AI and deep learning, with libraries such as TensorFlow and PyTorch
making it easy to build complex neural network models.
 Web Development: Python has a strong presence in web development, with
popular web frameworks such as Django and Flask that make it easy to build
web applications.
 Scripting and Automation: Python’s simplicity and ease of use make it a
popular choice for scripting and automation tasks.
 Internet of Things (IoT): IoT development is made easier with Python’s
versatility and ease of use, with a wide variety of libraries and frameworks
available.
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Importance of the subject in Industry/Entrepreneurial Ventures

 Python can run on multiple platforms, including


Windows, macOS, and Linux. Also, you can choose it if
you are planning to build the latest or new operating
system.
 Python is the ideal choice for creating a contemporary
AI/ML application.
 Python is ideal for image processing and graphic design
jobs. As a result, gaming apps in Python are well-
developed.
 Projects requiring Develops scripting can benefit from
Python.
 Python is a great tool for data scientists.
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Importance of the subject in Industry/Entrepreneurial Ventures

Top Companies In India Using Python

In India, many companies use Python to develop applications,


analyse data, and automate processes. Here are some of the top
companies in India that are using Python:

• TCS • Cognizant
• Infosys • Accenture
• Wipro • Capgemini
• HCL • Deloitte India
• IBM India • Amazon India
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Dr. Rajneesh Kumar Singh


Assistant Professor & Program Coordinator
rajneesh.singh@sharda.ac.in
Experience: 18+ Years
Research Area: My research interests are in modeling, analysis, and mitigation of various
impairments in Wireless Nanosensor Networks and Nano Communication.
Qualification
PhD (CSE), M. Tech (CSE), MCA
Award & Recognition
• Received a token of appreciation by the Management of ITS Institute, Greater Noida, to
delivering a good project “Students Feedback System”.
• Achieved Best faculty award two times by ITS Management
• Achieved two times appreciation by Director (GCET) for Best Students Feedback
• Invited as Session Co-Chair for several International Conferences.
• Reviewer of various International Conferences and Session Chair for several International
Conferences
• Reviewer of various SCI Indexed Journals.
Research: More than 7 SCI / Scopus Indexed Journal, 12 International Scopus Indexed
Conferences and Book Chapters, and 02 patents (01 International Granted)
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S

Time Table
What is Python…?

• Python is a general purpose programming language that is


often applied in scripting roles.
• So, Pythonis programming language as
well as scripting language.
• Python is also called as Interpreted language

2
2
What is Python?
Python is a popular programming
language. It was created by Guido
van Rossum( Netherlands), and
released in 1991.
It is used for:
1.Web development (server-side)
2.Software development
3.Mathematics
4.system scripting.

Guido Van Rossum


History
• Python laid its foundation in the late 1980s.
• The implementation of Python was started in the December 1989 by Guido Van
Rossum at CWI in Netherland.
• Guido Van Rossum is fan of ‘Monty Python’s Flying Circus’, this is a famous TV
show in Netherlands
• In February 1991, van Rossum published the code (labeled version 0.9.0).
• In 1994, Python 1.0 was released with new features like: lambda, map, filter, and
reduce.
• Python 2.0 added new features like: list comprehensions, garbage collection
system.
• On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was
designed to rectify fundamental flaw of the language.
• ABC programming language is said to be the predecessor of Python language,
which was capable of Exception Handling and interfacing with the Amoeba
Operating System.
• The following programming languages influence Python:
• ABC language. 2
4
• Modula-3
Python 1.0 January 1994
Versions
Python 1.5 December 31, 1997
Python 1.6 September 5, 2000
Python 2.0 October 16, 2000
Python 2.1 April 17, 2001
Python 2.2 December 21, 2001
Python 2.3 July 29, 2003
Python 2.4 November 30, 2004
Python 2.5 September 19, 2006
Python 2.6 October 1, 2008
Python 2.7 July 3, 2010
Python 3.0 December 3, 2008
Python 3.1 June 27, 2009
Python 3.2 February 20, 2011
Python 3.3 September 29, 2012
Python 3.4 March 16, 2014
Python 3.5 September 13, 2015
Python 3.6 December 23, 2016
Python 3.7 June 27, 2018
Python 3.8 October 2019
Python 3.9 October 2020
Python 3.10 October 2021 2
Python 3.11 October 2022 5
Python 3.12 October 2023
program and scripting language

Program Scripting
• a program is executed (i.e. the • a script is interpreted
source is first compiled, and the
result of that compilation is • A "script" is code written in
expected) a scripting language. A
scripting language is nothing
• A "program" in general, is a
sequence of instructions but a type of programming
written so that a computer can language in which we can
perform certain task.
write code to control
another software 26
application.
What can Python do?
1. Python can be used on a server to create
web applications.
2. Python can be used alongside software to
create workflows.
3. Python can connect to database systems. It
can also read and modify files.
4. Python can be used to handle big data and
perform complex mathematics.
5. Python can be used for rapid prototyping, or
for production-ready software development.
Why Python?
1. Python works on different platforms (Windows,
Mac, Linux, Raspberry Pi, etc).
2. Python has a simple syntax similar to the English
language.
3. Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.
4. Python runs on an interpreter system, meaning
that code can be executed as soon as it is written.
This means that prototyping can be very quick.
5. Python can be treated in a procedural way, an
object-oriented way or a functional way.
Python Syntax Compared to other Programming Languages

 Python was designed for readability and has some similarities to


the English language with influence from mathematics.

 Python uses new lines to complete a command, as opposed to


other programming languages which often use semicolons or
parentheses.

 Python relies on indentation, using whitespace, to define scope;


such as the scope of loops, functions, and classes. Other
programming languages often use curly brackets for this
purpose.
The Genesis of Python

Programming languages can be classified into the below three categories:

Low-level programming languages: It involves communication with the


system in binary format , consisting of 0's and 1's, which can be directly
executed. These languages provide instructions that can be used to perform
low-level operations such as memory management. An example of such
language is Assembly Language.

High-level programming language: In this, the communication with system


occurs in the form of text (usually in English), which is easily readable and
understandable by humans. Examples of such Languages include Ada,
JavaScript, Python, Ruby etc.

Middle-level programming languages: These are the languages which


support both low-level and high-level features. These are also written in human
readable text. They typically use tools like compilers to convert the human
readable instructions (high-level language constructs) into low-level executable
code (usually in binary format). Eg: C, C++, Java, etc.
Python Applications
Python is known for its general purpose nature that makes it
applicable in almost each domain of software development.
Python as a whole can be used in any sphere of development.

3
1
Python Applications
1) Web Applications
We can use Python to develop web applications. It provides libraries to
handle internet protocols such as HTML and XML, JSON, Email
processing, request, beautifulSoup, Feedparser etc. It also provides
Frameworks such as Django, Pyramid, Flask etc to design and delelop
web based applications.

2) Desktop GUI Applications


Python provides Tk GUI library to develop user interface in python based
application. Some other useful toolkits wxWidgets, Kivy, pyqt that are
useable on several platforms. The Kivy is popular for writing multitouch
applications.

3) Software Development
Python is helpful for software development process. It works
3
2 as a
support language and can be used for build control and management,
testing etc.
Python Applications
4) Scientific and Numeric
Python is popular and widely used in scientific and numeric computing. Some
useful library and package are SciPy, Pandas, IPython etc. SciPy is group of
packages of engineering, science and mathematics.

5) Business Applications
Python is used to build Business applications like ERP and e-commerce systems.
Tryton is a high level application platform.

6) Audio or Video based Applications


Python is awesome to perform multiple tasks and can be used to develop
multimedia applications. Some of real applications are: TimPlayer, cplay etc.

7) 3D CAD Applications
To create CAD application Fandango is a real application which provides full
features of CAD. 3
3
Python Features
Python provides many useful features which make it popular and valuable from
the other programming languages. It supports object-oriented programming,
procedural programming approaches and provides dynamic memory allocation.

1) Easy to Learn and Use


Python is easy to learn as compared to other programming languages. Its syntax
is straightforward and much the same as the English language. There is no use
of the semicolon or curly-bracket, the indentation defines the code block. It is
the recommended programming language for beginners.

2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple example,
the hello world program you simply type print("Hello World"). It will take only
one line to execute, while Java or C takes multiple lines.

3) Interpreted Language
Python is an interpreted language; it means the Python program 34is executed
one line at a time. The advantage of being interpreted language, it makes
debugging easy and portable.
Python Features
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX,
and Macintosh, etc. So, we can say that Python is a portable language. It
enables programmers to develop the software for several competing platforms
by writing a program only once.

5) Free and Open Source


Python is freely available for everyone. It is freely available on its official
website www.python.org. It has a large community across the world that is
dedicatedly working towards make new python modules and functions. Anyone
can contribute to the Python community. The open-source means, "Anyone can
download its source code without paying any penny."

6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects
come into existence. It supports inheritance, polymorphism, and encapsulation,
3
5

etc. The object-oriented procedure helps to programmer to write reusable code


and develop applications in less code.
7) Extensible
Python Features
It implies that other languages such as C/C++ can be used to compile the
code and thus it can be used further in our Python code. It converts the
program into byte code, and any platform can use that byte code.

8) Large Standard Library


It provides a vast range of libraries for the various fields such as machine
learning, web developer, and also for the scripting. There are various
machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras, and
Pytorch, etc. Django, flask, pyramids are the popular framework for Python
web development.

9) GUI Programming Support


Graphical User Interface is used for the developing Desktop application.
PyQT5, Tkinter, Kivy are the libraries which are used for developing the web
application.
3
6
Python Features
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python
runs code line by line like C,C++ Java. It makes easy to debug the code.

11. Embeddable
The code of the other programming language can use in the Python source
code. We can use Python source code in another programming language as
well. It can embed other language into our code.

12. Dynamic Memory Allocation


In Python, we don't need to specify the data-type of the variable. When we
assign some value to the variable, it automatically allocates the memory to the
variable at run time. Suppose we are assigned integer value 15 to x, then we
don't need to write int x = 15. Just write x = 15.
3
7
Python Implementations
Python Implementations

Python is an open source programming language. Python’s


interpreter has a reference implementation, “cpython” written in C,
and that source code is available to read.

Other Python implementations have different interpreters (not


written in C). For example, Jython runs on the Java VM and Iron
Python runs on the .NET CLR.

Many Python libraries are written as C extensions. They are a thin


Python API around some well-written C library. For example,
MySQLdb calls the mysql.so client library to connect to MySQL.
3
8
Compiler Vs Interpreter
Compiler Vs Interpreter
In the system both the compiler and interpreter are the same they
convert high-level code to machine code. The interpreter converts
source code into the machine when the program runs in a system
while a compiler converts the source code into machine code before
the program runs in our system.

3
9
Compiler Vs Interpreter
Compiler Interpreter
The compiler is faster, as conversion The interpreter runs slower as the
occurs before the program executes. execution occurs simultaneously.

Errors are detected during the Errors are identified and reported
compilation phase and displayed
before the execution of a program. during the given actual runtime.

Interpreted code is more portable as


Compile code needs to be recompiled it can run on any machine with the
to run on different machines. appropriate interpreter.

It requires more memory to translate It requires less memory than


the whole source code at once. compiled ones.

Debugging is more complex due to Debugging is easier due to40 the line-
batch processing of the code. by-line execution of a code.
Python Architectures/Internal working of Python
Python doesn’t convert its code into machine code, something that
hardware can understand. It converts it into something called byte
code. So within Python, compilation happens, but it’s just not in a
machine language. It is into byte code (.pyc or .pyo) and this byte code
can’t be understood by the CPU. So we need an interpreter called the
Python virtual machine to execute the byte codes.

4
1
Python Architectures/Internal working of Python

Source code extension is .py


Byte code extension is .pyc (compiled python code)

4
2
Python Architectures/Internal working of Python
Python Source Code Converted into Executable Code
The Python source code goes through the following to generate an
executable code

•Step 1: The Python compiler reads a Python source code or


instruction in the code editor. In this first stage, the execution of the
code starts.

•Step 2: After writing Python code it is then saved as a .py file in our
system. In this, there are instructions written by a Python script for the
system.

•Step 3: In this the compilation stage comes in which source


4
3 code is
converted into a byte code. Python compiler also checks the syntax
error in this step and generates a .pyc file.
Python Architectures/Internal working of Python
•Step 4: Byte code that is .pyc file is then sent to the Python Virtual
Machine(PVM) which is the Python interpreter. PVM converts the
Python byte code into machine-executable code and in this interpreter
reads and executes the given file line by line. If an error occurs during
this interpretation then the conversion is halted with an error message.

•Step 5: Within the PVM the bytecode is converted into machine code
that is the binary language consisting of 0’s and 1’s. This binary
language is only understandable by the CPU of the system as it is
highly optimized for the machine code.

•Step 6: In the last step, the final execution occurs where the CPU
executes the machine code and the final desired output will
4 come as
4
according to your program
Two ways to run a Program

• The instructions (called source code) written


in Python programming language can be executed in two
modes:

• Interactive mode: In this mode, lines of code are directly


written and executed in the Python interpreter shell, which
instantaneously provides the results.

• Normal or script mode: In this mode the source code is saved


to a file with .py extension, and the file is executed by
the Python interpreter.

The Python program statements have to be entered and saved
in a file through a text editor, and then the file has to be
executed to produce results.
Two ways to run a Program

Interactive mode:
Python provides us the feature to execute the Python statement one by
one at the interactive prompt. It is preferable in the case where we are
concerned about the output of each line of our Python program.

 open the interactive mode, open the terminal (or command prompt)
and type python
 It will open the following prompt where we can execute the Python
statement and check their impact on the console.
Two ways to run a Program

Here, we get the message "Hello World !" printed on the console.
Two ways to run a Program

Using a script file (Script Mode Programming)


• The interpreter prompt is best to run the single-line statements of
the code. However, we cannot write the code every-time on the
terminal. It is not suitable to write multiple lines of code.
• Using the script mode, we can write multiple lines code into a file
which can be executed later. For this purpose, we need to open an
editor like notepad, create a file named and save it with .py
extension, which stands for "Python". Now, we will implement the
above example using the script mode.
Two ways to run a Program

Step - 1: Open the Python interactive shell, and click "File" then choose "New", it will
open a new blank script in which we can write our code.
Two ways to run a Program

• Step - 3: After saving the code, we can run it by clicking


"Run" or "Run Module". It will display the output to the shell.
Basic structure of Python program

• Python programs are made up of statements, which are lines of


code that tell the computer to perform a specific task. Statements
can be grouped together into blocks, which are indented sections
of code.
• The basic structure of a Python program is as follows:
• Python
Basic structure of Python program

• Import statements: Import statements allow you to use modules,


which are libraries of code that can be used to perform common
tasks. For example, you might import the math module to use
math functions like sin() and cos().
• Functions: Functions are reusable blocks of code that can be used
to perform specific tasks. For example, you might define a
function to calculate the area of a triangle.
• Main code: The main code is the code that is executed when you
run your Python program. This is where you put all of the code
that you want to run.
Basic Structure of Python Program

Python is a high-level, interpreted programming language that is easy to


learn and use. It has a simple and easy-to-understand syntax that emphasizes
readability and reduces the cost of program maintenance.

The basic structure of a Python program consists of the following


components:

Comments: Comments are used to explain the purpose of the code or to


make notes for other programmers. They start with a ‘#’ symbol and are
ignored by the interpreter.

Import Statements: Import statements are used to import modules or


libraries into the program. These modules contain predefined functions that
can be used to accomplish tasks.

Variables: Variables are used to store data in memory for later use. In Python,
variables do not need to be declared with a specific type.
Basic Structure of Python Program

Functions: Functions are used to group a set of related statements together


and give them a name. They can be reused throughout a program.

Classes: Classes are used to define objects that have specific attributes and
methods. They are used to create more complex data structures and
encapsulate code.

Exceptions: Exceptions are used to handle errors that may occur during the
execution of a program.

Overall, the basic structure of a Python program consists of these


components working together to accomplish a specific task or solve a
particular problem.
Basic Structure of Python Program

# This is a comment

# Using if-else statement


import random # Importing a module
if z:
print(y)
# Defining variables else:
x = 10 print(result)
y = "Hello, Python!"
z = True # Defining a function
def greet(name):
print("Hello, " + name + "!")
# Performing arithmetic operation
result = x + 12 # Using the function
greet("Rajneesh")
Output:
Hello, Python!
Hello, Rajneesh!
Tokens

Tokens:
• The tokens can be defined as a punctuator mark, reserved words, and
each word in a statement.
• The token is the smallest unit inside the given program.

Tokens
The smallest distinct element in a Python program is called a
token. Tokens are used to construct each phrase and command in a
program. The different Python tokens include:

• There are following tokens in Python:


• Keywords.
• Identifiers.
• Literals.
• Operators.
Tokens

Keywords
In a computer language, keywords are English words with particular
importance or meaning. We cannot utilize them for any arbitrary
reason, including as names for functions or variables. They are
utilized for their unique qualities. The 33 keywords in Python
include, among others: try, break, continue, except, or, False, True,
class, and, as, assert, print, import, raise, while, for, in, not, if, elseif,
etc.

Identifiers
The names assigned to any variable, class, function, list, method,
etc., for their recognition are known as identifiers.
Tokens

Literals or Values
The constant values or data objects utilized in a source program are known as
literals. Various types of literal are supported by Python, including:
String Literals: The Python string literals are represented by text enclosed in
solo, double, or triple quotations. Examples include "Computer Programming,"
"sigma," etc. Multi-line phrases can also be written using triple quotations.
Character Literals: Another string literal is called the character literal, which
encloses the letter in single or double quotes.
Numeric Literals: These are literals that have been written as numbers. Python
supports the following three numerical literals:
 Integer Literal: Contains 0 as well as positive and negative values. There are
no fractional pieces included. In addition, literals in binary, decimal, octal,
and hexadecimal are permitted.
 Float Literal: It contains real numbers in the positive and negative ranges.
Fractional portions are also a part of it.
 Complex Literal: This contains the numerals a+bi, where a stands for the
fundamental component and b for the complex component.
Tokens

Boolean Literals
In Python, Boolean tokens only have two possible values. Every one of
these is true or false.
Special Literals
The special literal "None" in Python exists. It is employed to signify
emptiness, the lack of values, or nothingness.
Literals Collections
List, dictionary, tuple, and sets are examples of Python literal collections.
List: It is a comma-separated list of components enclosed in square
brackets. These elements can be of any data type and can be changed.
Tuple: In round brackets, this is similar to a list having comma-separated
items or values. The values are fixed and can have any Python data type.
Dictionary: This data structure is an unordered set of key-value
combinations.
Set: It is the group of components enclosed in curly braces, "{}"
Tokens

Operators
These represent the tokens in an expression in charge of carrying out
an operation. Operands are the elements to which an operation is
executed. Operators come in binary and unary forms. Unary
operators operate on a single argument, such as complementing and
others. At the same time, the operands for binary operators require
two.
Variable

Variables
Vary + able
A variable is the name given to a memory location. A value-holding
Python variable is also known as an identifier.

Since Python is an infer language that is smart enough to determine


the type of a variable, we do not need to specify its type in Python.

Variable names must begin with a letter or an underscore, but they


can be a group of both letters and digits.

The name of the variable should be written in lowercase. Both a and


A are distinct variables.
Variable

name = "Rajneesh"
age = 41.5
address = "Gr Noida"
print(name)
print(age)
print(address)

Multiple Assignment:
Multiple assignments, also known as assigning values to multiple variables in a
single statement, is a feature of Python.

1. Assigning single value to multiple variables


x=y=z=50 Output:
print(x) 50
print(y) 50
50
print(z)
Variable

2. Assigning multiple values to multiple variables:

a,b,c=5,10,15
print (a)
print (b)
print (c)

Output:
5
10
15
Variable: Unpack A Collection
• Python allows us to extract the values
into variables. This is called unpacking.
• Example:--
• fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Variable

Variable Types
There are two types of variables in Python - Local variable and Global
variable.
Local Variable
The variables that are declared within the function and have scope within
the function are known as local variables.
Example -
# Declaring a function
def add():
# Defining local variables. They has scope only within a function
a = 20
b = 30
c=a+b
Output:
print("The sum is:", c)
The sum is: 50
add()
Variable

Global Variables
Global variables can be utilized all through the program, and its
extension is in the whole program. Global variables can be used
inside or outside the function.

By default, a variable declared outside of the function serves as the


global variable. Python gives the worldwide catchphrase to utilize
worldwide variable inside the capability. The function treats it as a
local variable if we don't use the global keyword.
Variable

# Declare a variable and initialize it


x = 101

# Global variable in function


def mainFunction():
# printing a global variable
global x
print(x)
# modifying a global variable
x = 'Welcome to global variable'
print(x)

mainFunction()
print(x)
Input a value for the user in Python

A value can be input from the user with the help of input()
method the syntax is as follow :

variable_name = input(“Enter any number”)


input method return a string.
It can be changed to other datatypes by using type.
e.g.
age = int(input(“Enter your age”))
percentage= float(input(“Enter you percentage”))
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D E N G I N E E R I N G

THANK
YOU

You might also like