3. PYTHON
• Interpreter language, High level language, Dynamic, Garbage Collected language
• Developed by Guido Van Rossum in 1991
• Python Software Foundation
• Official Web Site: https://www.python.org
• Code Readability
• Simple Syntax
• Works quickly and effectively
VERSIONS: PYTHON 2 & PYTHON 3
2020- Python 2.7.15
2021- Python 3.9.7
2000- Python 2
2008- Python 3
4. WHY PYTHON?-FEATURES
• Object Oriented
• Free/Open Source
• Powerful
• Dynamic typing
• Built-in types and tools
• Library utilities
• Third party utilities (e.g. NumPy, sciPy)
• Automatic memory management
• Portable
• Easy to use and learn
• Interactive
5. ADVANTAGES
• Enhanced readability
• Uniform indents to delimit blocks of statements instead of curly brackets
• Free and distributed as open-source software
• Available to large programming community
• Support Python libraries for various applications such as web frameworks, mathematical
computing, and data science
• A cross-platform language
• Works on Windows, Linux, Mac, etc.
• Supports multiple programming paradigms
• Imperative, procedural, object-oriented, and functional programming styles
• An extensible language
• Additional functionality through modules and packages of other languages
• Integrated with other languages
8. INSTALLATION
• Available on https://www.python.org/downloads
• Installed on Windows, Linux, Mac OS and certain other platforms such as IBM
AS/400, iOS, Solaris, etc.
• Windows:
• Web-based installer
• Executable installer
• Embeddable zip files
9.
10.
11.
12.
13.
14.
15. • After successful installation
• Check the Python installation using command prompt
• type python --version or python -V and press
• If Python installed successfully
• Display the installed version
• C:>python --version
• Python 3.7.0
16. • Mac OS X
• https://www.python.org/downloads/mac-osx page.
• Download the latest version under the heading Python Releases for Mac OS X.
• Double click on the installer file to start the installation wizard.
• On the installation wizard, click on Continue a few times
• Until you're asked to agree to the software license agreement,
• Click on Agree and finish the installation.
17. • Linux
• Mostly come up with Python installation
• If Python 3.x is available, run the following command in the Linux terminal:
• $ which python3
• If available, it will return the path to the Python3 executable as
• /usr/local/bin/python3.
• To install Python on Ubuntu 18.04, Ubuntu 20.04 and above,
• Execute the following commands:
• $ sudo apt-get update
• $ sudo apt-get install python3.7 python3-pip
18. 3 STEPS
• Find the interpreter
• Write program
• Run and test
22. SYNTAX
• Set of rules - defines how a Python program will be written and interpreted
• For example – Grammar in English language
23. PYTHON LINE STRUCTURE
• Python program comprises logical lines
• NEWLINE token follows each
• Indicates the end of a logical line of Python code
• Interpreter ignores blank lines.
• >>> print("Hi
How are you?")
SyntaxError: EOL while scanning string literal
Error
24. PYTHON MULTILINE STATEMENTS
• Python does not mandate semicolons
• New line means a new statement
• Sometimes, need to split a statement over two or more lines
• to aid readability
a. Use a backward slash
• >>> print("Hi
how are you?")
b. Put the String in Triple Quotes
• >>> print("""Hi
how are you?""")
• Can distribute a statement without a string across lines.
• >>> a
=
10
>>> print(a)
Output: 10 instead a=10
25. PYTHON COMMENTS
• Declare a comment using an octothorpe (#)
• >>> #This is a comment
PYTHON DOCSTRINGS
• Documentation string
• Unlike comments, they are more specific
• As a comment, this Python Syntax is used to explain code.
>>> def func():
"""
This function prints out a greeting
"""
print("Hi")
>>> func()
Delimit a docstring using three double-quotes.
26. PYTHON INDENTATION
• Doesn’t use curly braces to delimit blocks of code
• Indent code under a function, loop, or class.
• >>> if 2>1:
print("2 is the bigger person");
print("But 1 is worthy too");
• Use 4 spaces or tab
PYTHON MULTIPLE STATEMENTS IN ONE
LINE
• Fit in more than one statement on one line.
• >>> a=7;print(a);
27. PYTHON QUOTATIONS
• Supports single quote and the double quote for string literals
• >>> print('We need a chaperone’);
• >>> print(“We need a chaperone”);
• >>> print(“We need a ‘chaperone’”);
>>> print(“We need a “chaperone””);
>>> print('We need a ‘chaperone’'); ERROR
28. PYTHON BLANK LINES
• If you leave a line with just whitespace, the interpreter will ignore it.
• Name of a program element - user-defined
• Python syntax uniquely identifies the element
• An identifier may only begin with A-Z, a-z, or an underscore(_).
• This may be followed by letters, digits, and underscores- zero or more.
• Python is case-sensitive. Name and name are two different identifiers.
• A reserved keyword may not be used as an identifier.
PYTHON IDENTIFIERS
30. PYTHON VARIABLES
• It is assumed on the basis of the value it holds.
>>> x=10
>>> print(x)
OUTPUT: 10
>>> x='Hello'
>>> print(x)
OUTPUT: Hello
DISCUSS IN
DETAIL LATER…..
31. PYTHON STRING FORMATTERS
a. % Operator
To format a string to contain text and values of identifiers
Use %s where you want a value to appear
After the string, put a % operator
Mention the identifiers in parameters
Example:
>>> x=10; printer="HP"
>>> print("I just printed %s pages to the printer %s" % (x, printer))
Output:
I just printed 10 pages to the printer HP
32. b. Format Method
To format a string in a similar way
At the places, you want to put values
Put 0,1,2,.. in curly braces.
Call the format method on the string
Mention the identifiers in the parameters.
Example:
>>> x=10, printer=‘HP’, y=‘Printer’
>>>print("I just printed {0} pages to the printer {1} {2}".format(x, printer,y))
>>> print("I just printed {x} pages to the printer {printer}".format(x=10, printer='HP'))
Output:
I just printed 10 pages to the printer HP Printer
33. c. f-strings
• Write ‘f’ right before the string
• But outside the quotes used.
>>> print(f"I just printed {x} pages to the printer {printer}")
Output:
I just printed 10 pages to the printer HP
Editor's Notes
Guido Van Rossum conceived Python in the late 1980s. It was released in 1991
at Centrum Wiskunde & Informatica (CWI) in the Netherlands as a successor to
the ABC language. He named this language after a popular comedy show called
'Monty Python's Flying Circus' (and not after Python-the snake).
Extensive basic data types are supported e.g., numbers (floating point,
complex, and unlimited-length long integers), strings (both ASCII and
Unicode), lists, and dictionaries.
Imperative- Statements that change a program's state,
No-built in functions, every operation is coded and the code itself specifies how the problem is to be solved
Procedural- Programming paradigm built around the idea that programs are sequences of instructions to be executed
OOP-Programming paradigm based upon objects (having both data and methods) that aims to incorporate the advantages of modularity and reusability
Check the Add Python 3.7 to PATH checkbox, so that you can execute python scripts from any path. You choose the installation folder or feature by clicking on
Customize installation.
Check the Add Python 3.7 to PATH checkbox, so that you can execute python scripts from any path. You choose the installation folder or feature by clicking on
Customize installation.