100% found this document useful (1 vote)
189 views

1 - Introduction To Python

This document provides an introduction to Python programming. It discusses why Python is a useful language, the topics that will be covered, and the intended audience. It defines Python as an interpreted, general-purpose, high-level programming language with easy syntax and dynamic semantics. It can be used for many domains like web development, robotics, and artificial intelligence. The document also discusses what compilers and interpreters are and how Python uses both approaches by compiling code to bytecode that is then interpreted.

Uploaded by

pdastagiri007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
189 views

1 - Introduction To Python

This document provides an introduction to Python programming. It discusses why Python is a useful language, the topics that will be covered, and the intended audience. It defines Python as an interpreted, general-purpose, high-level programming language with easy syntax and dynamic semantics. It can be used for many domains like web development, robotics, and artificial intelligence. The document also discusses what compilers and interpreters are and how Python uses both approaches by compiling code to bytecode that is then interpreted.

Uploaded by

pdastagiri007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

CHAPTER - 1

Introduction to Python
Programming

Neso Academy
Python Programming
Introduction
Topics
Why Python?
Syllabus
Target Audience
Why Python?
Open source Cross platform

Huge set of
Huge community support
libraries and tools
Basic Python

List, Dictionary,
Introduction and Tuple Functions Modules

Variables, Conditionals File Handling


Expressions, and Loops
and Statements
Intermediate Python

Exception Classes and Iterators and Sets and


Handling Objects Generators Command Line

Debugging Inheritance List/Dictionary


Comprehensions
Advanced Python

Multiprocessing Unit Testing Lambdas Context


Managers

Multithreading Decorators Regular


Expressions
Target Audience
✓ Absolute Beginners.
✓ Professional Programmers.
✓ Anyone willing to learn Python.
Python Programming
The Definition of Python
Topics
Python Definition
Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

Guido van Rossum


(Python Creator)
Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

Source Code Machine Code


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

Isn’t specialized to a specific domain.


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

Web Development Robotics Artificial Intelligence


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

length = 50 010110101010100010100010
breadth = 20 101010100101100100111110
area = length * breadth 101001111111101000001001
print(area) 010101010101010100101100

High-level code Low-level code


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

int x = 10; x = 10
int y = 5; y = 5
printf(“%d”, x+y); print(x+y)

C code Python code


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

Variables are dynamic objects


Python Definition
Python is an interpreted general-purpose (or multi-purpose) high-level
programming language with easy syntax and dynamic semantics.

int x = 10; x = 10
int y = 5; y = 5
printf(“%d”, x+y); print(x+y)
x = 10.78; x = 10.78

C code Python code

Statically typed Dynamically typed


Python Programming
What is a Compiler?
Topics
Why do we need a Translator?
What is a Compiler?
Why do we need a Translator?
o A computer program is written using a high-level language.

o Only humans are capable to understand these languages and not machines.

o Translator - converts a high-level language code to the binary language


code which machines can understand.
What is a Compiler?
A compiler is a complex piece of software whose job is to convert source code to
machine understandable code (or binary code) in one go.
Run

#include <stdio.h>
int main() { 101010100111110100010
Compiler 100100101010010010001
30
int sum, a = 10, b = 20;
sum = a + b; 010101010101001001010
printf(“%d”, sum); 100110010100101001001
} 001010101010101010010

Executable

My Machine Friend’s Machine


Compiling the given
C program in
Code::Blocks IDE
Click on Build
to Compile
Code is compiled
successfully
Executable
created
successfully
Run the executable by
double clicking to see
the output
Executable
has given the
output 30
Python Programming
What is an Interpreter?
Topics
What is an Interpreter?
What is a Compiler?
A compiler is a complex piece of software whose job is to convert source code to
machine understandable code (or binary code) in one go.
Run

#include <stdio.h>
int main() { 101010100111110100010
Compiler 100100101010010010001
30
int sum, a = 10, b = 20;
sum = a + b; 010101010101001001010
printf(“%d”, sum); 100110010100101001001
} 001010101010101010010

Executable

My Machine Friend’s Machine


Python Programming
Compiled vs Interpreted Languages
Topics
Pros and cons of Compiled and Interpreted Languages.
Hybrid approach.
Examples of Compiled, Interpreted, and Hybrid Languages.
Pros of Compiled Languages Pros of Interpreted Languages
o Private code. o Portable.
o Faster execution. o Easy debugging.
o Fully optimized.

Cons of Compiled Languages Cons of Interpreted Languages


o No portability. o Requires interpreter.
o Extra compilation step. o Slower.
o Public code.
Hybrid approach
o Best of both the worlds – compiler and interpreter.
o Code privacy from compilation and portability from interpretation.

Source code Compiler

Bytecode

Interpreter Interpreter Interpreter


(Windows) (Linux) (Mac)
Examples of Compiled, Interpreted, and Hybrid Languages
Compiled languages: C, C++, etc.
Interpreted languages: Javascript, PHP, etc.
Hybrid languages: Java, C#, Kotlin, etc.
Python Programming
Is Python Compiled or Interpreted?
Topics
Is Python Compiled or Interpreted?
Demo – How to see the bytecode
Is Python Compiled or Interpreted?
Short answer – Python is both compiled and interpreted language.

