UNIT I - Introduction and Syntax of Python Program
UNIT I - Introduction and Syntax of Python Program
08 Marks
INTRODUCTION
? Python is a general purpose, dynamic, high-level, and
interpreted programming language.
? It supports Object Oriented programming approach to
develop applications.
? It is simple and easy to learn and provides lots of high-level
data structures.
PYTHON HISTORY
? Python was invented by Guido van Rossum at National
Institute of Research for Mathematics and Computer
science in Netherlands in 1990.
? 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 FEATURES
? Object-Oriented Language:
⚫ Python supports object-oriented language and concepts of classes
and objects come into existence.
⚫ It supports inheritance, polymorphism, and encapsulation, etc.
? Free and Open Source:
⚫ Python is freely available for everyone. It is freely available on its
official website www.python.org
? Platform-Independent:
⚫ Python can run equally on different platforms such as Windows,
Linux, UNIX, and Macintosh, etc. So, we can say that Python is a
portable language.
APPLICATIONS OF PYTHON :
? GUI based desktop applications
? Graphic design, image processing applications, Games,
and Scientific/ computational Applications
? Web frameworks and applications
? Enterprise and Business applications
? Operating Systems
? Education
? Database Access
? Language Development
? Prototyping
? Software Development
PYTHON BUILDING BLOCKS
? Identifier :Variable name is known as identifier. An identifier is a
name given to entities like class, functions, variables, etc. It helps
to differentiate one entity from another.
? The rules to name an identifier are given below.
✔ The first character of the variable must be an alphabet or
underscore ( _ ).
✔ All the characters except the first character may be an alphabet of
lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).
✔ Identifier name must not contain any white-space, or special
character (!, @, #, %, ^, &, *).
✔ Identifier name must not be similar to any keyword defined in the
language.
✔ Identifier names are case sensitive for example my name, and
MyName is not the same.
✔ Examples of valid identifiers : a123, _n, n_9, etc. o Examples of
invalid identifiers: 1a, n%4, n 9, etc.
PYTHON BUILDING BLOCKS
? Keywords:
⚫ Python Keywords are special reserved words that convey a
special meaning to the compiler/interpreter. Each keyword has a
special meaning and a specific operation. These keywords can't
be used as a variable.
PYTHON BUILDING BLOCKS
? Python Comments:
⚫ Comments are generally used to explain the code.
⚫ Single-line Python Comment: To apply the comment in the code
we use the hash(#) at the beginning of the statement or code.
Ex:
# This is the print statement
print("Hello Python")
⚫ Multiline Python Comment: We must use the hash(#) at the
beginning of every line of code to apply the multiline Python
comment.
⚫ Multiline comments can be declared using two ways. You can
specify these comments using ''' (Triple single quotes) or """
(Triple double quotes). The quotes are mentioned at the beginning
and at the end of the comments.
? Ex: """This is also a
perfect example of multi-line comments"""
PYTHON BUILDING BLOCKS
? Indentation :
⚫ Python provides no braces to indicate blocks of code for class and
function definitions or flow control. Blocks of code are denoted
by line indentation, which is compulsory.
⚫ The number of spaces in the indentation is variable, but all
statements within the block must be indented the same amount.
⚫ For example −
if True:
print "True"
else:
print "False"
⚫ Thus, in Python all the continuous lines indented with same
number of spaces would form a block.
⚫ Indentation can be ignored in line continuation. But it's a good
idea to always indent. It makes the code more readable.
PYTHON BUILDING BLOCKS
? Variables:
⚫ Variables are used to store data, they take memory space based on
the type of value we assigning to them. Creating variables in Python
is simple, you just have write the variable name on the left side of =
and the value on the right side.
⚫ Example
num = 100
str = "BeginnersBook“
print(num)
print(str)
----------------------------------------------------------------------------------------
p = "Hello"
q = "World"
print(p + " " + q)
PYTHON ENVIRONMENT SETUP-
INSTALLATION AND WORKING OF IDE
? To install Python on your operating system, Visit the
link https://www.python.org/downloads/ to download the
latest release of Python
? Step - 1: Select the Python's version to download.
⚫ Click on the download button.
? Step - 2: Click on the Install Now
⚫ Double-click the executable file, which is downloaded; it will
open new window. Select Customize installation and proceed.
⚫ Click on the Add Path check box, it will set the Python path
automatically.
🞆 Step - 3 Installation in Process
? Step- 4 After Successful Installation Click on close Button
⚫ We are ready to work with the Python.
RUNNING SIMPLE PYTHON SCRIPTS TO
DISPLAY ‘WELCOME’ MESSAGE
? Python provides us the two ways to run a program:
⚫ Using Interactive interpreter prompt mode
⚫ Using a script mode
USING INTERACTIVE INTERPRETER PROMPT
? In the interactive mode as we enter a command and press
enter, the very next step we get the output.
? To run python in command prompt type “python”. Then
simply type the Python statement on >>> prompt. As we type
and press enter we can see the output in the very next line.