/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
National Institute of Electronics & Information Technology
Ministry of Electronics & Information Technology (MeitY), Government of India
Gorakhpur Center
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Contents to be covered
1. Introduction to Python
2. Varieties of Python
3. Installation of Python
4. Setting Environment Variable
5. Basic Data Types
6. First Python Program
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• It was created by Guido van Rossum in Netherland, and released in
1991.
• 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.
• It is compiled to byte code and executed in Python Virtual
Machine.
• It is suitable for use as a scripting language, Web application
implementation language, etc.
Introduction to Python
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• 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
Introduction to Python
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @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.python.org/doc/.
Varieties(Flavors) of Python:
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• Download the up-to-date source code, binaries, documentation
available on the official website of Python https://www.python.org/.
Installation of Python on Windows OS
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• 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.
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @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.
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
• To add the Python directory to the path for a par cular 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.
>>>
Setting Environment Variable
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Byte code in an understandable format
• 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)
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
• 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”.
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
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.
• x= 5 # Numeric Variable
• y= “Ajay“ # Character Variable
• print(x)
• print(y)
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
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)
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
List of the Python keywords
>>>help("keywords")
• False class from or None continue global
pass
• True def if raise and del import
return
• as elif in try assert else is
while
• async except lambda with await finally
nonlocal yield
• break for not
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Another way of access to the Python keywords:
• >>>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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Data types: Hierarchical View
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Python Programming
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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
First Python Code
• Open the IDLE Python .
• Type
print('Hello, Python!')
• Press the Enter Key.
Python Programming
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @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
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
References
• www.w3schools.com
• www.tutorialpoint.com
• Python Programming – Pearson – Taneja Kumar
/GKP.NIELIT
http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Thank You
Any Query

DAYdats science unit 1 1.pdf

  • 1.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center
  • 2.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Contents to be covered 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia • It was created by Guido van Rossum in Netherland, and released in 1991. • 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. • It is compiled to byte code and executed in Python Virtual Machine. • It is suitable for use as a scripting language, Web application implementation language, etc. Introduction to Python
  • 4.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia • 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 Introduction to Python
  • 5.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @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.python.org/doc/. Varieties(Flavors) of Python:
  • 6.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia • Download the up-to-date source code, binaries, documentation available on the official website of Python https://www.python.org/. Installation of Python on Windows OS
  • 7.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia • 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.
  • 8.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @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.
  • 9.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia • To add the Python directory to the path for a par cular 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. >>> Setting Environment Variable
  • 10.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia 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
  • 11.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @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
  • 12.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Byte code in an understandable format • 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming • 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming 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. • x= 5 # Numeric Variable • y= “Ajay“ # Character Variable • print(x) • print(y)
  • 15.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia List of the Python keywords >>>help("keywords") • False class from or None continue global pass • True def if raise and del import return • as elif in try assert else is while • async except lambda with await finally nonlocal yield • break for not
  • 18.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Another way of access to the Python keywords: • >>>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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Data types: Hierarchical View
  • 21.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia Python Programming 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.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia First Python Code • Open the IDLE Python . • Type print('Hello, Python!') • Press the Enter Key. Python Programming
  • 23.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @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
  • 24.
    /GKP.NIELIT http://www.nielit.gov.in/gorakhpur @GKP_NIELIT /NIELITIndia/school/NIELITIndia References • www.w3schools.com • www.tutorialpoint.com • Python Programming – Pearson – Taneja Kumar
  • 25.