National Institute of Electronics & Information Technology
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of
India
Contents to be covered
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
1. Introduction to Python
2. Varieties of Python
3. Installation of Python
4. Setting Environment Variable
5. Basic Data Types
6. First Python Program
• It was created by Guido van Rossum in Netherland, and released in
1991.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• The name "Python" was adopted from the Rossum’s
favourite
comedy series "Monty Python's Flying Circus".
• It is a high level general purpose programming language.
• Itis compiled to byte code and executed in Python
Virtual Machine.
• Itis suitable for use as a scripting language, Web
application implementation language, etc.
Introduction to Python
• It has a strong structuring constructs (nested code blocks, functions,
classes, modules, and packages) and the use of objects and object
oriented programming, enables us to write clear, logical applications
for small and large tasks.
• Python is interpreted language.
• Python is available as Free and Open Source.
• Python run on different platform like Windows , Linux , Unix etc
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Introduction to Python
/GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Flavors of python refer to the different types of python compilers.
These are useful to integrate various programming lanluages into
python.
. CPython Standard Python 2.x implemented in C.(python programs run in c)
• Jython Python programs run in java environment http://www.jython.org/
• PyPy Python with a JIT compiler and stackless mode http://pypy.org/(written in
python)
• Stackless Python with enhanced thread support and microthreads
etc. http://www.stackless.com/(tasklets->threads->process)
• IronPython Python for .NET and the CLR http://ironpython.net/
• Python 3 – The new, new Python. This is intended as a replacement for Python 2.x.
http://www.pythht
otp:
n//w
.oww
r.n
giel
/
it.g
dov.
oin/
cgo
/rak
.hpur
Varieties(Flavors) of Python:
• Download the up-to-date source code, binaries,
documentation available on the official website of Python https://
www.python.org/.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Installation of Python on Windows OS
• Python installer downloaded on to your system - Python-
3.8.3.exe
• Click this icon and a installer screen shown as below
Installation of Python on Windows OS – cont.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• Default option will also work, it automatically install the software at
the location, it is showing.
• We can select the customize installation option, to install
the software at the user specified location. - C:Python38-32
Installation of Python on Windows OS – cont.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• To add the Python directory to the path for a particular session in Windows −
• At the command prompt, type
• path %path%;C:Python38-32 and press Enter.
• Where C:Python38-32 is the path of the Python directory.
• You can also select the – “Add Python to Environment Variable” , while installing the Python.
• After setting the path variable
C:>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type
"help", "copyright", "credits" or "license" for more information.
>>>
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Setting Environment Variable
Execution of a python program
• Python file first get compiled to give us byte code and that byte code
is interpreted into machine language.
• This is performed by PVM.
• Execution:
• x.py x.pyc x.exe
Machine Code
Compile Using
Python Compiler
Interpreter
Run using Python
Virtual Machine
Python Code
Python
Compiled File
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Viewing the Byte code
• After the execution of a program, we can not find any .pyc file .
• We should specify the dis module while using python command.
• C:> python –m dis x.py
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Byte code in an understandable
format
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• The dis command is known as “disassembler” that
displays the byte code in an understandable format. The
code represents 5 columns:
1.Line Number
2.offset position of byte code
3.name of byte code
instruction
4.instruction’s argument
5.constants or names (in
brackets)
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• Comments – Everything after # on a line is ignored.
• Blocks and Indentation – It follows the block structure and nested
block structure with indentation.
ABC Block-1
ABC Block-2
ABC Block-3
• Lines – Statement separator is a semi colon, but needed only when
there are more than one statement on a line.
• File Extension- Program have the File Extension “.py”.
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Creating Variables
• Variables are containers for storing data values.
• Python has no command for declaring a variable.
• A variable is created at the time, first value assign to it.
# Numeric Variable
# Character Variable
• x= 5
• y= “Ajay“
• print(x)
• print(y)
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Rules for Variable Naming
• A variable can have a short name (like x and y) or a more
descriptive name (age, EmpName, total_volume).
• Rules for Python variables:
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different
variables)
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Reserve Words – It can not be used as
constant or any other identifier name.
and exec not assert finally or break
for pass class from continue
global raise def if return del
import
try elif in while else is with
except yield lambda
List of the Python keywords
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
>>>help("keywords")
• False
pass
class from or
None
continue global
• True
return
def if raise and del import
• as
while
elif in try assert else is
• async
nonlocal
except
yield
lambda with await finally
• break for not
Another way of access to the Python keywords:
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• >>>import keyword
• >>> keyword.kwlist
• ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from',
'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']
•>>> print(len(keyword.kwlist))#total keywords
35
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Basic Data Types
• The data stored in memory can be of many types. For example, a
person's age is stored as a numeric value and his or her address is
stored as alphanumeric characters.
• Python has five standard data types −
• Numbers
• String
• List
• Tuple
• Dictionary
Data types: Hierarchical View
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Identifiers
• It is a name used to identify a variable, function, class, module or
other objects in Python program.
• An identifier starts with a letter (A to Z) or (a to z) or an underscore
(_) followed by zero or more letters, underscores and digits (0 to 9).
• Python does not allow the characters - @, $, and % .
• Valid Identifier- myvar, my_var , _my_var, myVar, MYVAR, myvar2
First Python Code
• Open the IDLE Python .
• Type
print('Hello, Python!')
• Press the Enter Key.
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
First Python Program
• Open the IDLE Python .
• Open the File-> New File
#program to print the sum of 2 number
n1=10
n2=20
n3=n1+n2
print('Sum= ', n3)
• Save the file with
“.py” extension.
• Click the Run->
Module.
Python Programming
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
References
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• www.w3schools.com
• www.tutorialpoint.com
• Python Programming – Pearson – Taneja Kumar
Thank You
Any Query
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