Source code
Compiler
(.py)

Bytecode (.pyc)

PVM PVM PVM


(Windows) (Linux) (Mac)
Creating a
New Folder for
Python
Programs
Creating a New
Folder for Python
Programs
Writing the Python
Program in Notepad
Saving the Python
Program as hello.py
Saving the Python Program as hello.py
Verifying
the type
of the
Program
Running the
Program in
cmd
Activating Python
Interactive Shell to
see the Byte Code
of the Program
Importing
py_compile module
and using compile
function to generate
the Byte Code
The Byte Code inside
the _pycache_
Folder of Python
Programs Folder
Verifying the
Type of the
Byte Code
Contents of the Byte
Code File
(Not Human-Readable)
Python Programming
Python and Visual Studio Code Installation
Topics
Need of installing the Python Interpreter
Steps to install the Python Interpreter
Steps to install the Visual Studio Code
Writing your first program in Visual Studio Code
Need of installing the Python Interpreter

Source code
Compiler
(.py)

Bytecode (.pyc)

PVM PVM PVM


(Windows) (Linux) (Mac)
Steps to install the Python Interpreter
o Visit the official website – python.org/downloads.
o Download the latest version of Python from the website.
o Open the executable and follow the instructions as shown.

Click on the link to


download Python.
Download the latest version of Python for Windows/Linux/macOS
Open the downloaded executable
Check the check box
Click on Customize
installation to
install Python
Click on Next to proceed
Check the
recommended
check box for
installation
Click on Install
Process of installation
is in progress
The setup was successful.
Click on Close button
Type python --version
to Check whether python is
installed successfully
Python 3.11.1 has been installed successfully
Steps to install the Visual Studio Code
o Visit the official website – code.visualstudio.com
o Download the latest version of visual studio code from the website.
o Open the executable and follow the instructions as shown.
Click on the link to
download Visual
Studio Code.
Download
for
Windows
Open the downloaded executable
Accept the
agreement
Confirm the
path and
click on Next
Click on
Next to
continue
Check on Create a
desktop icon and
click on Next
Click on Install
Uncheck Launch
Visual Studio Code
and click on Finish
Open Visual Studio
Code IDE and
create a New file
Choose Text File
in dialogue box
Click on Select a Language
Select Python to
create a new
python file
Write the
simple python
program
Save this file with name add.py in
python programs folder
Open the Terminal window
for execution of program
Terminal window is
opened successfully
For execution of
program type
python add.py
Execution
of program
is successful
Download required
extensions for one
more ways of
execution
Search for
python extension
For the first time you will observe
Install instead of Uninstall. Click
on Install for Automatic
Installation
Search for
code runner
extension
For the first time you will observe
Install instead of Uninstall. Click
on Install for Automatic
Installation
After successful
installation of code runner
we see run button to run
the program directly
Click on the run
button to execute
the program
Python Programming
Indentation in Python (Part 1)
Topics
Basics of Indentation
Working of Indentation
Rules for Indentation
Basics of Indentation
Indentation is the leading whitespace before any statement in Python.

Purposes:
o Improves readability. o Helps in indicating a block of code.

char* name = “Sam”; name = ‘Sam’


if (name == “Sam”) if name == ‘Sam’:
{ print(‘Hello Sam’)
printf(“Hello Sam!”);
}

C program Python program


Working of Indentation

name = ‘Sam’ name = ‘Sam’


if name == ‘Sam’: if name == ‘Sam’:
print(‘Hello Sam’) print(‘Hello Sam’)
else: else:
print(‘Hello dude’) print(‘Hello dude’)
print(‘How are you?’) print(‘How are you?’)

Without Indentation With Indentation


Rules for Indentation
Rule #1: Minimum one space is necessary to represent an indented statement.
Rule #2: The first line of Python code cannot have indentation.
Rule #3: Indentation is mandatory to define a block of code.
Rule #4: The number of space must be uniform.
Python Programming
Indentation in Python (Part 2)
Topics
Common Indentation Errors
Advantages and Disadvantages of Indentation
Common Indentation Errors

Error #1

if name == ‘Sam’: if name == ‘Sam’:


print(‘Hello Sam’) print(‘Hello Sam’)
print(‘How are you?’) print(‘How are you?’)
The wrong way The right way
Common Indentation Errors

Error #2

print(‘How are you?’) print(‘How are you?’)


The wrong way The right way
Common Indentation Errors

Error #3

if name == ‘Sam’: if name == ‘Sam’:


print(‘Hello Sam’) print(‘Hello Sam’)
print(‘How are you?’) print(‘How are you?’)

The wrong way The right way


Common Indentation Errors

Error #4

if name == ‘Sam’: if name == ‘Sam’:


print(‘Hello Sam’) print(‘Hello Sam’)
print(‘How are you?’) print(‘How are you?’)

The wrong way The right way


Advantages and Disadvantages of Indentation
Advantages:

o Better readability and identification of the block of code.


o Reduced lines of code.

Disadvantages:

o Code must be carefully indented specially in the large programs.


o Indentation can become tedious without using good code editors.
Neso Academy

You might also like