Syllabus Module-1
• Python 3- Overview, history, features. Python 3 - Environment Setup,
Local Environment Setup. Interactive Mode Programming, Script
Mode Programming.
• Python Identifiers, Reserved Words, Lines and Indentation, Multiline
Statements, Quotation, Comments, Multiple Statements on a Single
Line, Multiple Statement Groups as Suites.
• https://www.pyforschool.com/tutorials.html
Python
• Python is an object-oriented programming language created by
Guido Van Rossum in 1989 and it is very easy to learn
• A Dutch programmer named Guido van Rossum
made(released) Python in 1991. He named it after the television
show Monty Python's Flying Circus.
• Python is a general-purpose, interpreted, interactive, and high-level
programming language with dynamic semantics
• Python can be used to develop different applications like web
applications, graphic user interface based applications, software
development application, scientific and numeric applications,
network programming, Games and 3D applications and other
business applications.
Python History
• Python was invented by Guido van Rossum in 1991 at CWI in
Netherland.
• The idea of Python programming language has taken from the ABC
programming language or we can say that ABC is a predecessor of
Python language(Replacement of BASIC programming language).
• There is also a fact behind the choosing name Python. Guido van
Rossum was a fan of the popular BBC comedy show of that
time, "Monty Python's Flying Circus". So he decided to pick the
name Python for his newly created programming language.
• Python has the vast community across the world and releases its
version within the short period.
Why python is famous
• Java came in 1995 and Python came in 1985
• But with the advancement of m/c learning, AI ,
(this is a field where we have research scientists).
• They don’t want to spend much time on learning programming
languages
• They went for the easiest language available and the easiest language
available in the market is Python. It is much simpler than c, c++, and
Java
• So most of the schools and colleges are teaching python
Write programs with fewer lines
• To print the "Hello World" program in Java; it will take three lines to print it.
public class HelloWorld {
public static void main(String[] args){
// Prints "Hello, World" to the terminal window.
System.out.println("Hello World");
• On the other hand, we can do this using one statement in Python.
• print("Hello World")
• Both programs will print the same result, but it takes only one statement without using a
semicolon or curly
Why Python(Features)
Python provides many useful features to the programmer. These features make it most popular and
widely used language. Few essential features of Python are listed below
• Easy to use and Learn
• Expressive Language
• Interpreted Language
• Object-Oriented Language
• Open Source Language
• Extensible
• Learn Standard Library
• GUI Programming Support
• Integrated
• Embeddable
• Dynamic Memory Allocation
• Wide Range of Libraries and Frameworks
Python features
Why Python(Features)
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python is Free and Open Source
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.(eg:- next slide)
• 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.
• Python can be treated in a procedural way, an object-orientated way or a functional way.
Python features
Where is Python used?
Python is a general-purpose, popular programming language and it is used in
almost every technical field. The various areas of Python use are given below.
• Data Science
• Date Mining
• Desktop Applications
• Console-based Applications
• Mobile Applications
• Software Development
• Artificial Intelligence
• Web Applications
• Enterprise Applications
• 3D CAD Applications
• Machine Learning
• Computer Vision or Image Processing Applications.
• Speech Recognitions
Who uses Python
• Youtube
• Google
• Dropbox
• BitTorrent
• Nasa
• Netflix
• Which all are companies using python?
• Small small companies,who uses python Google,Dropbox,YouTube,Yahoo,NASA,are
companies who uses python as a main language or as a support language.
• Eg:- google has features. Some of these features is coded in python.
• Today we have big website called Reddit . Reddit is written in Python.
Python versions
• Python has multiple versions. 1.x, 2.x, 3.x
• Python1.0 - January 1994
• Python2.0- October 2000
• Python3.0- December 2008
But 2.x is also available
https://www.python.org/doc/versions/
https://www.python.org/doc/versions/
• For unreleased (in development) documentation, see
In Development Versions.
• Python 3.11.4, documentation released on 6 June 2023.
• Python 3.11.3, documentation released on 5 April 2023.
• Python 3.11.2, documentation released on 8 February 2023.
• Python 3.11.1, documentation released on 6 December 2022.
• Python 3.11.0, documentation released on 24 October 2022.
• Python 3.10.12, documentation released on 6 June 2023.
• Python 3.10.11, documentation released on 5 April 2023.
• Python 3.10.10, documentation released on 8 February 2023.
• Python 3.10.9, documentation released on 6 December 2022.
• Python 3.10.8, documentation released on 8 October 2022.
• https://www.javatpoint.com/how-to-install-python
Python Environment Setup
• To get started working with Python 3, you’ll need to have access to the Python interpreter.
• Python can be obtained from the Python Software Foundation website at python.org. Typically, that involves
downloading the appropriate installer for your operating system and running it on your machine.
• Some operating systems, notably Linux, provide a package manager that can be run to install Python.
• On macOS, the best way to install Python 3 involves installing a package manager called Homebrew. You’ll see how to do this in the
relevant section in the tutorial.
• On mobile operating systems like Android and iOS, you can install apps that provide a Python programming environment
For Windows
Step 1: Download the Python 3 Installer
• Open a browser window and navigate to the Download page for Windows at python.org.
• Underneath the heading at the top that says Python Releases for Windows, click on the
link for the Latest Python 3 Release - Python 3.x.x. (As of this writing, the latest is Python
3.6.5.)
• Scroll to the bottom and select either Windows x86-64 executable installer for 64-bit
or Windows x86 executable installer for 32-bit.
Step 2: Run the Installer
Once you have chosen and downloaded an installer, simply run it by double-clicking on the
downloaded file. A dialog should appear that looks something like this:
• Important: You want to be sure to check the box that says Add
Python 3.x to PATH as shown to ensure that the interpreter will be
placed in your execution path.
• IDLE (Integrated Development and Learning Environment) is an
integrated development environment (IDE) for Python
• The Python installer for Windows contains the IDLE module by
default
• IDLE can be used to execute a single statement just
like Python Shell and also to create, modify, and
execute Python scripts
Python provides us the two ways to run a program:
1)Interactive mode
2) Script mode
Interactive Mode
• Interactive mode, also known as the REPL provides us with a quick way of
running blocks or a single line of Python code
• The code executes via the Python shell, which comes with Python installation
• The >>> indicates that the Python shell is ready to execute and send your
commands to the Python interpreter. The result is immediately displayed on
the Python shell as soon as the Python interpreter interprets the command.
• To run your Python statements, just type them and hit the enter key. You will
get the results immediately,
Eg: >>> print("Hello World")
Hello World
>>>
Interactive Mode Programming
• In Python, there are two options/methods for running code:
1)Interactive mode
2) Script 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.
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.
Refer the link : https://www.javatpoint.com/python-example
Interactive mode
Script Mode
• If you need to write a long piece of Python code or your Python script
spans multiple files, interactive mode is not recommended. Script
mode is the way to go in such cases.
• In script mode, You write your code in a text file then save it with
a .py extension which stands for "Python". Note that you can use any
text editor for this, including Sublime, Atom, notepad++, etc.
Pros and Cons of Script Mode
Pros:
The following are the advantages of running your code in script mode:
• It is easy to run large pieces of code.
• Editing your script is easier in script mode.
• Good for both beginners and experts.
The following are the disadvantages of using the script mode:
• Can be tedious when you need to run only a single or a few lines of
code.
• You must create and save a file before executing your code.
Key Differences Between Interactive and Script Mode
Here are the key differences between programming in interactive mode
and programming in script mode:
• In script mode, a file must be created and saved before executing the
code to get results. In interactive mode, the result is returned
immediately after pressing the enter key.
• In script mode, you are provided with a direct way of editing your
code. This is not possible in interactive mode.
Variables and Identifiers
• Variables means its value can vary.
• It is a part of the computer’s memory where information is stored.
• To identify easily each variable is given an appropriate name.
• It can be used to refer the value later in the program
• Variables are reserved memory locations that store values.
• Variables are examples of identifiers
Python Identifier
• A Python identifier is a name used to identify a variable, function, class, module or
other object.
• It helps to differentiate one entity from another.
Rules for writing identifiers
1)Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to
Z) or digits (0 to 9) or an underscore (_)
2) An identifier cannot start with a digit.
3) Keywords cannot be used as identifiers
4) We cannot use special symbols like !, @, #, $, % etc. in our identifier
5) Identifier can be of any length.
Eg:- sum, num1, var_20,First, _my_var,etc. are valid identifier names.
1num,my-var,%check,Basic sal,H#RA are invalid identifiers
Note: Python is case-sensitive language. For example, myvar and myVar are not the
same
Reserved Words or Keywords
• In every programming language there are certain words which have a pre-
defined meaning. These words cannot be used for naming identifiers.
• Python has a set of keywords that are reserved words that cannot be used
as variable names, function names, or any other identifiers
• They are used to define the syntax and structure of the Python language.
• In Python, keywords are case sensitive except True, False and None
• There are 33 keywords in Python
You can always get the list of keywords in your current version by typing the
following in the prompt.
>>>import keyword
>>>print(keyword.kwlist)
Keywords
['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']
Indentation
• White space at the beginning of the line is called indentation.
• In Python indentation is used to associate and group statements.
• The level of indentation, groups statements to form a block of
statements.
• This means that statements in a block must have the same
indentation level
Lines and Indentation
• Statements typically end with ‘New Line’ character(Enter key) is called Lines
• Most of the programming languages like C, C++, Java use braces { } to define
a block of code. Python uses indentation.
• A code block (body of a function, loop etc.) starts with indentation and ends
with the first unindented line. The amount of indentation is up to you, but it
must be consistent throughout that block.
• Generally four whitespaces are used for indentation and is preferred over
tabs.
• In Python, indentation is used to declare a block. If two
statements are at the same indentation level, then they are
the part of the same block.
Multiline Statements
• A statement in Python is a logical instruction which Python interpreter
can read and execute. In Python, it could be an expression or an
assignment statement.
• Usually, every Python statement ends with a newline character.
However, we can extend it over to multiple lines using the line
continuation character (\).
• Python gives us two ways to enable multi-line statements in a
program.
1)Explicit Line Continuation using \
2) Implicit Line Continuation -in parenthesis
Eg:
message = "Hello There.\nYou have come to the right place to learn Python
Programming.\n" \
"Follow the tutorials to become expert in Python. " \
"Don't forget to share it with your friends too."
math_result = 1 + 2 + 3 + 4 + \
5+6+7+8+\
9 + 10
print(message)
print(math_result)
print(message)
Hello There.
You have come to the right place to learn Python
Programming.
Follow the tutorials to become expert in Python. Don't forget to share it with
your friends too.
print(math_result)
55
Comments
• Comments are the non-executable statements in a program.
• Comments are the way to improve the readability of a code, by explaining what we have
done in code in simple English
• A comment is a text that doesn’t affect the outcome of a code, it is just a piece of text to
let someone know what you have done in a program or what is being done in a block of
code.
• This is especially helpful when someone else has written a code and you are analyzing it
for bug fixing or making a change in logic, by reading a comment you can understand the
purpose of code much faster then by just going through the actual code.
• Comments starts with a hash sign # , and Python will ignore them:
• You can type a comment in a new line or on the same line after a statement or expression
# this is a comment
Print (“Hello”) # to display Hello
# program end here
Suites
• A group of individual statements, which make a single code block are called
suites in Python.
• Compound or complex statements, such as if, while, def, and class require a
header line and a suite.
• Header lines begin the statement (with the keyword) and terminate with a
colon (: ) and are followed by one or more lines which make up the suite.
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even")
Example
if expr1==True:
stmt1
stmt2
elif expr2==True:
stmt3
stmt4
else:
stmt5
stmt6
while expr==True:
stmt1
stmt2