DAY 1.pptx

  • 1.
    National Institute ofElectronics & Information Technology http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia Gorakhpur Center Ministry of Electronics & Information Technology (MeitY), Government of India
  • 2.
    Contents to becovered http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia 1. Introduction to Python 2. Varieties of Python 3. Installation of Python 4. Setting Environment Variable 5. Basic Data Types 6. First Python Program
  • 3.
    • It wascreated by Guido van Rossum in Netherland, and released in 1991. http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia • The name "Python" was adopted from the Rossum’s favourite comedy series "Monty Python's Flying Circus". • It is a high level general purpose programming language. • Itis compiled to byte code and executed in Python Virtual Machine. • Itis suitable for use as a scripting language, Web application implementation language, etc. Introduction to Python
  • 4.
    • It hasa strong structuring constructs (nested code blocks, functions, classes, modules, and packages) and the use of objects and object oriented programming, enables us to write clear, logical applications for small and large tasks. • Python is interpreted language. • Python is available as Free and Open Source. • Python run on different platform like Windows , Linux , Unix etc http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia Introduction to Python
  • 5.
    /GKP.NIELIT @GKP_NIELIT /NIELITIndia/school/NIELITIndia Flavors of python refer to the different types of python compilers. These are useful to integrate various programming lanluages into python. . CPython Standard Python 2.x implemented in C.(python programs run in c) • Jython Python programs run in java environment http://www.jython.org/ • PyPy Python with a JIT compiler and stackless mode http://pypy.org/(written in python) • Stackless Python with enhanced thread support and microthreads etc. http://www.stackless.com/(tasklets->threads->process) • IronPython Python for .NET and the CLR http://ironpython.net/ • Python 3 – The new, new Python. This is intended as a replacement for Python 2.x. http://www.pythht otp: n//w .oww r.n giel / it.g dov. oin/ cgo /rak .hpur Varieties(Flavors) of Python:
  • 6.
    • Download theup-to-date source code, binaries, documentation available on the official website of Python https:// www.python.org/. http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia Installation of Python on Windows OS
  • 7.
    • Python installerdownloaded on to your system - Python- 3.8.3.exe • Click this icon and a installer screen shown as below Installation of Python on Windows OS – cont. http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 8.
    • Default optionwill also work, it automatically install the software at the location, it is showing. • We can select the customize installation option, to install the software at the user specified location. - C:Python38-32 Installation of Python on Windows OS – cont. http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 9.
    • To addthe Python directory to the path for a particular session in Windows − • At the command prompt, type • path %path%;C:Python38-32 and press Enter. • Where C:Python38-32 is the path of the Python directory. • You can also select the – “Add Python to Environment Variable” , while installing the Python. • After setting the path variable C:>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia Setting Environment Variable
  • 10.
    Execution of apython program • Python file first get compiled to give us byte code and that byte code is interpreted into machine language. • This is performed by PVM. • Execution: • x.py x.pyc x.exe Machine Code Compile Using Python Compiler Interpreter Run using Python Virtual Machine Python Code Python Compiled File http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 11.
    Viewing the Bytecode • After the execution of a program, we can not find any .pyc file . • We should specify the dis module while using python command. • C:> python –m dis x.py http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 12.
    Byte code inan understandable format http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia • The dis command is known as “disassembler” that displays the byte code in an understandable format. The code represents 5 columns: 1.Line Number 2.offset position of byte code 3.name of byte code instruction 4.instruction’s argument 5.constants or names (in brackets)
  • 13.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia • Comments – Everything after # on a line is ignored. • Blocks and Indentation – It follows the block structure and nested block structure with indentation. ABC Block-1 ABC Block-2 ABC Block-3 • Lines – Statement separator is a semi colon, but needed only when there are more than one statement on a line. • File Extension- Program have the File Extension “.py”.
  • 14.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia Creating Variables • Variables are containers for storing data values. • Python has no command for declaring a variable. • A variable is created at the time, first value assign to it. # Numeric Variable # Character Variable • x= 5 • y= “Ajay“ • print(x) • print(y)
  • 15.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia Rules for Variable Naming • A variable can have a short name (like x and y) or a more descriptive name (age, EmpName, total_volume). • Rules for Python variables: • A variable name must start with a letter or the underscore character • A variable name cannot start with a number • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • 16.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia Reserve Words – It can not be used as constant or any other identifier name. and exec not assert finally or break for pass class from continue global raise def if return del import try elif in while else is with except yield lambda
  • 17.
    List of thePython keywords http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia >>>help("keywords") • False pass class from or None continue global • True return def if raise and del import • as while elif in try assert else is • async nonlocal except yield lambda with await finally • break for not
  • 18.
    Another way ofaccess to the Python keywords: http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia • >>>import keyword • >>> keyword.kwlist • ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] •>>> print(len(keyword.kwlist))#total keywords 35
  • 19.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia Basic Data Types • The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. • Python has five standard data types − • Numbers • String • List • Tuple • Dictionary
  • 20.
    Data types: HierarchicalView http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 21.
    Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT@GKP_NIELIT /NIELITIndia /school/NIELITIndia Python Identifiers • It is a name used to identify a variable, function, class, module or other objects in Python program. • An identifier starts with a letter (A to Z) or (a to z) or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). • Python does not allow the characters - @, $, and % . • Valid Identifier- myvar, my_var , _my_var, myVar, MYVAR, myvar2
  • 22.
    First Python Code •Open the IDLE Python . • Type print('Hello, Python!') • Press the Enter Key. Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 23.
    First Python Program •Open the IDLE Python . • Open the File-> New File #program to print the sum of 2 number n1=10 n2=20 n3=n1+n2 print('Sum= ', n3) • Save the file with “.py” extension. • Click the Run-> Module. Python Programming http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
  • 24.
    References http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT/NIELITIndia /school/NIELITIndia • www.w3schools.com • www.tutorialpoint.com • Python Programming – Pearson – Taneja Kumar
  • 25.
    Thank You Any Query http://www.nielit.gov.in/gorakhpur/GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia